207 lines
5.7 KiB
TypeScript
207 lines
5.7 KiB
TypeScript
"use client";
|
||
|
||
import { zodResolver } from "@hookform/resolvers/zod";
|
||
import { useForm } from "react-hook-form";
|
||
import { z } from "zod";
|
||
import { Button } from "@/components/ui/button";
|
||
import {
|
||
Form,
|
||
FormControl,
|
||
FormDescription,
|
||
FormField,
|
||
FormItem,
|
||
FormLabel,
|
||
FormMessage,
|
||
} from "@/components/ui/form";
|
||
import { Input } from "@/components/ui/input";
|
||
import { toast } from "sonner";
|
||
import Image from "next/image";
|
||
import { RefreshCcw } from "lucide-react";
|
||
import { Oval, ThreeDots } from "react-loader-spinner";
|
||
import img1 from "@/assets/images/direct.png";
|
||
import Link from "next/link";
|
||
import { getCaptcha } from "@/utils/commonApi";
|
||
import { useRouter } from "next/navigation";
|
||
import useData from "@/store/useData";
|
||
import { useMutation, useQuery } from "@tanstack/react-query";
|
||
import { logisterCitizen } from "@/utils/authenticateApi";
|
||
|
||
const formSchema = z.object({
|
||
phoneNumber: z.string({ required_error: "*فیلد اجباری*" }).max(11, {
|
||
message: "حداکثر تعداد کاراکتر 11 تا میباشد.",
|
||
}),
|
||
|
||
captcha: z
|
||
.string({ required_error: "*فیلد اجباری*" })
|
||
.length(5, { message: "لطفا کد امنیتی را به طور کامل وارد نمایید." }),
|
||
});
|
||
|
||
function LoginForm() {
|
||
const router = useRouter();
|
||
const saveData = useData((state) => state.saveData);
|
||
|
||
const {
|
||
data: CaptchaData,
|
||
isLoading,
|
||
refetch,
|
||
} = useQuery({
|
||
queryKey: ["Captcha"],
|
||
queryFn: () => getCaptcha(),
|
||
refetchOnWindowFocus: false,
|
||
});
|
||
|
||
const loginMutation = useMutation({
|
||
mutationFn: logisterCitizen,
|
||
onSuccess: (res: any | string) => {
|
||
router.push("/Verify");
|
||
localStorage.setItem("VerifyRole", "citizen");
|
||
localStorage.setItem("verificationToken", res);
|
||
},
|
||
onError: (err: any | string) => {
|
||
refetch();
|
||
toast.error(err.response.data.detail);
|
||
|
||
form.reset({
|
||
phoneNumber:"",
|
||
captcha: "",
|
||
});
|
||
|
||
console.log(err);
|
||
console.log(err.message);
|
||
console.log(err.detail);
|
||
},
|
||
});
|
||
|
||
// 1. Define your form.
|
||
const form = useForm<z.infer<typeof formSchema>>({
|
||
resolver: zodResolver(formSchema),
|
||
});
|
||
|
||
// 2. Define a submit handler.
|
||
function onSubmit(data: z.infer<typeof formSchema>) {
|
||
const payload = {
|
||
phoneNumber: data.phoneNumber,
|
||
captcha: {
|
||
key: CaptchaData?.headers["captcha-key"],
|
||
value: data.captcha,
|
||
},
|
||
};
|
||
loginMutation.mutateAsync(payload);
|
||
saveData({
|
||
phoneNumber: data.phoneNumber,
|
||
});
|
||
|
||
console.log(data);
|
||
}
|
||
|
||
const srcForImage = CaptchaData
|
||
? URL.createObjectURL(CaptchaData.data)
|
||
: null;
|
||
|
||
return (
|
||
<>
|
||
<Form {...form}>
|
||
<form onSubmit={form.handleSubmit(onSubmit)} className="w-1/2">
|
||
{/* Phone Number */}
|
||
<FormField
|
||
control={form.control}
|
||
name="phoneNumber"
|
||
render={({ field }) => (
|
||
<FormItem className="space-y-2">
|
||
<FormLabel >تلفن همراه</FormLabel>
|
||
<FormControl>
|
||
<Input
|
||
placeholder="09"
|
||
{...field}
|
||
className=" w-full"
|
||
/>
|
||
</FormControl>
|
||
<FormMessage />
|
||
</FormItem>
|
||
)}
|
||
/>
|
||
|
||
{/* Capcha */}
|
||
<div className="flex items-start justify-center h-20 mt-8">
|
||
<FormField
|
||
control={form.control}
|
||
name="captcha"
|
||
render={({ field }) => (
|
||
<FormItem className="space-y-2">
|
||
<FormControl>
|
||
<Input
|
||
placeholder="عبارت امنیتی"
|
||
{...field}
|
||
className="w-full h-10 m-0"
|
||
/>
|
||
</FormControl>
|
||
<FormMessage />
|
||
</FormItem>
|
||
)}
|
||
/>
|
||
|
||
{isLoading ? (
|
||
<div className="flex justify-center text-center w-36">
|
||
<ThreeDots
|
||
visible={true}
|
||
height="50"
|
||
width="50"
|
||
color="#003778"
|
||
radius="9"
|
||
ariaLabel="three-dots-loading"
|
||
wrapperStyle={{}}
|
||
wrapperClass=""
|
||
/>
|
||
</div>
|
||
) : (
|
||
<div className="flex justify-center items-center gap-2 text-center ">
|
||
<Image
|
||
alt="icon"
|
||
src={srcForImage as string}
|
||
// src={img1}
|
||
className="h-10 rounded-sm shadow-md"
|
||
width={150}
|
||
height={20}
|
||
quality={100}
|
||
/>
|
||
<RefreshCcw
|
||
className="cursor-pointer "
|
||
onClick={() => refetch()}
|
||
color="#fcc52d"
|
||
/>
|
||
</div>
|
||
)}
|
||
</div>
|
||
|
||
<Button
|
||
type="submit"
|
||
className="w-full p-2 text-black transition bg-yellow hover:bg-yellow rounded-xl"
|
||
>
|
||
{loginMutation.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>
|
||
</form>
|
||
</Form>
|
||
|
||
<Link href={"#"} className="mt-6 underline text-[16px]">
|
||
ورود با یزد من{" "}
|
||
</Link>
|
||
</>
|
||
);
|
||
}
|
||
|
||
export default LoginForm;
|