142 lines
4.8 KiB
TypeScript
142 lines
4.8 KiB
TypeScript
"use client";
|
||
import Image from "next/image";
|
||
import React, { useEffect, useState } from "react";
|
||
import LoginIcon from "../public/img/login-icon.svg";
|
||
import OtpInput from "react-otp-input";
|
||
import { Button } from "./ui/button";
|
||
import { verifyCitizen, verifyStaff } from "@/utils/authenticateApi";
|
||
import { useMutation } from "@tanstack/react-query";
|
||
import { useRouter } from "next/navigation";
|
||
import useData from "@/store/useData";
|
||
import { JwtPayload, jwtDecode } from "jwt-decode";
|
||
import { setCookie } from "cookies-next";
|
||
import { Oval } from "react-loader-spinner";
|
||
import { toast } from "sonner";
|
||
|
||
export default function Verify() {
|
||
const [otp, setOtp] = useState("");
|
||
const space = "\xa0";
|
||
const router = useRouter();
|
||
const userData = useData((state) => state.data);
|
||
const [VerifyRole, setVerifyRole] = useState<any>("");
|
||
const verifyMutation = useMutation({
|
||
mutationFn: verifyStaff,
|
||
onSuccess: (res) => {
|
||
const decodedToken = jwtDecode<any>(res);
|
||
setCookie("Sootzani_Token", res);
|
||
const userRole =
|
||
decodedToken[
|
||
"http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
|
||
];
|
||
localStorage.setItem("Sootzani_Role", userRole);
|
||
if (userRole == "Inspector") {
|
||
router.push("/UserPanel/ControllerDashboard");
|
||
} else if (userRole == "Admin") {
|
||
router.push("/UserPanel/AdminDashboard");
|
||
} else {
|
||
router.push("/UserPanel/CitizenDashboard");
|
||
}
|
||
},
|
||
onError: (err: any) => {
|
||
toast("کد تایید شما صحیح نمیباشد.", {
|
||
className: "!bg-red-500 !text-white",
|
||
});
|
||
},
|
||
});
|
||
const CitizenVerifyMutation = useMutation({
|
||
mutationFn: verifyCitizen,
|
||
onSuccess: (res) => {
|
||
const decodedToken = jwtDecode<any>(res);
|
||
setCookie("Sootzani_Token", res);
|
||
const userRole =
|
||
decodedToken[
|
||
"http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
|
||
];
|
||
localStorage.setItem("Sootzani_Role", userRole);
|
||
router.push("/UserPanel/CitizenDashboard");
|
||
},
|
||
onError: (err: any) => {},
|
||
});
|
||
|
||
const handelVerify = () => {
|
||
console.log(otp);
|
||
const payload = { ...userData, verificationCode: otp };
|
||
console.log(userData);
|
||
if (userData?.phoneNumber) {
|
||
CitizenVerifyMutation.mutate(payload);
|
||
} else {
|
||
verifyMutation.mutate(payload);
|
||
}
|
||
};
|
||
|
||
useEffect(() => {
|
||
useData.persist.rehydrate();
|
||
}, []);
|
||
|
||
useEffect(() => {
|
||
if (otp.length == 6) {
|
||
const payload = { ...userData, verificationCode: otp };
|
||
console.log(userData);
|
||
verifyMutation.mutate(payload);
|
||
}
|
||
}, [otp]);
|
||
|
||
return (
|
||
<div className=" w-screen h-screen grid place-content-center bg-gray-200">
|
||
<div
|
||
className="w-full sm:[20rem] md:[20rem] xl:w-[30rem] bg-white rounded-lg dark:border xl:p-0 dark:bg-gray-800 dark:border-gray-700 shadow-[0px_0px_15px_0px_#00000040]
|
||
h-fit"
|
||
>
|
||
<div className="space-y-3 md:space-y-4 flex flex-col md:py-12 md:px-20 h-full justify-center">
|
||
<Image alt="icon" src={LoginIcon} className="self-center w-1/3 " />
|
||
<p className="text-lg font-bold leading-tight tracking-tight text-blue dark:text-white">
|
||
تایید کد
|
||
</p>
|
||
<div className="w-full " 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"
|
||
inputStyle=" !w-10 rounded-md h-10 bg-[#F5F5F5] border border-blue/30 text-black"
|
||
/>
|
||
</div>
|
||
<label className="label text-center">
|
||
<span className="text-xs text-blue/70 label-text text-center">
|
||
کد دریافت شده توسط پیامک را وارد نمایید.
|
||
</span>
|
||
</label>
|
||
{/* <div className="flex justify-end gap-2 w-full mt-14">
|
||
<Button
|
||
disabled={verifyMutation.isPending}
|
||
onClick={handelVerify}
|
||
type="submit"
|
||
className="bg-blue hover:bg-blue/90 text-white w-1/2 md:w-1/3 p-2 rounded-xl"
|
||
>
|
||
{verifyMutation.isPending ? (
|
||
<div className="flex justify-center items-center gap-2">
|
||
<Oval
|
||
visible={true}
|
||
height="20"
|
||
width="20"
|
||
color="#fff"
|
||
ariaLabel="oval-loading"
|
||
wrapperStyle={{}}
|
||
wrapperClass=""
|
||
/>
|
||
</div>
|
||
) : (
|
||
"تایید"
|
||
)}
|
||
</Button>
|
||
</div> */}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|