199 lines
6.7 KiB
TypeScript
199 lines
6.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 { Eye, EyeOff, Lock, RefreshCcw, Shield, UserRound } from "lucide-react";
|
||
import { useQuery } from "@tanstack/react-query";
|
||
import { getCaptcha } from "@/lib/api/Authenticate";
|
||
import { Oval, ThreeDots } from "react-loader-spinner";
|
||
import Image from "next/image";
|
||
import { useState } from "react";
|
||
|
||
const FormSchema = z.object({
|
||
username: z.string().min(2, {
|
||
message: "Username must be at least 2 characters.",
|
||
}),
|
||
password: z.string().min(2, {
|
||
message: "Username must be at least 2 characters.",
|
||
}),
|
||
captcha: z
|
||
.string({ required_error: "لطفا کد امنیتی را وارد نمایید." })
|
||
.length(5, { message: "لطفا کد امنیتی را به طور کامل وارد نمایید." }),
|
||
});
|
||
|
||
export default function LoginForm() {
|
||
const [showPassword, setShowPassword] = useState(false);
|
||
|
||
const {
|
||
data: CaptchaData,
|
||
isLoading,
|
||
refetch,
|
||
} = useQuery({
|
||
queryKey: ["Captcha"],
|
||
queryFn: () => getCaptcha(),
|
||
refetchOnWindowFocus: false,
|
||
});
|
||
|
||
const form = useForm<z.infer<typeof FormSchema>>({
|
||
resolver: zodResolver(FormSchema),
|
||
defaultValues: {
|
||
username: "",
|
||
},
|
||
});
|
||
|
||
function onSubmit(data: z.infer<typeof FormSchema>) {
|
||
console.log("🚀 ~ onSubmit ~ data:", data);
|
||
}
|
||
|
||
console.log(CaptchaData);
|
||
const srcForImage = CaptchaData ? URL.createObjectURL(CaptchaData) : null;
|
||
|
||
return (
|
||
<div className="">
|
||
<Form {...form}>
|
||
<form
|
||
onSubmit={form.handleSubmit(onSubmit)}
|
||
className="w-full space-y-2 mt-2"
|
||
>
|
||
<FormField
|
||
control={form.control}
|
||
name="username"
|
||
render={({ field }) => (
|
||
<FormItem>
|
||
<FormLabel>نام کاربری</FormLabel>
|
||
<FormControl>
|
||
<div className="w-full flex h-10 relative border rounded-lg">
|
||
<div className=" w-12 h-10 rounded-s-lg flex justify-center items-center">
|
||
<UserRound size={23} strokeWidth={1.25} />
|
||
</div>
|
||
<Input
|
||
placeholder="نام کاربری خود را وارد نمایید."
|
||
{...field}
|
||
className=" focus-visible:outline-none focus-visible:ring-0 rounded-e-lg rounded-s-none w-full border-none h-full pr-1"
|
||
/>
|
||
</div>
|
||
</FormControl>
|
||
<FormMessage />
|
||
</FormItem>
|
||
)}
|
||
/>
|
||
|
||
<FormField
|
||
control={form.control}
|
||
name="password"
|
||
render={({ field }) => (
|
||
<FormItem>
|
||
<FormLabel>رمز عبور</FormLabel>
|
||
<FormControl>
|
||
<div className="w-full flex items-center h-10 relative border rounded-lg">
|
||
<div className=" w-12 h-10 rounded-s-lg flex justify-center items-center">
|
||
<Lock size={23} strokeWidth={1.25} />
|
||
</div>
|
||
<Input
|
||
placeholder="رمز عبور خود را وارد نمایید."
|
||
{...field}
|
||
type={showPassword ? "text" : "password"}
|
||
className=" focus-visible:outline-none focus-visible:ring-0 rounded-e-lg rounded-s-none w-full border-none h-full pr-1"
|
||
/>
|
||
<div className=" w-12 h-10 rounded-s-lg flex justify-center items-center">
|
||
{showPassword ? (
|
||
<EyeOff
|
||
onClick={() => setShowPassword(false)}
|
||
size={20}
|
||
className=" cursor-pointer"
|
||
strokeWidth={1.5}
|
||
/>
|
||
) : (
|
||
<Eye
|
||
className=" cursor-pointer"
|
||
onClick={() => setShowPassword(true)}
|
||
size={20}
|
||
strokeWidth={1.5}
|
||
/>
|
||
)}
|
||
</div>
|
||
</div>
|
||
</FormControl>
|
||
<FormMessage />
|
||
</FormItem>
|
||
)}
|
||
/>
|
||
<div className=" flex w-full gap-2 items-end">
|
||
<FormField
|
||
control={form.control}
|
||
name="captcha"
|
||
render={({ field }) => (
|
||
<FormItem className=" w-1/2">
|
||
<FormLabel>کد امنیتی</FormLabel>
|
||
<FormControl>
|
||
<div className="w-full flex h-10 relative border rounded-lg">
|
||
<div className=" w-12 h-10 rounded-s-lg flex justify-center items-center">
|
||
<Shield size={23} strokeWidth={1.25} />
|
||
</div>
|
||
<Input
|
||
placeholder="کد امنیتی"
|
||
{...field}
|
||
className=" focus-visible:outline-none focus-visible:ring-0 rounded-e-lg rounded-s-none w-full border-none h-full pr-1"
|
||
/>
|
||
</div>
|
||
</FormControl>
|
||
<FormMessage />
|
||
</FormItem>
|
||
)}
|
||
/>
|
||
|
||
{isLoading ? (
|
||
<div className="flex justify-between text-center w-1/2">
|
||
<ThreeDots
|
||
visible={true}
|
||
height="50"
|
||
width="50"
|
||
color="#003778"
|
||
radius="9"
|
||
ariaLabel="three-dots-loading"
|
||
wrapperStyle={{}}
|
||
wrapperClass=""
|
||
/>
|
||
</div>
|
||
) : (
|
||
<div className="flex items-center justify-between gap-2 text-center ">
|
||
<Image
|
||
alt="icon"
|
||
src={srcForImage as string}
|
||
className="h-10 rounded-md shadow-md flex-1"
|
||
width={150}
|
||
height={20}
|
||
quality={100}
|
||
/>
|
||
<RefreshCcw
|
||
className="cursor-pointer "
|
||
onClick={() => refetch()}
|
||
size={23}
|
||
color="#003778"
|
||
/>
|
||
</div>
|
||
)}
|
||
</div>
|
||
|
||
<Button className=" w-full !mt-5" type="submit">
|
||
ورود
|
||
</Button>
|
||
</form>
|
||
</Form>
|
||
</div>
|
||
);
|
||
}
|