Skip to content

Commit

Permalink
fix(EMI-2145): auction artwork timer real-time countdown (#10941)
Browse files Browse the repository at this point in the history
  • Loading branch information
rquartararo authored Oct 11, 2024
1 parent 2954fd1 commit 31ba2f5
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/app/Components/ArtworkGrids/ArtworkAuctionTimer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ArtworkAuctionTimer_collectorSignals$key } from "__generated__/ArtworkA
import { formattedTimeLeft } from "app/Scenes/Activity/utils/formattedTimeLeft"
import { getTimer } from "app/utils/getTimer"
import { DateTime } from "luxon"
import { useEffect, useRef, useState } from "react"
import { graphql, useFragment } from "react-relay"

interface ArtworkAuctionTimerProps {
Expand All @@ -11,19 +12,32 @@ interface ArtworkAuctionTimerProps {
inRailCard?: boolean
}

const INTERVAL = 1000

export const ArtworkAuctionTimer: React.FC<ArtworkAuctionTimerProps> = ({
collectorSignals,
hideRegisterBySignal,
inRailCard,
}) => {
const lineHeight = inRailCard ? "20px" : "18px"
const data = useFragment(fragment, collectorSignals)
const { lotClosesAt, onlineBiddingExtended, registrationEndsAt } = data.auction ?? {}

if (!data.auction) {
return null
}
const lotEndAt = DateTime.fromISO(lotClosesAt ?? "")
const intervalId = useRef<ReturnType<typeof setInterval> | null>(null)
const [time, setTime] = useState(getTimer(lotClosesAt ?? "")["time"])

useEffect(() => {
intervalId.current = setInterval(() => {
const { time: timerTime } = getTimer(lotClosesAt ?? "")

const { lotClosesAt, onlineBiddingExtended, registrationEndsAt } = data.auction
setTime(timerTime)
}, INTERVAL)

return () => {
if (intervalId.current) clearInterval(intervalId.current)
}
}, [])

if (
registrationEndsAt &&
Expand All @@ -39,14 +53,11 @@ export const ArtworkAuctionTimer: React.FC<ArtworkAuctionTimerProps> = ({
)
}

const lotEndAt = DateTime.fromISO(lotClosesAt ?? "")

if (!lotClosesAt || lotEndAt.diffNow().as("days") > 5 || lotEndAt.diffNow().as("seconds") <= 0) {
return null
}

const timerColor = lotEndAt.diffNow().as("hours") <= 1 ? "red100" : "blue100"
const { time } = getTimer(lotClosesAt)
const { timerCopy } = formattedTimeLeft(time)

if (onlineBiddingExtended) {
Expand Down

0 comments on commit 31ba2f5

Please sign in to comment.