232 lines
7.2 KiB
TypeScript
232 lines
7.2 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";
|
||
import { resendOtp } from "@/utils/commonApi";
|
||
import { appConstants } from "../../utils/variables";
|
||
|
||
export const VerifyInput = () => {
|
||
const [otp, setOtp] = useState("");
|
||
const space = "\xa0";
|
||
const router = useRouter();
|
||
const saveToken = useToken((state) => state.saveToken);
|
||
const [isShow, setIsShow] = useState(true);
|
||
const userData = useData((state) => state.data);
|
||
const [VerifyRole, setVerifyRole] = useState<any>("");
|
||
|
||
const CitizenVerifyMutation = useMutation({
|
||
mutationFn: verifyCitizen,
|
||
onSuccess: (res) => {
|
||
saveToken(true);
|
||
const decodedToken = jwtDecode<any>(res.data.jwtToken);
|
||
setCookie("Shahrbin_Token", res.data.jwtToken);
|
||
setCookie("Shahrbin_Refresh_Token", res.data.refreshToken);
|
||
const userRole =
|
||
decodedToken[
|
||
"http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
|
||
];
|
||
localStorage.setItem("Shahrbin_Role", userRole);
|
||
const justLogin = localStorage.getItem("Shahrbin_Login_State");
|
||
|
||
if (justLogin) {
|
||
router.push("/User/RecentRequest");
|
||
localStorage.removeItem("Shahrbin_Login_State");
|
||
console.log("justLogin");
|
||
} else {
|
||
console.log("Dont justLogin");
|
||
router.push("/User/RecentRequest");
|
||
}
|
||
},
|
||
onError: (err: any | string) => {
|
||
console.log("Dont Login");
|
||
|
||
toast(<>{err.response.data.detail}</>, {
|
||
className: "!bg-rose-800 !text-white flex justify-end ",
|
||
});
|
||
},
|
||
});
|
||
|
||
useEffect(() => {
|
||
useData.persist.rehydrate();
|
||
}, []);
|
||
|
||
// SH....
|
||
const resendOtpMutation = useMutation({
|
||
mutationKey: ["resendOtp"],
|
||
mutationFn: resendOtp,
|
||
onSuccess: (res: any) => {},
|
||
onError: (err: any) => {
|
||
if (err.response.status === 428) {
|
||
console.log(err.response);
|
||
localStorage.setItem(
|
||
appConstants.SH_CT_OTP_TOKEN,
|
||
err.response.data.data
|
||
);
|
||
// toast("کد تایید مجدد ارسال شد.", { type: "info" });
|
||
toast("کد تایید مجدد ارسال شد.", {
|
||
className: "!bg-yellow !text-black",
|
||
});
|
||
|
||
localStorage.removeItem("countdownTime");
|
||
localStorage.removeItem("CountDownCompleted");
|
||
setIsShow(true);
|
||
} else {
|
||
// toast(err.response.data.message, { type: "error" });
|
||
toast(<>{err.response.data.message}</>, {
|
||
className: "!bg-red-500 !text-white",
|
||
});
|
||
}
|
||
},
|
||
});
|
||
|
||
const handelResendOtp = () => {
|
||
console.log(11);
|
||
resendOtpMutation.mutate({
|
||
otpToken: localStorage.getItem(appConstants.SH_CT_OTP_TOKEN),
|
||
});
|
||
};
|
||
|
||
//Handle Verify
|
||
useEffect(() => {
|
||
console.log(otp);
|
||
if (otp.length == 6) {
|
||
console.log("Lenght 6");
|
||
if (localStorage.getItem("CountDownCompleted")) {
|
||
const otpTokenString = localStorage.getItem(
|
||
appConstants.SH_CT_OTP_TOKEN
|
||
);
|
||
// if (otpTokenString !== null) {
|
||
// resendOtpMutation.mutate({
|
||
// otpToken: JSON.parse(otpTokenString),
|
||
// });
|
||
// }
|
||
|
||
if (otpTokenString !== null) {
|
||
try {
|
||
const otpToken = JSON.parse(otpTokenString);
|
||
resendOtpMutation.mutate({
|
||
otpToken: otpToken,
|
||
});
|
||
} catch (error) {
|
||
console.error("Error parsing otpTokenString:", error);
|
||
// Handle the error accordingly, e.g., notify the user or handle it gracefully.
|
||
}
|
||
}
|
||
} else {
|
||
console.log(2);
|
||
// makeRequest();
|
||
}
|
||
|
||
const payload = {
|
||
// otpToken: localStorage.getItem("verificationToken"),
|
||
otpToken: localStorage.getItem(appConstants.SH_CT_OTP_TOKEN),
|
||
verificationCode: otp,
|
||
};
|
||
CitizenVerifyMutation.mutate(payload);
|
||
}
|
||
}, [otp]);
|
||
|
||
// Soot
|
||
// const handelVerify = () => {
|
||
// const payload = {
|
||
// // otpToken: localStorage.getItem("verificationToken"),
|
||
// otpToken: localStorage.getItem(appConstants.SH_CT_OTP_TOKEN),
|
||
// verificationCode: otp,
|
||
// };
|
||
// CitizenVerifyMutation.mutate(payload);
|
||
// };
|
||
|
||
// Sha
|
||
// const handleVerify = async (e) => {
|
||
// const payload = {
|
||
// verificationCode: otp,
|
||
// otpToken: localStorage.getItem(appConstants.SH_CT_OTP_TOKEN),
|
||
// };
|
||
// try {
|
||
// const { success, data } = await api.Authenticate({
|
||
// // tail: state.type === "signin" ? "Verify" : "RequestToken",
|
||
// tail: " ",
|
||
// payload,
|
||
// method: httpMethods.post,
|
||
// });
|
||
// if (success) {
|
||
// // if (state.type === "signin") onSignInSuccess(data);
|
||
// // else
|
||
// // navigate(appRoutes.resetpass, {
|
||
// // state: { token: data, userName: state.phoneNumber },
|
||
// // });
|
||
// console.log(data);
|
||
// onSignInSuccess(data);
|
||
// if (localStorage.getItem(appConstants.SH_CT_LOGIN_URL)) {
|
||
// navigate(localStorage.getItem(appConstants.SH_CT_LOGIN_URL));
|
||
// localStorage.removeItem(appConstants.SH_CT_LOGIN_URL);
|
||
// } else {
|
||
// navigate(appRoutes.menu);
|
||
// }
|
||
// }
|
||
// } catch (err) {}
|
||
// };
|
||
|
||
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-sm md: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-10 h-10 md:!w-12 md:h-12 rounded-full bg-[#F5F5F5] border border-blue/30 text-black"
|
||
/>
|
||
</div>
|
||
|
||
{/* <input type="text" className="w-full"/> */}
|
||
<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-xs md:text-sm font-semibold cursor-pointer"
|
||
>
|
||
ارسال مجدد کد تایید
|
||
</p>
|
||
)}
|
||
</div>
|
||
</>
|
||
);
|
||
};
|