Skip to content

Commit

Permalink
Refactor countdown hook to use enum for status management
Browse files Browse the repository at this point in the history
  • Loading branch information
skyturkish committed Mar 2, 2024
1 parent 1aca42c commit 221d56a
Showing 1 changed file with 26 additions and 34 deletions.
60 changes: 26 additions & 34 deletions src/countdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ type useCountdownParams = {
onCompleted?: VoidFunction
}

enum CountdownStatus {
Inactive,
Running,
Paused,
}

/**
* @name useCountdown
* @description React hook countdown timer.
Expand All @@ -35,20 +41,16 @@ const useCountdown = ({
)

// status
const [isActive, setIsActive] = useState(false)
const [isInactive, setIsInactive] = useState(true)
const [isRunning, setIsRunning] = useState(false)
const [isPaused, setIsPaused] = useState(false)
const [status, setStatus] = useState<CountdownStatus>(
CountdownStatus.Inactive,
)

useEffect(
() => {
if (autoStart) {
id.current = window.setInterval(calculateRemainingTime, 1000)

setIsActive(true)
setIsInactive(false)
setIsRunning(true)
setIsPaused(false)
setStatus(CountdownStatus.Running)
}

return () => window.clearInterval(id.current)
Expand All @@ -64,10 +66,7 @@ const useCountdown = ({
window.clearInterval(id.current)
onCompleted?.()

setIsActive(false)
setIsInactive(true)
setIsRunning(false)
setIsPaused(false)
setStatus(CountdownStatus.Inactive)

return 0
}
Expand All @@ -77,29 +76,26 @@ const useCountdown = ({
}

const pause = (): void => {
if (isPaused || isInactive) {
if (
status === CountdownStatus.Paused ||
status === CountdownStatus.Inactive
) {
return
}

window.clearInterval(id.current)

setIsActive(true)
setIsInactive(false)
setIsRunning(false)
setIsPaused(true)
setStatus(CountdownStatus.Paused)
}

const start = (): void => {
if (isRunning) {
if (status === CountdownStatus.Running) {
return
}

id.current = window.setInterval(calculateRemainingTime, 1000)

setIsActive(true)
setIsInactive(false)
setIsRunning(true)
setIsPaused(false)
setStatus(CountdownStatus.Running)
}

const reset = (time: Time = {minutes, seconds}) => {
Expand All @@ -108,15 +104,9 @@ const useCountdown = ({
if (autoStart) {
id.current = window.setInterval(calculateRemainingTime, 1000)

setIsActive(true)
setIsInactive(false)
setIsRunning(true)
setIsPaused(false)
setStatus(CountdownStatus.Running)
} else {
setIsActive(false)
setIsInactive(true)
setIsRunning(false)
setIsPaused(false)
setStatus(CountdownStatus.Inactive)
}

setRemainingTime(calculateInitialTime(time))
Expand All @@ -126,10 +116,12 @@ const useCountdown = ({
minutes: calculateRemainingMinutes(remainingTime),
seconds: calculateRemainingSeconds(remainingTime),
formatted: formatTime(remainingTime, format),
isActive,
isInactive,
isRunning,
isPaused,
isActive:
status === CountdownStatus.Running ||
status === CountdownStatus.Paused,
isInactive: status === CountdownStatus.Inactive,
isRunning: status === CountdownStatus.Running,
isPaused: status === CountdownStatus.Paused,
start,
pause,
resume: start,
Expand Down

0 comments on commit 221d56a

Please sign in to comment.