141 lines
4.2 KiB
TypeScript
141 lines
4.2 KiB
TypeScript
"use client";
|
||
|
||
import React from "react";
|
||
import { zodResolver } from "@hookform/resolvers/zod";
|
||
import { useForm } from "react-hook-form";
|
||
import * as 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 "@/components/ui/use-toast";
|
||
import Image from "next/image";
|
||
import LoginIcon from "../public/img/login-icon.svg";
|
||
import { useMutation } from "@tanstack/react-query";
|
||
import { loginStaff } from "@/utils/authenticateApi";
|
||
|
||
const FormSchema = z.object({
|
||
username: z.string().min(2, {
|
||
message: "Username must be at least 2 characters.",
|
||
}),
|
||
password: z.string().min(4, {
|
||
message: "Password must be at least 4 characters.",
|
||
}),
|
||
});
|
||
|
||
export default function Login() {
|
||
const LoginMutation = useMutation({
|
||
mutationFn: loginStaff,
|
||
onSuccess: (res) => {
|
||
console.log(res);
|
||
// localStorage.setItem("roles", res.roles[0]);
|
||
// setCookie("token", res.token);
|
||
// if (res.roles[0] == "Operator") {
|
||
// router.push("/MainPage/Prescriptions");
|
||
// } else {
|
||
// router.push("/MainPage");
|
||
// }
|
||
},
|
||
onError: (err) => {},
|
||
});
|
||
|
||
const form = useForm<z.infer<typeof FormSchema>>({
|
||
resolver: zodResolver(FormSchema),
|
||
defaultValues: {
|
||
username: "",
|
||
password: "",
|
||
},
|
||
});
|
||
|
||
function onSubmit(data: z.infer<typeof FormSchema>) {
|
||
toast({
|
||
title: "You submitted the following values:",
|
||
description: (
|
||
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4 pt-12">
|
||
<code className="text-white">{JSON.stringify(data, null, 2)}</code>
|
||
</pre>
|
||
),
|
||
});
|
||
console.log(data);
|
||
}
|
||
|
||
return (
|
||
<div
|
||
className="w-full mt-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>
|
||
|
||
<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>
|
||
);
|
||
}
|