From 9b6264fc2d990bf908188b94ea643891744bd7ec Mon Sep 17 00:00:00 2001 From: YasamanKahdooei Date: Sat, 13 Apr 2024 15:17:57 +0330 Subject: [PATCH] Add Otp Input --- components/SignIn/LoginForm.tsx | 6 +- components/Verify/CountDown.jsx | 74 ++++++++++++++++++ components/Verify/Verify.tsx | 2 - components/Verify/VerifyInput.tsx | 124 ++++++++++++++++++++++++++++-- components/ui/form.tsx | 4 +- package-lock.json | 26 ++++++- package.json | 2 + 7 files changed, 220 insertions(+), 18 deletions(-) create mode 100644 components/Verify/CountDown.jsx diff --git a/components/SignIn/LoginForm.tsx b/components/SignIn/LoginForm.tsx index 41d4d67..4a51a9a 100644 --- a/components/SignIn/LoginForm.tsx +++ b/components/SignIn/LoginForm.tsx @@ -108,7 +108,7 @@ function LoginForm() { name="phoneNumber" render={({ field }) => ( - تلفن همراه + تلفن همراه - + )} /> @@ -135,7 +135,7 @@ function LoginForm() { className="w-full h-10 m-0" /> - + )} /> diff --git a/components/Verify/CountDown.jsx b/components/Verify/CountDown.jsx new file mode 100644 index 0000000..b588c75 --- /dev/null +++ b/components/Verify/CountDown.jsx @@ -0,0 +1,74 @@ +import React, { useState, useEffect } from "react"; +import Countdown from "react-countdown"; +// import { CN } from "../../utils/functions"; + +const CountdownTimer = ({ isShow, setIsShow }) => { + const [remainingTime, setRemainingTime] = useState(2 * 60000); + const [key, setKey] = useState(0); + + useEffect(() => { + // Retrieve remaining time from local storage on component mount + const storedTime = localStorage.getItem("countdownTime"); + if (storedTime && storedTime != NaN) { + setRemainingTime(parseInt(storedTime)); + } + // else { + // setRemainingTime(2 * 60000); + // } + if (localStorage.getItem("CountDownCompleted")) { + setIsShow(false); + } + }, []); + + const handleComplete = () => { + // Clear remaining time in local storage when countdown completes + localStorage.removeItem("countdownTime"); + localStorage.setItem("CountDownCompleted", 1); + console.log("ccccc"); + setIsShow(false); + }; + + useEffect(() => { + if (localStorage.getItem("CountDownCompleted")) { + setIsShow(false); + } else { + setIsShow(true); + } + }, []); + const RestartTimer = () => { + setKey(!key); + }; + const handleTick = (time) => { + // Store remaining time in local storage on each tick + localStorage.setItem("countdownTime", time.total); + }; + const renderer = ({ hours, minutes, seconds, completed }) => { + return ( + + {minutes}:{seconds} + + ); + }; + useEffect(() => { + if (!isShow) { + setRemainingTime(2 * 60000); + const newKey = Math.random(); + setKey(newKey); + } + }, [isShow]); + return ( +
+ {/* {!localStorage.getItem("CountDownCompleted") && ( */} + + {/* )} */} +
+ ); +}; + +export default CountdownTimer; diff --git a/components/Verify/Verify.tsx b/components/Verify/Verify.tsx index cc77bc4..ca765c7 100644 --- a/components/Verify/Verify.tsx +++ b/components/Verify/Verify.tsx @@ -7,8 +7,6 @@ import useData from "@/store/useData"; const Verify = () => { const data = useData((state) => state.data); - - console.log(data); return ( diff --git a/components/Verify/VerifyInput.tsx b/components/Verify/VerifyInput.tsx index 643a027..2848ad9 100644 --- a/components/Verify/VerifyInput.tsx +++ b/components/Verify/VerifyInput.tsx @@ -1,5 +1,5 @@ -"use client" -import React, { useState ,useEffect } from 'react' +"use client"; +import React, { useState, useEffect } from "react"; import OtpInput from "react-otp-input"; import { useMutation } from "@tanstack/react-query"; import { useRouter } from "next/navigation"; @@ -7,8 +7,10 @@ import useData from "@/store/useData"; import { setCookie } from "cookies-next"; import { Oval, Triangle } from "react-loader-spinner"; import useToken from "@/store/useToken"; +import { JwtPayload, jwtDecode } from "jwt-decode"; import { toast } from "sonner"; - +import { verifyCitizen } from "@/utils/authenticateApi"; +import CountdownTimer from "./CountDown"; export const VerifyInput = () => { const [otp, setOtp] = useState(""); @@ -17,12 +19,118 @@ export const VerifyInput = () => { const userData = useData((state) => state.data); const [VerifyRole, setVerifyRole] = useState(""); const saveToken = useToken((state) => state.saveToken); + const [isShow, setIsShow] = useState(true); + + + const CitizenVerifyMutation = useMutation({ + mutationFn: verifyCitizen, + onSuccess: (res) => { + saveToken(true); + const decodedToken = jwtDecode(res.jwtToken); + setCookie("Sootzani_Token", res.jwtToken); + setCookie("Sootzani_Refresh_Token", res.refreshToken); + const userRole = + decodedToken[ + "http://schemas.microsoft.com/ws/2008/06/identity/claims/role" + ]; + localStorage.setItem("Sootzani_Role", userRole); + const justLogin = localStorage.getItem("Sootzani_Login_State"); + if (justLogin) { + router.push("/Account/Complaints"); + localStorage.removeItem("Sootzani_Login_State"); + } else { + router.push("/RequestStep1"); + } + }, + onError: (err: any | string) => { + toast(<>{err.response.data.detail}, { + className: "!bg-red-500 !text-white", + }); + }, + }); + + const handelVerify = () => { + const payload = { + otpToken: localStorage.getItem("verificationToken"), + verificationCode: otp, + }; + CitizenVerifyMutation.mutate(payload); + }; + + // useEffect(() => { + // useData.persist.rehydrate(); + // }, []); + + useEffect(() => { + if (otp.length == 6) { + const payload = { + otpToken: localStorage.getItem("verificationToken"), + verificationCode: otp, + }; + CitizenVerifyMutation.mutate(payload); + } + }, [otp]); + + // SH.... + + + const handelResendOtp = () => { + console.log(11); + // resendOtpMutation.mutate({ + // otpToken: localStorage.getItem(appConstants.SH_CT_OTP_TOKEN), + // }); + }; return ( <> -

- کد تایید ارسالی به تلفن خود را وارد نمایید. -

+ {CitizenVerifyMutation.isPending && ( +
+ +
+ )} + +

+ کد تایید ارسالی به تلفن خود را وارد نمایید. +

+ +
+ {space}} + renderInput={(props, idx) => ( + + )} + containerStyle=" flex justify-center gap-1" + inputStyle=" !w-12 rounded-full h-12 bg-[#F5F5F5] border border-blue/30 text-black" + /> +
+ +
+ {" "} + setIsShow(state)} + isShow={isShow} + /> + + {!isShow && ( +

+ ارسال مجدد کد تایید +

+ )} +
- ) -} + ); +}; diff --git a/components/ui/form.tsx b/components/ui/form.tsx index 5c2e2db..b6d044c 100644 --- a/components/ui/form.tsx +++ b/components/ui/form.tsx @@ -93,7 +93,7 @@ const FormLabel = React.forwardRef< return (