Sootzani-Inspector/components/InspectorOperations.tsx

253 lines
8.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"use client";
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 { Textarea } from "@/components/ui/textarea";
import { Label } from "./ui/label";
import { Input } from "./ui/input";
import { RadioGroup, RadioGroupItem } from "./ui/radio-group";
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectLabel,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Operations } from "@/constants";
import useData from "@/store/useData";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { OperateByInspector } from "@/utils/inspectorApi";
import { toast } from "sonner";
import secureLocalStorage from "react-secure-storage";
import { Decrypt } from "@/lib/utils";
export default function InspectorOperations({
complaintData,
role,
updateData,
id,
key2,
}: {
complaintData: any;
role: string;
updateData: (data: any) => any;
id: any;
key2: any;
}) {
const FormSchema = z.object({
text: z.string({ required_error: "لطفا متن خود را وارد نمایید." }),
...(role == "Inspector" && {
type: z.enum(["private", "public"], {
required_error: "لطفا وضعیت عملیات را انتخاب نمایید.",
}),
}),
operation: z.string({
required_error: "لطفا نوع عملیات را انتخاب نمایید.",
}),
media: z
.custom<FileList>((val) => val instanceof FileList, "Required")
// .refine((files) => files.length > 0, `Required`)
.refine((files) => files.length <= 5, `Maximum of 5 File are allowed.`)
.nullish(),
});
const password = useData((state) => state.data.password);
const possibleOperations = complaintData?.possibleOperationsWithDescription;
const queryClient = useQueryClient();
console.log(possibleOperations);
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
defaultValues: {
operation:
possibleOperations.length == 1
? String(possibleOperations[0].value)
: undefined,
text: "",
},
});
const OperationMutation = useMutation({
mutationKey: ["Operation"],
mutationFn: OperateByInspector,
onSuccess: (res) => {
toast("عملیات شما با موفقیت انجام شد. ", {
className: "!bg-green-500 !text-white",
});
const tk = localStorage.getItem("trackingNumber");
updateData({ trackingNumber: tk, password: key2 });
},
onError: (err) => {},
});
function onSubmit(data: z.infer<typeof FormSchema>) {
const storedData = JSON.parse(localStorage.getItem("CompliantData"));
console.log(storedData);
console.log(key2);
const dcKey = Decrypt(storedData.cipherKeyInspector, key2);
const formData = new FormData();
formData.append("text", data.text);
formData.append("trackingNumber", complaintData.trackingNumber);
formData.append("Operation", data.operation);
if (data.type) {
formData.append("IsPublic", data.type == "public" ? "true" : "false");
}
formData.append("EncodedKey", key2);
if (data?.media && data?.media?.length > 0) {
for (let i = 0; i < data.media.length; i++) {
formData.append("Medias", data.media[i]);
}
}
OperationMutation.mutate(formData);
}
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="">
<FormField
control={form.control}
name="text"
render={({ field }) => (
<FormItem>
<FormLabel className="">متن</FormLabel>
<FormControl>
<Textarea
placeholder="متن خود را وارد نمایید"
className="resize-none bg-white"
{...field}
/>
</FormControl>
{/* <FormDescription>
You can <span>@mention</span> other users and organizations.
</FormDescription> */}
<FormMessage />
</FormItem>
)}
/>
{/* <div className="grid w-full max-w-sm items-center gap-1.5 mt-1">
<Label htmlFor="picture">پیوست</Label>
<Input multiple id="picture" type="file" className=" bg-white" />
</div> */}
<FormField
control={form.control}
name="media"
render={({ field }) => (
<FormItem>
<FormLabel>پیوست</FormLabel>
<FormControl>
<Input
accept=".jpg, .jpeg, .png, .svg, .gif, .mp4 , .pdf"
type="file"
onChange={(e) => {
field.onChange(e.target.files ? e.target.files : null);
console.log(e.target.files);
}}
multiple
className=" bg-white"
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
{role == "Inspector" && (
<FormField
control={form.control}
name="type"
render={({ field }) => (
<FormItem className="space-y-3">
<FormControl dir="rtl">
<RadioGroup
onValueChange={field.onChange}
defaultValue={field.value}
className="flex space-y-1 text-sm"
>
<FormItem className="flex items-center space-x-3 space-y-0">
<FormControl>
<RadioGroupItem value="private" className=" ml-2" />
</FormControl>
<FormLabel className="font-normal">خصوصی</FormLabel>
</FormItem>
<FormItem className="flex items-center space-x-3 space-y-0">
<FormControl>
<RadioGroupItem value="public" className=" ml-2" />
</FormControl>
<FormLabel className="font-normal">عمومی</FormLabel>
</FormItem>
</RadioGroup>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
)}
<div className=" flex justify-between mt-3 gap-2">
<FormField
control={form.control}
name="operation"
render={({ field }) => (
<FormItem className=" flex-1">
{/* <FormLabel>Email</FormLabel> */}
<Select
onValueChange={field.onChange}
defaultValue={field.value}
dir="rtl"
>
<FormControl>
<SelectTrigger
className=" bg-white h-10 !placeholder:text-xs text-xs"
dir="rtl"
>
<SelectValue
className=" text-xs"
placeholder="نوع عملیات را انتخاب نمایید."
/>
</SelectTrigger>
</FormControl>
<SelectContent dir="rtl">
<SelectGroup>
<SelectLabel>عملیات</SelectLabel>
{possibleOperations.map((item) => {
return (
<SelectItem value={String(item.value)}>
{item.description}
</SelectItem>
);
})}
</SelectGroup>
</SelectContent>
</Select>
{/* <FormDescription>
You can manage email addresses in your{" "}
</FormDescription> */}
<FormMessage />
</FormItem>
)}
/>
<Button
disabled={possibleOperations.length == 0}
className=" bg-blue w-1/3"
type="submit"
>
ثبت
</Button>
</div>
</form>
</Form>
);
}