From 7e7725c8ff1d8022a862508616554248c1c2f28f Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Wed, 29 Nov 2023 16:47:38 +0100 Subject: [PATCH] :sparkles: Send another code working --- src/front/Api/Auth/Customer/Auth.ts | 10 ++++ .../StepTotp/classes.module.scss | 20 ++++++++ .../Layouts/LoginCustomer/StepTotp/index.tsx | 47 +++++++++++++++++-- .../Layouts/LoginCustomer/index.tsx | 15 +++++- 4 files changed, 87 insertions(+), 5 deletions(-) diff --git a/src/front/Api/Auth/Customer/Auth.ts b/src/front/Api/Auth/Customer/Auth.ts index 237df5c3..906d939d 100644 --- a/src/front/Api/Auth/Customer/Auth.ts +++ b/src/front/Api/Auth/Customer/Auth.ts @@ -94,4 +94,14 @@ export default class Auth extends BaseApiService { return Promise.reject(err); } } + + public async sendAnotherCode(body: IMailVerifyParams): Promise { + const url = new URL(this.baseURl.concat("/send-another-code")); + try { + return this.postRequest(url, body); + } catch (err) { + this.onError(err); + return Promise.reject(err); + } + } } diff --git a/src/front/Components/Layouts/LoginCustomer/StepTotp/classes.module.scss b/src/front/Components/Layouts/LoginCustomer/StepTotp/classes.module.scss index efbcd6ad..fae541af 100644 --- a/src/front/Components/Layouts/LoginCustomer/StepTotp/classes.module.scss +++ b/src/front/Components/Layouts/LoginCustomer/StepTotp/classes.module.scss @@ -21,4 +21,24 @@ margin-top: 32px; } } + + .ask-another-code { + margin-top: 48px; + display: flex; + flex-direction: column; + gap: 16px; + align-items: flex-start; + + .new-code-button { + &[data-disabled="true"] { + opacity: 0.5; + cursor: not-allowed; + } + } + .new-code-timer { + display: flex; + gap: 6px; + align-items: center; + } + } } diff --git a/src/front/Components/Layouts/LoginCustomer/StepTotp/index.tsx b/src/front/Components/Layouts/LoginCustomer/StepTotp/index.tsx index c71044ee..443c2369 100644 --- a/src/front/Components/Layouts/LoginCustomer/StepTotp/index.tsx +++ b/src/front/Components/Layouts/LoginCustomer/StepTotp/index.tsx @@ -1,6 +1,6 @@ -import React from "react"; +import React, { useEffect } from "react"; import classes from "./classes.module.scss"; -import Typography, { ITypo } from "@Front/Components/DesignSystem/Typography"; +import Typography, { ITypo, ITypoColor } from "@Front/Components/DesignSystem/Typography"; import Form from "@Front/Components/DesignSystem/Form"; import TextField from "@Front/Components/DesignSystem/Form/TextField"; import Button, { EButtonVariant } from "@Front/Components/DesignSystem/Button"; @@ -9,15 +9,37 @@ type IProps = { onSubmit: (e: React.FormEvent | null, values: { [key: string]: string }) => void; validationErrors: ValidationError[]; partialPhoneNumber: string; + onSendAnotherCode: () => void; }; export default function StepTotp(props: IProps) { - const { onSubmit, validationErrors, partialPhoneNumber } = props; + const { onSubmit, validationErrors, partialPhoneNumber, onSendAnotherCode } = props; + const [disableNewCodeButton, setDisableNewCodeButton] = React.useState(true); + const [secondsBeforeNewCode, setSecondsBeforeNewCode] = React.useState(30); + + useEffect(() => { + const interval = setInterval(() => { + if (secondsBeforeNewCode > 0) { + setSecondsBeforeNewCode(secondsBeforeNewCode - 1); + } else { + setDisableNewCodeButton(false); + } + }, 1000); + return () => clearInterval(interval); + }, [secondsBeforeNewCode]); + + const sendAnotherCode = () => { + onSendAnotherCode(); + setDisableNewCodeButton(true); + setSecondsBeforeNewCode(30); + }; return (
-
Votre code a été envoyé par SMS au ** ** ** {partialPhoneNumber}
+
+ Votre code a été envoyé par SMS au ** ** ** {partialPhoneNumber.replace(/(.{2})/g, "$1 ")} +
+
+ Vous n'avez rien reçu ? + + + Redemandez un code dans + + 00:{secondsBeforeNewCode < 10 ? `0${secondsBeforeNewCode}` : secondsBeforeNewCode} + + +
); } diff --git a/src/front/Components/Layouts/LoginCustomer/index.tsx b/src/front/Components/Layouts/LoginCustomer/index.tsx index 0030af15..c0a48bcc 100644 --- a/src/front/Components/Layouts/LoginCustomer/index.tsx +++ b/src/front/Components/Layouts/LoginCustomer/index.tsx @@ -177,12 +177,25 @@ export default function Login() { } }, [email]); + const onSendAnotherCode = useCallback(async () => { + try { + await Auth.getInstance().sendAnotherCode({ email }); + } catch (error: any) { + return; + } + }, [email]); + return (
{step === LoginStep.EMAIL && } {step === LoginStep.TOTP && ( - + )} {step === LoginStep.PASSWORD && (