Sootzani/components/Verify.tsx

196 lines
6.6 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"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, Triangle } from "react-loader-spinner";
import useToken from "@/store/useToken";
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 saveToken = useToken((state) => state.saveToken);
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]);
return (
<div
className="w-full sm:w-2/3 md:w-2/4 xl:w-1/3 bg-white rounded-lg dark:border xl:p-0 dark:bg-gray-800 dark:border-gray-700 shadow-[0px_0px_15px_0px_#00000040]
h-3/5 sm:h-3/4 max-md:p-8 relative z-10 bg-[url('/img/yazd1.png')] bg-no-repeat bg-bottom bg-contain"
>
{CitizenVerifyMutation.isPending && (
<div className="absolute top-3 left-5 ">
<Triangle
visible={true}
height="40"
width="40"
color="#003778"
ariaLabel="triangle-loading"
wrapperStyle={{}}
wrapperClass=""
/>
</div>
)}
<div className="flex flex-col justify-start h-full space-y-3 md:space-y-6 md:py-16 md:px-20">
<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="text-center label">
<span className="text-xs text-center text-blue/70 label-text">
کد دریافت شده توسط پیامک را وارد نمایید.
</span>
</label>
{/* <div className="flex justify-end w-full gap-2 mt-14">
<Button
disabled={CitizenVerifyMutation.isPending}
onClick={handelVerify}
type="submit"
className="w-1/2 p-2 text-white bg-blue hover:bg-blue/90 md:w-1/3 rounded-xl"
>
{CitizenVerifyMutation.isPending ? (
<div className="flex items-center justify-center gap-2">
<Oval
visible={true}
height="20"
width="20"
color="#fff"
ariaLabel="oval-loading"
wrapperStyle={{}}
wrapperClass=""
/>
</div>
) : (
"تایید"
)}
</Button>
</div> */}
{/* <Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="flex flex-col w-full "
>
<FormField
control={form.control}
name="username"
render={({ field }) => (
<FormItem>
<FormLabel className="text-[#7C838A] text-sm font-medium">
نام کاربری
</FormLabel>
<FormControl>
<Input
className="mb-4"
placeholder="نام کاربری خود را وارد نمایید"
{...field}
/>
</FormControl>
<FormDescription>
This is your public display name.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem>
<FormLabel className="text-[#7C838A] text-sm font-medium mt-7">
رمز عبور
</FormLabel>
<FormControl>
<Input placeholder="رمز عبور خود را وارد کنید" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<div className="flex justify-end w-full gap-2 mt-14">
<Button
type="submit"
className="w-1/2 p-2 text-white bg-blue md:w-1/3 rounded-xl"
>
ورود
</Button>
</div>
</form>
</Form> */}
</div>
</div>
);
}