160 lines
5.0 KiB
TypeScript
160 lines
5.0 KiB
TypeScript
"use client";
|
||
import React, { useEffect } 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 Link from "next/link";
|
||
import RequestStep2 from "./RequestStep2";
|
||
import { useMutation, useQuery } from "@tanstack/react-query";
|
||
import { editProfile, getProfile } from "@/utils/authenticateApi";
|
||
import { useRouter } from "next/navigation";
|
||
import { toast } from "sonner";
|
||
|
||
const FormSchema = z.object({
|
||
firstName: z.string().min(1, {
|
||
message: "لطفا نام خود را وارد نمایید.",
|
||
}),
|
||
lastName: z.string().min(1, {
|
||
message: "لطفا نام خانوادگی خود را وارد نمایید.",
|
||
}),
|
||
// nationalId: z
|
||
// .string()
|
||
// .length(10, { message: "کد ملی معتبر نمی باشد." })
|
||
// .transform((value) => {
|
||
// const parsedNumber = Number(value);
|
||
// if (Number.isNaN(parsedNumber)) {
|
||
// throw new Error("کد ملی معتبر نمی باشد.");
|
||
// } else if (
|
||
// Number(parsedNumber) > 100000000 &&
|
||
// Number(parsedNumber) < 999999999
|
||
// ) {
|
||
// throw new Error("کد ملی معتبر نمی باشد.");
|
||
// }
|
||
// return parsedNumber;
|
||
// }),
|
||
});
|
||
|
||
export default function RequestStep1() {
|
||
const router = useRouter();
|
||
const {
|
||
data: profileData,
|
||
isLoading,
|
||
refetch,
|
||
} = useQuery({
|
||
queryKey: ["Profile"],
|
||
queryFn: () => getProfile(),
|
||
refetchOnWindowFocus: false,
|
||
});
|
||
|
||
const editProfileMutation = useMutation({
|
||
mutationKey: ["editProfile"],
|
||
mutationFn: editProfile,
|
||
onSuccess: (res) => {
|
||
router.push("/RequestStep2");
|
||
localStorage.setItem("authorized", "true");
|
||
},
|
||
onError: (err: any | string) => {
|
||
toast(<>{err.response.data.detail}</>, {
|
||
className: "!bg-red-500 !text-white",
|
||
});
|
||
},
|
||
});
|
||
|
||
const form = useForm<z.infer<typeof FormSchema>>({
|
||
resolver: zodResolver(FormSchema),
|
||
});
|
||
if (profileData?.firstName) {
|
||
}
|
||
useEffect(() => {
|
||
if (profileData) {
|
||
form.setValue("firstName", profileData.firstName);
|
||
form.setValue("lastName", profileData.lastName);
|
||
}
|
||
}, [profileData]);
|
||
|
||
function onSubmit(data: z.infer<typeof FormSchema>) {
|
||
// const strNationalId = String(data.nationalId);
|
||
const payload = data;
|
||
editProfileMutation.mutate(payload);
|
||
}
|
||
|
||
return (
|
||
<div className="w-full h-full pt-12 sm:w-2/3 md:w-2/4 max-sm:p-8">
|
||
<div className="flex flex-col justify-center h-full space-y-4 md:space-y-4">
|
||
<p className="text-sm font-bold leading-tight tracking-tight md:text-base text-blue dark:text-white">
|
||
فرم ثبت درخواست{" "}
|
||
</p>
|
||
{profileData && (
|
||
<Form {...form}>
|
||
<form
|
||
onSubmit={form.handleSubmit(onSubmit)}
|
||
className="flex flex-col w-full mt-6 "
|
||
>
|
||
<div className="flex flex-row flex-wrap justify-between">
|
||
<FormField
|
||
control={form.control}
|
||
name="firstName"
|
||
render={({ field }) => (
|
||
<FormItem className="w-full sm:w-[48%]">
|
||
<FormLabel className="text-[#7C838A] text-sm font-medium">
|
||
نام
|
||
</FormLabel>
|
||
<FormControl>
|
||
<Input
|
||
placeholder="نام خود را وارد نمایید"
|
||
{...field}
|
||
/>
|
||
</FormControl>
|
||
<FormMessage />
|
||
</FormItem>
|
||
)}
|
||
/>
|
||
<FormField
|
||
control={form.control}
|
||
name="lastName"
|
||
render={({ field }) => (
|
||
<FormItem className="w-full sm:w-[48%] ">
|
||
<FormLabel className="text-[#7C838A] text-sm font-medium">
|
||
نام خانوادگی
|
||
</FormLabel>
|
||
<FormControl>
|
||
<Input
|
||
placeholder="نام خانوادگی خود را وارد نمایید"
|
||
{...field}
|
||
/>
|
||
</FormControl>
|
||
<FormMessage />
|
||
</FormItem>
|
||
)}
|
||
/>
|
||
</div>
|
||
|
||
<div className="flex justify-center w-full gap-2 mt-8 sm:justify-end">
|
||
<Button
|
||
type="submit"
|
||
className="w-1/2 p-2 text-center text-white sm:w-1/3 lg:w-1/4 bg-blue rounded-xl"
|
||
>
|
||
صفحه ی بعد
|
||
</Button>
|
||
</div>
|
||
</form>
|
||
</Form>
|
||
)}
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|