Sootzani/components/InspectorOperations.tsx

258 lines
8.4 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 } from "@tanstack/react-query";
import { OperateByInspector } from "@/utils/inspectorApi";
import { toast } from "sonner";
import { postComplaintsDetails } from "@/utils/citizenApi";
export default function InspectorOperations({
complaintData,
role,
updateData,
}: {
complaintData: any;
role: string;
updateData: () => any;
}) {
const FormSchema = z.object({
text: z
.string({ required_error: "لطفا متن خود را وارد نمایید." })
.min(8, {
message: "Bio must be at least 8 characters.",
})
.max(260, {
message: "Bio must not be longer than 30 characters.",
}),
...(role == "Inspector" && {
type: z.enum(["private", "public"], {
required_error: "لطفا وضعیت عملیات را انتخاب نمایید.",
}),
}),
operation: z.string({
required_error: "Please select an email to display.",
}),
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 selectOptions = Operations.filter((item, index) =>
possibleOperations.includes(index)
);
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
defaultValues: {
operation:
possibleOperations.length == 1
? String(possibleOperations[0].value)
: undefined,
},
});
const OperationMutation = useMutation({
mutationKey: ["Operation"],
mutationFn:
role == "Inspector" ? OperateByInspector : postComplaintsDetails,
onSuccess: (res) => {
toast("عملیات شما با موفقیت انجام شد. ", {
className: "!bg-green-500 !text-white",
});
},
onError: (err) => {},
});
function onSubmit(data: z.infer<typeof FormSchema>) {
console.log(data);
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");
}
if (role == "Inspector") {
formData.append("EncodedKey", password);
} else {
formData.append("password", localStorage.getItem("password2") as string);
}
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: any) => {
return (
<SelectItem
key={item.value}
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>
);
}