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

fix/time interval on cool down email verification process #4933

Merged
merged 1 commit into from
Jan 13, 2025
Merged
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
109 changes: 59 additions & 50 deletions src/components/InputUserEmailVerify.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ const InputUserEmailVerify = forwardRef<HTMLInputElement, InputType>(
// It will send request to backend to check if email exists and if it's not verified yet
// or email is already exist on another user account
// If email isn't verified it will send email with verification code to user
let intervalId: NodeJS.Timeout;

const verificationEmailHandler = async () => {
// Prevent the button from being clicked during cooldown
if (isCooldown) {
Expand All @@ -245,7 +247,7 @@ const InputUserEmailVerify = forwardRef<HTMLInputElement, InputType>(
setIsCooldown(true);
setCooldownTime(180);

const intervalId = setInterval(() => {
intervalId = setInterval(() => {
setCooldownTime(prev => {
if (prev <= 1) {
resetCoolDown();
Expand Down Expand Up @@ -286,12 +288,6 @@ const InputUserEmailVerify = forwardRef<HTMLInputElement, InputType>(
}),
);
}

// Stop the timer when fetch ends
resetCoolDown();

// Clear interval when fetch is done
clearInterval(intervalId);
} catch (error) {
if (error instanceof Error) {
clearInterval(intervalId);
Expand Down Expand Up @@ -338,6 +334,12 @@ const InputUserEmailVerify = forwardRef<HTMLInputElement, InputType>(
email: email,
isEmailVerified: true,
});

// Stop the timer when fetch ends
resetCoolDown();

// Clear interval when fetch is done
clearInterval(intervalId);
}
} catch (error) {
if (error instanceof Error) {
Expand Down Expand Up @@ -436,36 +438,7 @@ const InputUserEmailVerify = forwardRef<HTMLInputElement, InputType>(
size={InputSizeToLinkSize(size)}
$validationstatus={validationStatus}
>
{isCooldown && (
<InputCodeDesc>
<FormattedMessage
id='label.email_cooldown'
values={{
button: chunks => (
<button
type='button'
onClick={
verificationEmailHandler
}
disabled={isCooldown}
>
{chunks}
</button>
),
time: () => (
<b>
{Math.floor(cooldownTime / 60)}:
{(
'0' +
(cooldownTime % 60)
).slice(-2)}
</b>
),
}}
/>
</InputCodeDesc>
)}
{!isCooldown && inputDescription}
{inputDescription}
</InputDesc>
)}
{isVerificationProcess && (
Expand Down Expand Up @@ -524,19 +497,55 @@ const InputUserEmailVerify = forwardRef<HTMLInputElement, InputType>(
</Absolute>
</InputWrapper>
<InputCodeDesc>
<FormattedMessage
id='label.email_get_resend'
values={{
button: chunks => (
<button
type='button'
onClick={verificationEmailHandler}
>
{chunks}
</button>
),
}}
/>
{isCooldown && (
<InputCodeDesc>
<FormattedMessage
id='label.email_cooldown'
values={{
button: chunks => (
<button
type='button'
onClick={
verificationEmailHandler
}
disabled={isCooldown}
>
{chunks}
</button>
),
time: () => (
<b>
{Math.floor(
cooldownTime / 60,
)}
:
{(
'0' +
(cooldownTime % 60)
).slice(-2)}
</b>
),
}}
/>
</InputCodeDesc>
)}
{!isCooldown && (
<FormattedMessage
id='label.email_get_resend'
values={{
button: chunks => (
<button
type='button'
onClick={
verificationEmailHandler
}
>
{chunks}
</button>
),
}}
/>
)}
</InputCodeDesc>
</ValidationCode>
)}
Expand Down
Loading