-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from sliit-foss/fix/negative-countdown
Fix negative values in countdown after deadline
- Loading branch information
Showing
9 changed files
with
46 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const CURRENT_YEAR = 2024; | ||
export const TIME_REGISTRATION_CLOSING = 'September 12, 2024 23:59:00'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,35 @@ | ||
import { useEffect } from 'react'; | ||
import { useState } from 'react'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
function useCountdown({ targetDate }) { | ||
const countDownDate = targetDate.getTime(); | ||
const [remainingTime, setRemainingTime] = useState(countDownDate - new Date().getTime()); | ||
const countDownDate = new Date(targetDate).getTime(); | ||
const getRemainingTime = () => Math.max(0, countDownDate - Date.now()); | ||
|
||
const [remainingTime, setRemainingTime] = useState(getRemainingTime()); | ||
const [didCountDownComplete, setDidCountDownComplete] = useState(false); | ||
|
||
useEffect(() => { | ||
const interval = setInterval(() => { | ||
setRemainingTime(countDownDate - new Date().getTime()); | ||
const timeLeft = getRemainingTime(); | ||
setRemainingTime(timeLeft); | ||
setDidCountDownComplete(timeLeft === 0); | ||
}, 1000); | ||
|
||
return () => clearInterval(interval); | ||
}, [targetDate]); | ||
|
||
const formatTimeUnit = (val) => | ||
Number(val).toLocaleString('en-US', { | ||
minimumIntegerDigits: 2 | ||
}); | ||
|
||
const extractUnitVals = (remainingTime) => { | ||
const days = formatTimeUnit(Math.floor(remainingTime / (1000 * 60 * 60 * 24))); | ||
const hours = formatTimeUnit(Math.floor((remainingTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))); | ||
const minutes = formatTimeUnit(Math.floor((remainingTime % (1000 * 60 * 60)) / (1000 * 60))); | ||
const seconds = formatTimeUnit(Math.floor((remainingTime % (1000 * 60)) / 1000)); | ||
|
||
return { | ||
days, | ||
hours, | ||
minutes, | ||
seconds, | ||
milliSeconds: remainingTime | ||
}; | ||
}; | ||
|
||
return extractUnitVals(remainingTime); | ||
}, [countDownDate]); | ||
|
||
const extractUnitVals = (time) => ({ | ||
days: Math.floor(time / (1000 * 60 * 60 * 24)), | ||
hours: Math.floor((time / (1000 * 60 * 60)) % 24), | ||
minutes: Math.floor((time / (1000 * 60)) % 60), | ||
seconds: Math.floor((time / 1000) % 60) | ||
}); | ||
|
||
const formatUnitTime = (val) => val.toLocaleString('en-US', { minimumIntegerDigits: 2 }); | ||
|
||
const transformTime = (time) => | ||
Object.entries(extractUnitVals(time)).reduce((acc, [key, value]) => ({ ...acc, [key]: formatUnitTime(value) }), {}); | ||
|
||
return { ...transformTime(remainingTime), didCountDownComplete }; | ||
} | ||
|
||
export default useCountdown; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters