387 lines
14 KiB
TypeScript
387 lines
14 KiB
TypeScript
"use client";
|
||
import React, { useState } from "react";
|
||
import { zodResolver } from "@hookform/resolvers/zod";
|
||
import { useForm, Controller } from "react-hook-form";
|
||
import * as z from "zod";
|
||
import {
|
||
Form,
|
||
FormControl,
|
||
FormDescription,
|
||
FormField,
|
||
FormItem,
|
||
FormLabel,
|
||
FormMessage,
|
||
} from "@/components/ui/form";
|
||
import {
|
||
Select,
|
||
SelectContent,
|
||
SelectItem,
|
||
SelectTrigger,
|
||
SelectValue,
|
||
} from "@/components/ui/select";
|
||
import { Input } from "@/components/ui/input";
|
||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||
import { getOperatorProfile } from "../../utils/api";
|
||
import { getCookie } from "cookies-next";
|
||
import DatePicker, { DateObject } from "react-multi-date-picker";
|
||
import gregorian from "react-date-object/calendars/gregorian";
|
||
import gregorian_en from "react-date-object/locales/gregorian_en";
|
||
import persian from "react-date-object/calendars/persian";
|
||
import persian_fa from "react-date-object/locales/persian_fa";
|
||
import { editOperator, getOperator } from "../../utils/api/Operator";
|
||
import dayjs from "dayjs";
|
||
import utc from "dayjs/plugin/utc";
|
||
import jalaliday from "jalaliday";
|
||
import { useToast } from "@/components/ui/use-toast";
|
||
import dynamic from "next/dynamic";
|
||
import { Dna } from "react-loader-spinner";
|
||
import { errorMessages, messageIcons } from "@/app/constants";
|
||
const ProfileUploader = dynamic(
|
||
() => import("@/app/components/UploadProfile"),
|
||
{
|
||
ssr: false,
|
||
}
|
||
);
|
||
dayjs.extend(jalaliday);
|
||
dayjs.extend(utc);
|
||
function OperatorProfile(props: any) {
|
||
const [value, setValue] = useState<any>();
|
||
const [url, setUrl] = useState<any>();
|
||
const queryClient = useQueryClient();
|
||
const token = getCookie("token");
|
||
const { toast } = useToast();
|
||
console.log(token);
|
||
const formSchema = z.object({
|
||
userName: z
|
||
.string({
|
||
required_error: "تلفن الزامیست",
|
||
invalid_type_error: "Age must be a number",
|
||
})
|
||
.nullish(),
|
||
firstName: z.nullable(
|
||
z.string({
|
||
required_error: "رمز عبور الزامیست",
|
||
// invalid_type_error: "Age must be a number",
|
||
})
|
||
),
|
||
lastName: z.nullable(
|
||
z.string({
|
||
required_error: "رمز عبور الزامیست",
|
||
// invalid_type_error: "Age must be a number",
|
||
})
|
||
),
|
||
birthday: z.nullable(
|
||
z.any({
|
||
required_error: "رمز عبور الزامیست",
|
||
// invalid_type_error: "Age must be a number",
|
||
})
|
||
),
|
||
gender: z.nullable(
|
||
z.any({
|
||
required_error: "رمز عبور الزامیست",
|
||
// invalid_type_error: "Age must be a number",
|
||
})
|
||
),
|
||
});
|
||
|
||
const form = useForm<z.infer<typeof formSchema>>({
|
||
resolver: zodResolver(formSchema),
|
||
});
|
||
|
||
const editOperatorMutation = useMutation({
|
||
mutationFn: editOperator,
|
||
onSuccess: (res) => {
|
||
queryClient.invalidateQueries(["OperatorProfile"]);
|
||
toast({
|
||
description: (
|
||
<p className=" flex gap-1 items-center">
|
||
{messageIcons.success}
|
||
تغییرات شما با موفقیت ثبت شد.
|
||
</p>
|
||
),
|
||
className: "bg-green-600 text-white",
|
||
});
|
||
},
|
||
onError: (err: any) => {
|
||
toast({
|
||
description: (
|
||
<p className=" flex gap-1 items-center">
|
||
{messageIcons.error}
|
||
{errorMessages[err.response.status]}
|
||
</p>
|
||
),
|
||
className: "bg-red-600 text-white",
|
||
});
|
||
},
|
||
});
|
||
|
||
const onSubmit = (Data: z.infer<typeof formSchema>) => {
|
||
console.log(Data);
|
||
const formData = new FormData();
|
||
const gender = Number(Data.gender);
|
||
formData.append("Id", data.id);
|
||
formData.append("FirstName", Data.firstName ? Data.firstName : null);
|
||
formData.append("LastName", Data.lastName ? Data.lastName : null);
|
||
if (gender) {
|
||
formData.append("Gender", Data.gender);
|
||
}
|
||
if (Data.birthday) {
|
||
formData.append("BirthDate", Data.birthday);
|
||
}
|
||
if (url) {
|
||
formData.append("AvatarFile", url?.originFileObj);
|
||
}
|
||
editOperatorMutation.mutate({ token: token, Data: formData });
|
||
};
|
||
|
||
const { data, isLoading, isFetching } = useQuery({
|
||
queryKey: ["OperatorProfile"],
|
||
queryFn: () => getOperator(token),
|
||
onSuccess: (res) => {
|
||
if (res.gender != null) {
|
||
form.setValue("gender", (res.gender + 1).toString());
|
||
}
|
||
if (res.birthDate) {
|
||
console.log(res.birthDate);
|
||
const date = new DateObject(res.birthDate).convert(
|
||
gregorian,
|
||
gregorian_en
|
||
);
|
||
form.setValue("birthday", date.format("YYYY-MM-DDTHH:mm:ss.SSSSSSS"));
|
||
const date2 = new DateObject(res.birthDate).convert(
|
||
persian,
|
||
persian_fa
|
||
);
|
||
setValue(date2);
|
||
}
|
||
},
|
||
});
|
||
console.log(data);
|
||
// console.log(data?.gender.toString());
|
||
|
||
console.log(form.getValues("birthday"));
|
||
// console.log(data?.birthDate);
|
||
if (isLoading) {
|
||
return (
|
||
<div>
|
||
<Dna
|
||
visible={true}
|
||
height="80"
|
||
width="80"
|
||
ariaLabel="dna-loading"
|
||
wrapperStyle={{}}
|
||
wrapperClass="dna-wrapper mx-auto mt-20"
|
||
/>
|
||
</div>
|
||
);
|
||
}
|
||
return (
|
||
<div className="w-full mt-8 sm:mt-5 lg:mx-auto sm:overflow-y-auto scrollbar-thumb-gray-600 scrollbar-track-gray-100 scrollbar-thin p-2">
|
||
<p className="text-[#686868] font-bold mb-2">پروفایل اپراتور</p>
|
||
<Form {...form}>
|
||
<form onSubmit={form.handleSubmit(onSubmit)} className="">
|
||
{!isLoading && (
|
||
<>
|
||
<div className=" bg-white rounded-lg shadow-md p-7">
|
||
<div className="grid md:grid-cols-3 grid-cols-1 gag-x-5 place-content-center">
|
||
<div className=" w-full col-span-1 flex justify-center items-center">
|
||
<ProfileUploader
|
||
fileUrl={(file) => setUrl(file)}
|
||
Picture={data?.avatar}
|
||
/>
|
||
</div>
|
||
<div className="grid md:grid-cols-2 gap-x-5 col-span-2">
|
||
<FormField
|
||
control={form.control}
|
||
name="userName"
|
||
defaultValue={data?.userName}
|
||
disabled
|
||
render={({ field }) => (
|
||
<FormItem>
|
||
<FormLabel className="text-black">
|
||
نام کاربری
|
||
</FormLabel>
|
||
<FormControl>
|
||
<Input
|
||
// id="firstName"
|
||
disabled
|
||
className=" text-black"
|
||
placeholder="نام"
|
||
{...field}
|
||
/>
|
||
</FormControl>
|
||
<FormDescription></FormDescription>
|
||
<FormMessage />
|
||
</FormItem>
|
||
)}
|
||
/>
|
||
<FormField
|
||
control={form.control}
|
||
name="firstName"
|
||
defaultValue={data?.firstName}
|
||
render={({ field }) => (
|
||
<FormItem>
|
||
<FormLabel className="text-black">نام</FormLabel>
|
||
<FormControl>
|
||
<Input
|
||
id="firstName"
|
||
className=" text-black"
|
||
placeholder="نام"
|
||
{...field}
|
||
/>
|
||
</FormControl>
|
||
<FormDescription></FormDescription>
|
||
<FormMessage />
|
||
</FormItem>
|
||
)}
|
||
/>
|
||
<FormField
|
||
control={form.control}
|
||
name="lastName"
|
||
rules={{ required: false }}
|
||
defaultValue={data?.lastName}
|
||
render={({ field }) => (
|
||
<FormItem>
|
||
<FormLabel className="text-black">
|
||
نام خانوادگی
|
||
</FormLabel>
|
||
<FormControl>
|
||
<Input
|
||
// id="lastName"
|
||
className="text-black"
|
||
placeholder="نام خانوادگی"
|
||
{...field}
|
||
/>
|
||
</FormControl>
|
||
<FormDescription></FormDescription>
|
||
<FormMessage />
|
||
</FormItem>
|
||
)}
|
||
/>
|
||
<FormField
|
||
control={form.control}
|
||
name="gender"
|
||
render={({ field }) => (
|
||
<FormItem>
|
||
<FormLabel className=" text-black">جنسیت</FormLabel>
|
||
<Select
|
||
onValueChange={field.onChange}
|
||
defaultValue={field.value}
|
||
>
|
||
<FormControl>
|
||
<SelectTrigger
|
||
dir="rtl"
|
||
className=" text-black placeholder:text-gray-400"
|
||
>
|
||
<SelectValue
|
||
// className=" placeholder:text-gray-400"
|
||
placeholder="جنسیت خود را انتخاب نمایید"
|
||
/>
|
||
</SelectTrigger>
|
||
</FormControl>
|
||
<SelectContent className="text-black" dir="rtl">
|
||
<SelectItem value="1">مرد</SelectItem>
|
||
<SelectItem value="2">زن</SelectItem>
|
||
</SelectContent>
|
||
</Select>
|
||
<FormDescription>
|
||
{/* You can manage email addresses in your{" "}
|
||
<Link href="/examples/forms">email settings</Link>. */}
|
||
</FormDescription>
|
||
<FormMessage />
|
||
</FormItem>
|
||
)}
|
||
/>
|
||
{/* <div className=" flex flex-col">
|
||
<label className="">
|
||
<span className="text-sm text-gray-900 ">تاریخ تولد</span>
|
||
</label>
|
||
<DatePicker
|
||
calendar={persian}
|
||
locale={persian_fa}
|
||
// value={date2?.format()}
|
||
className=" !text-black"
|
||
inputClass=" text-black border w-full text-sm md:text-base h-10 rounded-md px-2 cursor-pointer"
|
||
onChange={(ab) => {
|
||
const date = new DateObject(ab).convert(
|
||
gregorian,
|
||
gregorian_en
|
||
);
|
||
setValue(date.format("YYYY-MM-DDTHH:mm:ss.SSSSSSS"));
|
||
}}
|
||
onOpenPickNewDate={false}
|
||
// format="YYYY-MM-DDTHH:mm:ss.sssZ"
|
||
/>
|
||
</div> */}
|
||
<FormField
|
||
control={form.control}
|
||
name="birthday"
|
||
render={({ field }) => (
|
||
<FormItem className="">
|
||
<FormLabel className="text-black">
|
||
تاریخ تولد
|
||
</FormLabel>
|
||
<FormControl className="w-full">
|
||
<DatePicker
|
||
calendar={persian}
|
||
locale={persian_fa}
|
||
style={{ width: "100%" }}
|
||
// value={date2?.format()}
|
||
className=" !text-black"
|
||
inputClass=" text-black border w-full placeholder:text-sm md:text-base h-10 rounded-md px-2 cursor-pointer"
|
||
arrowClassName=""
|
||
value={value || ""}
|
||
onChange={(ab) => {
|
||
const date = new DateObject(ab).convert(
|
||
gregorian,
|
||
gregorian_en
|
||
);
|
||
form.setValue(
|
||
"birthday",
|
||
date.format("YYYY-MM-DDTHH:mm:ss.SSSSSSS")
|
||
);
|
||
console.log(
|
||
date.format("YYYY-MM-DDTHH:mm:ss.SSSSSSS")
|
||
);
|
||
// setValue(
|
||
// date.format("YYYY-MM-DDTHH:mm:ss.SSSSSSS")
|
||
// );
|
||
// console.log(ab.format("YYYY-MM-DD"));
|
||
}}
|
||
placeholder="تاریخ تولد خود را انتخاب نمایید"
|
||
onOpenPickNewDate={false}
|
||
// {...field}
|
||
// format="YYYY-MM-DDTHH:mm:ss.sssZ"
|
||
/>
|
||
</FormControl>
|
||
<FormDescription></FormDescription>
|
||
<FormMessage />
|
||
</FormItem>
|
||
)}
|
||
/>
|
||
</div>
|
||
{/* <Button className=" w-full" type="submit">
|
||
ثبت نام
|
||
</Button> */}
|
||
{/* <Filepond /> */}
|
||
{/* <Input></Input> */}
|
||
</div>
|
||
</div>
|
||
<div className=" flex justify-end">
|
||
<button
|
||
type="submit"
|
||
className=" mt-6 px-2 h-12 w-24 sm:w-28 md:w-36 text-xs md:text-sm text-white rounded-full bg-main hover:bg-main-dark hover:shadow-sm hover:shadow-main"
|
||
>
|
||
ذخیره
|
||
</button>
|
||
</div>
|
||
</>
|
||
)}
|
||
</form>
|
||
</Form>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
export default OperatorProfile;
|