137 lines
3.9 KiB
TypeScript
137 lines
3.9 KiB
TypeScript
"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";
|
||
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("");
|
||
const space = "\xa0";
|
||
const router = useRouter();
|
||
const userData = useData((state) => state.data);
|
||
const [VerifyRole, setVerifyRole] = useState<any>("");
|
||
const saveToken = useToken((state) => state.saveToken);
|
||
const [isShow, setIsShow] = useState(true);
|
||
|
||
|
||
const CitizenVerifyMutation = useMutation({
|
||
mutationFn: verifyCitizen,
|
||
onSuccess: (res) => {
|
||
saveToken(true);
|
||
const decodedToken = jwtDecode<any>(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 && (
|
||
<div className="absolute top-3 left-5 ">
|
||
<Triangle
|
||
visible={true}
|
||
height="40"
|
||
width="40"
|
||
color="#003778"
|
||
ariaLabel="triangle-loading"
|
||
wrapperStyle={{}}
|
||
wrapperClass=""
|
||
/>
|
||
</div>
|
||
)}
|
||
|
||
<p className="text-lg color-black text-center font-bold">
|
||
کد تایید ارسالی به تلفن خود را وارد نمایید.
|
||
</p>
|
||
|
||
<div className="w-full mt-5" dir="ltr">
|
||
<OtpInput
|
||
value={otp}
|
||
onChange={setOtp}
|
||
numInputs={6}
|
||
renderSeparator={<span>{space}</span>}
|
||
renderInput={(props, idx) => (
|
||
<input autoFocus={idx == 0} pattern="[0-9]*" {...props} />
|
||
)}
|
||
containerStyle=" flex justify-center gap-1"
|
||
inputStyle=" !w-12 rounded-full h-12 bg-[#F5F5F5] border border-blue/30 text-black"
|
||
/>
|
||
</div>
|
||
|
||
<div className="mx-auto w-full text-center text-xl mt-6">
|
||
{" "}
|
||
<CountdownTimer
|
||
setIsShow={(state : any) => setIsShow(state)}
|
||
isShow={isShow}
|
||
/>
|
||
|
||
{!isShow && (
|
||
<p
|
||
onClick={handelResendOtp}
|
||
className=" text-blue text-sm font-semibold cursor-pointer"
|
||
>
|
||
ارسال مجدد کد تایید
|
||
</p>
|
||
)}
|
||
</div>
|
||
</>
|
||
);
|
||
};
|