From e2489ae975ac5914976fcd76c6069bf02db1ad96 Mon Sep 17 00:00:00 2001 From: kkatusic Date: Mon, 13 Jan 2025 13:29:14 +0100 Subject: [PATCH] fix/time interval on cool down email verification process --- src/components/InputUserEmailVerify.tsx | 109 +++++++++++++----------- 1 file changed, 59 insertions(+), 50 deletions(-) diff --git a/src/components/InputUserEmailVerify.tsx b/src/components/InputUserEmailVerify.tsx index 3c38eb3858..e7949d8ea2 100644 --- a/src/components/InputUserEmailVerify.tsx +++ b/src/components/InputUserEmailVerify.tsx @@ -235,6 +235,8 @@ const InputUserEmailVerify = forwardRef( // 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) { @@ -245,7 +247,7 @@ const InputUserEmailVerify = forwardRef( setIsCooldown(true); setCooldownTime(180); - const intervalId = setInterval(() => { + intervalId = setInterval(() => { setCooldownTime(prev => { if (prev <= 1) { resetCoolDown(); @@ -286,12 +288,6 @@ const InputUserEmailVerify = forwardRef( }), ); } - - // Stop the timer when fetch ends - resetCoolDown(); - - // Clear interval when fetch is done - clearInterval(intervalId); } catch (error) { if (error instanceof Error) { clearInterval(intervalId); @@ -338,6 +334,12 @@ const InputUserEmailVerify = forwardRef( 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) { @@ -436,36 +438,7 @@ const InputUserEmailVerify = forwardRef( size={InputSizeToLinkSize(size)} $validationstatus={validationStatus} > - {isCooldown && ( - - ( - - ), - time: () => ( - - {Math.floor(cooldownTime / 60)}: - {( - '0' + - (cooldownTime % 60) - ).slice(-2)} - - ), - }} - /> - - )} - {!isCooldown && inputDescription} + {inputDescription} )} {isVerificationProcess && ( @@ -524,19 +497,55 @@ const InputUserEmailVerify = forwardRef( - ( - - ), - }} - /> + {isCooldown && ( + + ( + + ), + time: () => ( + + {Math.floor( + cooldownTime / 60, + )} + : + {( + '0' + + (cooldownTime % 60) + ).slice(-2)} + + ), + }} + /> + + )} + {!isCooldown && ( + ( + + ), + }} + /> + )} )}