Skip to content

Commit 3f3b704

Browse files
committed
Adjust logic to make it work everyday
Adjust logic to make it work everyday by updating the day at midnight of everyday.
1 parent 1af8f88 commit 3f3b704

3 files changed

Lines changed: 33 additions & 3 deletions

File tree

src/components/models/props.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ export type TimeBasedRenderingProps = {
66
startDate: Date
77
endDate: Date
88
}
9+
10+
export type TimeBasedRenderingV1Props = {
11+
children: ReactNode
12+
incomingStartDate: Date
13+
incomingEndDate: Date
14+
}

src/components/v1/TimeBasedWrapper.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
// React
22
import { useEffect, useState } from 'react'
33
// Models
4-
import { TimeBasedRenderingProps as Props } from '../models/props'
4+
import { TimeBasedRenderingV1Props as Props } from '../models/props'
55

6-
const TimeBasedWrapper = ({ startDate, endDate, children }: Props) => {
6+
const TimeBasedWrapper = ({ incomingStartDate, incomingEndDate, children }: Props) => {
7+
const [startDate, setStartDate] = useState(incomingStartDate)
8+
const [endDate, setEndDate] = useState(incomingEndDate)
79
const [canRender, setCanRender] = useState(false)
810

911
useEffect(() => {
1012
const currentDate = new Date()
1113
let startRenderingTimeOut: NodeJS.Timeout | undefined = undefined
1214
let stopRenderingTimeOut: NodeJS.Timeout | undefined = undefined
15+
let nextDayUpdateTimeOut: NodeJS.Timeout | undefined = undefined
1316

1417
const shouldRenderNow = currentDate.getTime() >= startDate.getTime() && currentDate.getTime() <= endDate.getTime()
1518

@@ -33,9 +36,18 @@ const TimeBasedWrapper = ({ startDate, endDate, children }: Props) => {
3336
stopRenderingTimeOut = setTimeout(() => setCanRender(false), timeToStopRendering)
3437
}
3538

39+
// Update the date (day) of the start and end dates so it would run everyday
40+
// Basically we have a timeout to launch after midnight by one second to update the day of the start and end dates
41+
const timeUntilMidnight = new Date(new Date().setHours(0, 0, 0, 0)).getTime() - new Date().getTime() + 1000
42+
nextDayUpdateTimeOut = setTimeout(() => {
43+
setStartDate(new Date(startDate.setDate(startDate.getDate() + 1)))
44+
setEndDate(new Date(endDate.setDate(endDate.getDate() + 1)))
45+
}, timeUntilMidnight)
46+
3647
return () => {
3748
clearTimeout(startRenderingTimeOut)
3849
clearTimeout(stopRenderingTimeOut)
50+
clearTimeout(nextDayUpdateTimeOut)
3951
}
4052
}, [startDate, endDate])
4153

src/components/v2/hooks/useTimeBasedRendering.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { useEffect, useState } from 'react'
22

3-
export const useTimeBasedRendering = (startDate: Date, endDate: Date) => {
3+
export const useTimeBasedRendering = (incomingStartDate: Date, incomingEndDate: Date) => {
4+
const [startDate, setStartDate] = useState(incomingStartDate)
5+
const [endDate, setEndDate] = useState(incomingEndDate)
46
const [canRender, setCanRender] = useState(false)
57

68
useEffect(() => {
79
const currentDate = new Date()
810
let startRenderingTimeOut: NodeJS.Timeout | undefined = undefined
911
let stopRenderingTimeOut: NodeJS.Timeout | undefined = undefined
12+
let nextDayUpdateTimeOut: NodeJS.Timeout | undefined = undefined
1013

1114
const shouldRenderNow = currentDate.getTime() >= startDate.getTime() && currentDate.getTime() <= endDate.getTime()
1215

@@ -30,9 +33,18 @@ export const useTimeBasedRendering = (startDate: Date, endDate: Date) => {
3033
stopRenderingTimeOut = setTimeout(() => setCanRender(false), timeToStopRendering)
3134
}
3235

36+
// Update the date (day) of the start and end dates so it would run everyday
37+
// Basically we have a timeout to launch after midnight by one second to update the day of the start and end dates
38+
const timeUntilMidnight = new Date(new Date().setHours(0, 0, 0, 0)).getTime() - new Date().getTime() + 1000
39+
nextDayUpdateTimeOut = setTimeout(() => {
40+
setStartDate(new Date(startDate.setDate(startDate.getDate() + 1)))
41+
setEndDate(new Date(endDate.setDate(endDate.getDate() + 1)))
42+
}, timeUntilMidnight)
43+
3344
return () => {
3445
clearTimeout(startRenderingTimeOut)
3546
clearTimeout(stopRenderingTimeOut)
47+
clearTimeout(nextDayUpdateTimeOut)
3648
}
3749
}, [startDate, endDate])
3850

0 commit comments

Comments
 (0)