Sootzani/components/Verify.tsx

192 lines
6.5 KiB
TypeScript
Raw 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 } from "react-loader-spinner";
import useToken from "@/store/useToken";
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 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) => {},
// });
const CitizenVerifyMutation = useMutation({
mutationFn: verifyCitizen,
onSuccess: (res) => {
saveToken(true);
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);
const justLogin = localStorage.getItem("Sootzani_Login_State");
if (justLogin) {
router.push("/");
localStorage.removeItem("Sootzani_Login_State");
} else {
router.push("/RequestStep1");
}
},
onError: (err: any) => {},
});
const handelVerify = () => {
console.log(otp);
const payload = { ...userData, verificationCode: otp };
console.log(userData);
CitizenVerifyMutation.mutate(payload);
};
useEffect(() => {
useData.persist.rehydrate();
}, []);
return (
<div
className="w-full my-12 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-full max-md:p-8 pt-12"
>
<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}
inputMode="decimal"
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={CitizenVerifyMutation.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"
>
{CitizenVerifyMutation.isPending ? (
<div className="flex justify-center items-center gap-2">
<Oval
visible={true}
height="20"
width="20"
color="#fff"
ariaLabel="oval-loading"
wrapperStyle={{}}
wrapperClass=""
/>
{/* <p>در حال انجام ...</p> */}
</div>
) : (
"تایید"
)}
</Button>
</div>
{/* <Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="w-full flex flex-col "
>
<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 gap-2 w-full mt-14">
<Button
type="submit"
className="bg-blue text-white w-1/2 md:w-1/3 p-2 rounded-xl"
>
ورود
</Button>
</div>
</form>
</Form> */}
</div>
</div>
);
}