diff --git a/components/AttachmentsDialog.tsx b/components/AttachmentsDialog.tsx new file mode 100644 index 0000000..ec931cc --- /dev/null +++ b/components/AttachmentsDialog.tsx @@ -0,0 +1,57 @@ +import React from "react"; +import { + Dialog, + DialogContent, + DialogDescription, + DialogHeader, + DialogTitle, + DialogTrigger, +} from "@/components/ui/dialog"; +import { X } from "lucide-react"; +export default function AttachmentsDialog({ + children, + attachments, + deleteFile, +}: { + children: React.ReactNode; + attachments: File[] | []; + deleteFile: (file: any) => void; +}) { + return ( + + {children} + + + پیوست‌ها + + {attachments.length == 0 ? ( +
هیج پیوستی وجود ندارد.
+ ) : ( +
+ {attachments?.map((item,index) => { + return ( +
+
+ {index + 1} +
+
+
+

{item.name}

+
+ + deleteFile(item.name)} + className=" cursor-pointer hover:drop-shadow-md" + /> +
+
+ ); + })} +
+ )} +
+
+
+
+ ); +} diff --git a/components/ComplainsDetails.tsx b/components/ComplainsDetails.tsx index a1629cb..6b2a41c 100644 --- a/components/ComplainsDetails.tsx +++ b/components/ComplainsDetails.tsx @@ -116,8 +116,6 @@ export default function ComplainsDetails({ mutationFn: postComplaintTracking, onSuccess: (res) => { setOpen(false); - localStorage.removeItem("trackingNumber2"); - localStorage.removeItem("password2"); }, onError: (err: any | string) => { toast(<>{err.response.data.detail}, { @@ -145,10 +143,15 @@ export default function ComplainsDetails({ getComplaintMutation.mutate({ Data: { trackingNumber: tk, password: pw }, }); - } + } }, []); data = getComplaintMutation?.data; + const possibleOperations = data?.possibleOperationsWithDescription; + if (possibleOperations?.length == 0) { + localStorage.removeItem("trackingNumber2"); + localStorage.removeItem("password2"); + } return ( <> @@ -201,11 +204,11 @@ export default function ComplainsDetails({ )} */} {/* Editt */} -
+
{/* 1 */} -
+
{/* ***** */} -
+

انجام عملیات

@@ -408,16 +411,6 @@ export default function ComplainsDetails({ {/* 3 */} {/* ****** */} -
-

- انجام عملیات -

- getComplaintMutation.mutate(storeData)} - role={role} - complaintData={data} - /> -
) : ( diff --git a/components/InspectorOperations.tsx b/components/InspectorOperations.tsx index 8337b33..1959a4c 100644 --- a/components/InspectorOperations.tsx +++ b/components/InspectorOperations.tsx @@ -29,10 +29,16 @@ import { } from "@/components/ui/select"; import { Operations } from "@/constants"; import useData from "@/store/useData"; -import { useMutation } from "@tanstack/react-query"; +import { useMutation, useQuery } from "@tanstack/react-query"; import { OperateByInspector } from "@/utils/inspectorApi"; import { toast } from "sonner"; import { postComplaintsDetails } from "@/utils/citizenApi"; +import { useState } from "react"; +import { DialogTrigger } from "./ui/dialog"; +import AttachmentsDialog from "./AttachmentsDialog"; +import { Eye, Upload } from "lucide-react"; +import { getOptions } from "@/utils/commonApi"; +import { bytesToMB, formatBytes } from "@/lib/utils"; export default function InspectorOperations({ complaintData, @@ -45,18 +51,63 @@ export default function InspectorOperations({ updateData: (data: any) => any; password?: string; }) { + const [first, setfirst] = useState([]); + + // Queries + const { data, isLoading, refetch } = useQuery({ + queryKey: ["Options"], + queryFn: () => getOptions(), + refetchOnWindowFocus: false, + }); + + const OperationMutation = useMutation({ + mutationKey: ["Operation"], + mutationFn: postComplaintsDetails, + onSuccess: (res) => { + toast("عملیات شما با موفقیت انجام شد. ", { + className: "!bg-green-500 !text-white", + }); + updateData({ + trackingNumber: complaintData.trackingNumber, + password: password + ? password + : (localStorage.getItem("password2") as string), + }); + localStorage.removeItem("trackingNumber2"); + localStorage.removeItem("password2"); + }, + onError: (err: any | string) => { + toast(<>{err.response.data.detail}, { + className: "!bg-red-500 !text-white", + }); + }, + }); + const FormSchema = z.object({ - text: z.string({ required_error: "لطفا متن خود را وارد نمایید." }), + text: z + .string({ required_error: "لطفا متن خود را وارد نمایید." }) + .max( + data?.maxTextLength, + `حداکثر ${data?.maxTextLength} کاراکتر می توانید وارد کنید.` + ), operation: z.string({ required_error: "لطفا نوع عملیات را انتخاب نمایید.", }), media: z - .custom((val) => val instanceof FileList, "Required") + .custom() // .refine((files) => files.length > 0, `Required`) - .refine((files) => files.length <= 5, `Maximum of 5 File are allowed.`) + .refine( + (files) => files.length <= data?.maxFileCount, + `تعداد فایل های پیوست شده می بایست حداکثر ${data?.maxFileCount} عدد باشد.` + ) + .refine( + (files) => files.every((item) => item.size < data?.maxFileSize), + `حجم هر فایل حداکثر می بایست ${data?.maxFileSize} باشد.` + ) .nullish(), }); const possibleOperations = complaintData?.possibleOperationsWithDescription; + const form = useForm>({ resolver: zodResolver(FormSchema), defaultValues: { @@ -67,29 +118,7 @@ export default function InspectorOperations({ }, }); - const OperationMutation = useMutation({ - mutationKey: ["Operation"], - mutationFn: postComplaintsDetails, - onSuccess: (res) => { - toast("عملیات شما با موفقیت انجام شد. ", { - className: "!bg-green-500 !text-white", - }); - console.log(complaintData.trackingNumber); - console.log(localStorage.getItem("password2") as string); - updateData({ - trackingNumber: complaintData.trackingNumber, - password: password - ? password - : (localStorage.getItem("password2") as string), - }); - }, - onError: (err: any | string) => { - toast(<>{err.response.data.detail}, { - className: "!bg-red-500 !text-white", - }); - }, - }); - + //Functions function onSubmit(data: z.infer) { const formData = new FormData(); formData.append("text", data.text); @@ -107,54 +136,113 @@ export default function InspectorOperations({ OperationMutation.mutate(formData); } + const deleteFileFromAttachments = (file: any) => { + const filtered = first?.filter((item) => item.name !== file); + setfirst(filtered); + form.setValue("media", filtered); + }; + + const allowedExtensions = data?.allowedExtensions; + const split = allowedExtensions?.split(","); + const withDot = split?.map((item: string) => "." + item); + const newAllowedExtensions = withDot?.join(","); + const formatSize = (size: number) => { + const string = formatBytes(size); + return ( + + {string} + + ); + }; return (
( + render={({ field, fieldState: { error } }) => ( متن