-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add timer to resend passcode action
- Loading branch information
Showing
6 changed files
with
99 additions
and
8 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
53 changes: 53 additions & 0 deletions
53
web-marketplace/src/components/organisms/LoginButton/hooks/useTimer.tsx
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,53 @@ | ||
import { useCallback, useEffect, useState } from 'react'; | ||
|
||
type Props = { | ||
duration: number; | ||
}; | ||
|
||
export const useTimer = ({ duration }: Props) => { | ||
const [timeLeft, setTimeLeft] = useState(duration); | ||
const [timerId, setTimerId] = useState<NodeJS.Timeout | null>(null); | ||
|
||
const startTimer = () => { | ||
if (timerId !== null) return; | ||
|
||
const id = setInterval(() => { | ||
setTimeLeft(time => time - 1); | ||
}, 1000); | ||
|
||
setTimerId(id); | ||
}; | ||
|
||
const stopTimer = useCallback(() => { | ||
if (timerId !== null) { | ||
clearInterval(timerId); | ||
setTimerId(null); | ||
} | ||
}, [timerId]); | ||
|
||
const resetTimer = useCallback(() => { | ||
stopTimer(); | ||
setTimeLeft(duration); | ||
}, [duration, stopTimer]); | ||
|
||
useEffect(() => { | ||
return () => { | ||
if (timerId !== null) { | ||
clearInterval(timerId); | ||
} | ||
}; | ||
}, [timerId]); | ||
|
||
useEffect(() => { | ||
if (timeLeft <= 0) { | ||
resetTimer(); | ||
} | ||
}, [resetTimer, timeLeft]); | ||
|
||
return { | ||
timeLeft: timerId !== null ? timeLeft : null, | ||
resetTimer, | ||
startTimer, | ||
stopTimer, | ||
}; | ||
}; |
18 changes: 18 additions & 0 deletions
18
web-marketplace/src/components/organisms/LoginButton/utils/getResendCodeButtonLink.tsx
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,18 @@ | ||
import { RESEND_BUTTON_TEXT } from '../LoginButton.constants'; | ||
|
||
type Params = { | ||
resendTimeLeft: number | null; | ||
onResendPasscode: () => Promise<void>; | ||
}; | ||
|
||
export const getResendCodeButtonLink = ({ | ||
resendTimeLeft, | ||
onResendPasscode, | ||
}: Params) => { | ||
return resendTimeLeft | ||
? undefined | ||
: { | ||
text: RESEND_BUTTON_TEXT, | ||
onClick: onResendPasscode, | ||
}; | ||
}; |
11 changes: 11 additions & 0 deletions
11
web-marketplace/src/components/organisms/LoginButton/utils/getResendCodeLabel.tsx
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,11 @@ | ||
import { RESEND_TEXT } from '../LoginButton.constants'; | ||
|
||
type Params = { | ||
resendTimeLeft: number | null; | ||
}; | ||
|
||
export const getResendCodeLabel = ({ resendTimeLeft }: Params) => { | ||
return resendTimeLeft === null | ||
? RESEND_TEXT | ||
: `Resend after ${resendTimeLeft} seconds`; | ||
}; |