Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor countdown hook to use enum for status management #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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