File uploader changed
parent
6f7b40a907
commit
9f6b2d744c
|
|
@ -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 (
|
||||
<Dialog>
|
||||
{children}
|
||||
<DialogContent>
|
||||
<DialogHeader>
|
||||
<DialogTitle>پیوستها</DialogTitle>
|
||||
<DialogDescription>
|
||||
{attachments.length == 0 ? (
|
||||
<div className=" text-center my-5">هیج پیوستی وجود ندارد.</div>
|
||||
) : (
|
||||
<div className=" flex flex-col gap-2 mt-2">
|
||||
{attachments?.map((item,index) => {
|
||||
return (
|
||||
<div key={index} className=" flex gap-2 w-full items-center">
|
||||
<div className=" p-2 bg-blue w-10 grid place-content-center rounded-lg text-white">
|
||||
{index + 1}
|
||||
</div>
|
||||
<div className="bg-gray-200 p-2 rounded-lg max-w-full justify-between flex flex-1">
|
||||
<div className="">
|
||||
<p className=" truncate">{item.name}</p>
|
||||
</div>
|
||||
|
||||
<X
|
||||
onClick={() => deleteFile(item.name)}
|
||||
className=" cursor-pointer hover:drop-shadow-md"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
|
@ -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 */}
|
||||
<div className="flex items-start w-full gap-2 mt-2 max-lg:flex-col">
|
||||
<div className="flex items-start w-full gap-2 mt-2 max-lg:flex-col mb-10">
|
||||
{/* 1 */}
|
||||
<div className="flex flex-col w-full lg:w-1/3 gap-3 lg:sticky top-28 lg:h-[80vh]">
|
||||
<div className="flex flex-col w-full lg:w-1/3 gap-3 lg:sticky top-28 lg:h-[80vh] max-lg:order-3">
|
||||
{/* ***** */}
|
||||
<div className="max-lg:hidden bg-[#E9E9E9] rounded-md p-4 px-10 flex flex-col lg:h-full">
|
||||
<div className=" bg-[#E9E9E9] rounded-md p-4 px-10 flex flex-col lg:h-full">
|
||||
<p className="text-[#464646] text-base font-bold mb-1">
|
||||
انجام عملیات
|
||||
</p>
|
||||
|
|
@ -408,16 +411,6 @@ export default function ComplainsDetails({
|
|||
|
||||
{/* 3 */}
|
||||
{/* ****** */}
|
||||
<div className="lg:hidden order-2 w-full bg-[#E9E9E9] rounded-md py-4 px-10 flex flex-col mt-2">
|
||||
<p className="text-[#464646] text-base font-bold mb-1">
|
||||
انجام عملیات
|
||||
</p>
|
||||
<InspectorOperations
|
||||
updateData={() => getComplaintMutation.mutate(storeData)}
|
||||
role={role}
|
||||
complaintData={data}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
|
|
|
|||
|
|
@ -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<File[]>([]);
|
||||
|
||||
// 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<FileList>((val) => val instanceof FileList, "Required")
|
||||
.custom<File[]>()
|
||||
// .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<z.infer<typeof FormSchema>>({
|
||||
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<typeof FormSchema>) {
|
||||
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 (
|
||||
<span className="" dir="ltr">
|
||||
{string}
|
||||
</span>
|
||||
);
|
||||
};
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="text"
|
||||
render={({ field }) => (
|
||||
render={({ field, fieldState: { error } }) => (
|
||||
<FormItem>
|
||||
<FormLabel className="">متن</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea
|
||||
placeholder="متن خود را وارد نمایید"
|
||||
className="resize-none bg-white lg:min-h-[20em]"
|
||||
{...field}
|
||||
// disabled={possibleOperations.length == 0}
|
||||
readOnly={possibleOperations.length == 0}
|
||||
/>
|
||||
</FormControl>
|
||||
{/* <FormDescription>
|
||||
You can <span>@mention</span> other users and organizations.
|
||||
</FormDescription> */}
|
||||
<FormMessage />
|
||||
{!error ? (
|
||||
<FormDescription className=" text-xs my-1 ">
|
||||
حداکثر {data?.maxTextLength} کاراکتر می توانید وارد کنید.
|
||||
</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 }) => (
|
||||
render={({ field, fieldState: { error } }) => (
|
||||
<FormItem>
|
||||
<FormLabel>پیوست</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
accept=".jpg, .jpeg, .png, .svg, .gif, .mp4 , .pdf"
|
||||
<FormLabel>
|
||||
پیوستها ({field?.value?.length ? field.value.length : 0})
|
||||
</FormLabel>
|
||||
<div className="flex gap-2 w-full items-center mt-1 mb-2">
|
||||
<AttachmentsDialog
|
||||
attachments={first}
|
||||
deleteFile={(item) => deleteFileFromAttachments(item)}
|
||||
>
|
||||
<DialogTrigger className=" bg-white rounded-md h-9 text-xs flex-1 flex gap-2 items-center justify-center">
|
||||
<Eye className=" w-4 h-4" />
|
||||
<p>مشاهده</p>
|
||||
</DialogTrigger>
|
||||
</AttachmentsDialog>
|
||||
<label
|
||||
htmlFor="fileInput"
|
||||
className="cursor-pointer text-sm rounded-md grid place-content-center h-9 px-2 bg-white w-1/2 items-center"
|
||||
>
|
||||
<div className=" flex gap-2 justify-center items-center">
|
||||
<p className="text-xs flex gap-2 items-center">
|
||||
<Upload className=" w-4 h-4" />
|
||||
<span>افزودن</span>
|
||||
</p>
|
||||
</div>
|
||||
</label>
|
||||
<input
|
||||
id="fileInput"
|
||||
type="file"
|
||||
onChange={(e) => {
|
||||
field.onChange(e.target.files ? e.target.files : null);
|
||||
}}
|
||||
className="hidden"
|
||||
accept={newAllowedExtensions}
|
||||
multiple
|
||||
className="bg-white "
|
||||
disabled={possibleOperations.length == 0}
|
||||
onChange={(e) => {
|
||||
// field.onChange(e.target.files ? e.target.files : null);
|
||||
if (first && e.target.files) {
|
||||
const fileArray1 = Array.from(first);
|
||||
const fileArray2 = Array.from(e.target.files);
|
||||
const mergedArray = fileArray1.concat(fileArray2);
|
||||
setfirst(mergedArray);
|
||||
field.onChange(mergedArray);
|
||||
} else if (!first && e.target.files) {
|
||||
const fileArray2 = Array.from(e.target.files);
|
||||
setfirst(fileArray2);
|
||||
field.onChange(fileArray2);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</div>
|
||||
{!error ? (
|
||||
<FormDescription className=" text-xs my-1 ">
|
||||
<span>
|
||||
حجم هر فایل حداکثر می بایست {formatSize(data?.maxFileSize)}{" "}
|
||||
مگابایت باشد
|
||||
</span>{" "}
|
||||
و{" "}
|
||||
{`تعداد فایل های پیوست شده می بایست حداکثر ${data?.maxFileCount} تا باشد.`}
|
||||
</FormDescription>
|
||||
) : (
|
||||
<FormMessage />
|
||||
)}
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
|
|
|||
|
|
@ -40,60 +40,20 @@ import {
|
|||
postComplaintSubmit,
|
||||
} from "@/utils/citizenApi";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { getCaptcha, getCategory, getOrganizations } from "@/utils/commonApi";
|
||||
import {
|
||||
getCaptcha,
|
||||
getCategory,
|
||||
getOptions,
|
||||
getOrganizations,
|
||||
} from "@/utils/commonApi";
|
||||
import { Oval, ThreeDots } from "react-loader-spinner";
|
||||
import useToken from "@/store/useToken";
|
||||
import { toast } from "sonner";
|
||||
import { RefreshCcw } from "lucide-react";
|
||||
import { Eye, RefreshCcw, Upload } from "lucide-react";
|
||||
import { getCookie } from "cookies-next";
|
||||
|
||||
const MAX_IMAGE_SIZE = 5242880; // 5 MB
|
||||
const ALLOWED_IMAGE_TYPES = [
|
||||
"image/jpeg",
|
||||
"image/png",
|
||||
"image/webp",
|
||||
"image/jpg",
|
||||
];
|
||||
|
||||
const FormSchema = z.object({
|
||||
title: z.string().min(1, {
|
||||
message: "لطفا موضوع شکایت را وارد نمایید.",
|
||||
}),
|
||||
OrganizationId: z.string({
|
||||
required_error: "لطفا دستگاه مربوطه را انتخاب نمایید.",
|
||||
}),
|
||||
person: z.string().optional(),
|
||||
description: z
|
||||
.string()
|
||||
.min(1, {
|
||||
message: "لطفا توضیحات خود را بنویسید.",
|
||||
})
|
||||
.max(300, {
|
||||
message: "تعداد کاراکترها بیشتر از حد مجاز میباشد.",
|
||||
}),
|
||||
uploadFile: 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.`)
|
||||
.refine(
|
||||
(files) => Array.from(files).every((file) => file.size <= MAX_IMAGE_SIZE),
|
||||
`Each file size should be less than 5 MB.`
|
||||
)
|
||||
.nullish(),
|
||||
// .refine(
|
||||
// (files) =>
|
||||
// Array.from(files).every((file) =>
|
||||
// ALLOWED_IMAGE_TYPES.includes(file.type)
|
||||
// ),
|
||||
// "Only these types are allowed .jpg, .jpeg, .png and .webp"
|
||||
// ),
|
||||
categoryId: z.string({
|
||||
required_error: "لطفا دسته بندی را انتخاب نمایید.",
|
||||
}),
|
||||
captchaValue: z.string().min(1, {
|
||||
message: "لطفا کد امنیتی را وارد نمایید.",
|
||||
}),
|
||||
});
|
||||
import { formatBytes } from "@/lib/utils";
|
||||
import AttachmentsDialog from "./AttachmentsDialog";
|
||||
import { DialogTrigger } from "./ui/dialog";
|
||||
|
||||
interface Organization {
|
||||
title: string;
|
||||
|
|
@ -102,14 +62,53 @@ interface Organization {
|
|||
id: string;
|
||||
}
|
||||
|
||||
// Form Hook
|
||||
export default function RequestStep2() {
|
||||
// const [trackingComplaint, setTrackingComplaint] = useState({});
|
||||
const isAuth = useToken((state) => state.token);
|
||||
const router = useRouter();
|
||||
useEffect(() => {
|
||||
useToken.persist.rehydrate();
|
||||
}, []);
|
||||
const [file, setFile] = useState<File[]>([]);
|
||||
|
||||
// Queries
|
||||
const { data: optionsData, isLoading: isLoadingData } = useQuery({
|
||||
queryKey: ["Options"],
|
||||
queryFn: () => getOptions(),
|
||||
refetchOnWindowFocus: false,
|
||||
});
|
||||
|
||||
const FormSchema = z.object({
|
||||
title: z.string().min(1, {
|
||||
message: "لطفا موضوع شکایت را وارد نمایید.",
|
||||
}),
|
||||
OrganizationId: z.string({
|
||||
required_error: "لطفا دستگاه مربوطه را انتخاب نمایید.",
|
||||
}),
|
||||
person: z.string().optional(),
|
||||
description: z
|
||||
.string()
|
||||
.min(1, {
|
||||
message: "لطفا توضیحات خود را بنویسید.",
|
||||
})
|
||||
.max(300, {
|
||||
message: "تعداد کاراکترها بیشتر از حد مجاز میباشد.",
|
||||
}),
|
||||
uploadFile: z
|
||||
.custom<File[]>()
|
||||
// .refine((files) => files.length > 0, `Required`)
|
||||
.refine(
|
||||
(files) => files.length <= optionsData?.maxFileCount,
|
||||
`تعداد فایل های پیوست شده می بایست حداکثر ${optionsData?.maxFileCount} عدد باشد.`
|
||||
)
|
||||
.refine(
|
||||
(files) => files.every((item) => item.size < optionsData?.maxFileSize),
|
||||
`حجم هر فایل حداکثر می بایست ${optionsData?.maxFileSize} مگابایت باشد.`
|
||||
)
|
||||
.nullish(),
|
||||
categoryId: z.string({
|
||||
required_error: "لطفا دسته بندی را انتخاب نمایید.",
|
||||
}),
|
||||
captchaValue: z.string().min(1, {
|
||||
message: "لطفا کد امنیتی را وارد نمایید.",
|
||||
}),
|
||||
});
|
||||
|
||||
const complaintSubmitMutation = useMutation({
|
||||
mutationKey: ["complaintSubmit"],
|
||||
mutationFn: postComplaintSubmit,
|
||||
|
|
@ -217,6 +216,25 @@ export default function RequestStep2() {
|
|||
|
||||
const { resetField } = useForm();
|
||||
|
||||
const deleteFileFromAttachments = (File: any) => {
|
||||
const filtered = file?.filter((item) => item.name !== File);
|
||||
setFile(filtered);
|
||||
form.setValue("uploadFile", filtered);
|
||||
};
|
||||
|
||||
const allowedExtensions = optionsData?.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 (
|
||||
<span className="" dir="ltr">
|
||||
{string}
|
||||
</span>
|
||||
);
|
||||
};
|
||||
|
||||
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-3 md:space-y-4">
|
||||
|
|
@ -329,7 +347,7 @@ export default function RequestStep2() {
|
|||
)}
|
||||
/>
|
||||
|
||||
<div className="flex flex-row flex-wrap justify-between">
|
||||
<div className="flex flex-row flex-wrap justify-between mb-2">
|
||||
{/* Select Option */}
|
||||
<FormField
|
||||
control={form.control}
|
||||
|
|
@ -366,7 +384,7 @@ export default function RequestStep2() {
|
|||
<FormField
|
||||
control={form.control}
|
||||
name="uploadFile"
|
||||
render={({ field: { onChange }, ...field }) => {
|
||||
render={({ fieldState: { error }, field }) => {
|
||||
// Get current uploadFile value (always watched updated)
|
||||
const uploadFile = form.watch("uploadFile");
|
||||
|
||||
|
|
@ -380,44 +398,65 @@ export default function RequestStep2() {
|
|||
|
||||
return (
|
||||
<FormItem className="w-full sm:w-[48%]">
|
||||
<FormLabel className="text-[#7C838A] text-sm font-medium mt-7">
|
||||
پیوست
|
||||
<FormLabel className=" text-[#7C838A] text-sm mt-7">
|
||||
پیوستها (
|
||||
{field?.value?.length ? field.value.length : 0})
|
||||
</FormLabel>
|
||||
{/* File Upload */}
|
||||
<FormControl>
|
||||
<Input
|
||||
<div className="flex gap-2 w-full items-center">
|
||||
<AttachmentsDialog
|
||||
attachments={file}
|
||||
deleteFile={(item) => deleteFileFromAttachments(item)}
|
||||
>
|
||||
<DialogTrigger className=" bg-slate-200 rounded-md h-9 text-xs flex-1 flex gap-2 items-center justify-center">
|
||||
<Eye className=" w-4 h-4" />
|
||||
<p>مشاهده</p>
|
||||
</DialogTrigger>
|
||||
</AttachmentsDialog>
|
||||
<label
|
||||
htmlFor="fileInput"
|
||||
className="cursor-pointer text-sm rounded-md grid place-content-center h-9 px-2 bg-slate-200 w-1/2 items-center"
|
||||
>
|
||||
<div className=" flex gap-2 justify-center items-center">
|
||||
<p className="text-xs flex gap-2 items-center">
|
||||
<Upload className=" w-4 h-4" />
|
||||
<span>افزودن</span>
|
||||
</p>
|
||||
</div>
|
||||
</label>
|
||||
<input
|
||||
id="fileInput"
|
||||
type="file"
|
||||
className="mb-3"
|
||||
accept="image/* , .pdf"
|
||||
multiple={true}
|
||||
disabled={form.formState.isSubmitting}
|
||||
{...field}
|
||||
onChange={(event) => {
|
||||
// Triggered when user uploaded a new file
|
||||
// FileList is immutable, so we need to create a new one
|
||||
const dataTransfer = new DataTransfer();
|
||||
|
||||
// Add old images
|
||||
// if (images) {
|
||||
// Array.from(images).forEach((image) =>
|
||||
// dataTransfer.items.add(image)
|
||||
// );
|
||||
// }
|
||||
|
||||
// Add newly uploaded images
|
||||
Array.from(event.target.files!).forEach((image) =>
|
||||
dataTransfer.items.add(image)
|
||||
);
|
||||
|
||||
// Validate and update uploaded file
|
||||
const newFiles = dataTransfer.files;
|
||||
onChange(newFiles);
|
||||
className="hidden"
|
||||
accept={newAllowedExtensions}
|
||||
multiple
|
||||
onChange={(e) => {
|
||||
// field.onChange(e.target.files ? e.target.files : null);
|
||||
if (file && e.target.files) {
|
||||
const fileArray1 = Array.from(file);
|
||||
const fileArray2 = Array.from(e.target.files);
|
||||
const mergedArray = fileArray1.concat(fileArray2);
|
||||
setFile(mergedArray);
|
||||
field.onChange(mergedArray);
|
||||
} else if (!file && e.target.files) {
|
||||
const fileArray2 = Array.from(e.target.files);
|
||||
setFile(fileArray2);
|
||||
field.onChange(fileArray2);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</FormControl>
|
||||
{/* Show File Name */}
|
||||
{/* <FormDescription>{`${filenames}`}</FormDescription> */}
|
||||
<FormMessage />
|
||||
</div>
|
||||
{/* {!error ? (
|
||||
<FormDescription className=" text-xs my-1 ">
|
||||
<span>
|
||||
حجم هر فایل حداکثر می بایست{" "}
|
||||
{formatSize(optionsData?.maxFileSize)} مگابایت باشد
|
||||
</span>{" "}
|
||||
و{" "}
|
||||
{`تعداد فایل های پیوست شده می بایست حداکثر ${optionsData?.maxFileCount} تا باشد.`}
|
||||
</FormDescription>
|
||||
) : (
|
||||
<FormMessage />
|
||||
)} */}
|
||||
</FormItem>
|
||||
);
|
||||
}}
|
||||
|
|
|
|||
57
lib/utils.ts
57
lib/utils.ts
|
|
@ -20,7 +20,6 @@ export function convertDate(date: string) {
|
|||
return convertedDate;
|
||||
}
|
||||
|
||||
|
||||
export function convertFullTime(date: string) {
|
||||
const convertedDate = dayjs(date + "Z")
|
||||
.calendar("jalali")
|
||||
|
|
@ -29,29 +28,13 @@ export function convertFullTime(date: string) {
|
|||
return convertedDate;
|
||||
}
|
||||
|
||||
export function Decrypt(encryptedData: string , privateKey: string) {
|
||||
const Key = `-----BEGIN RSA PRIVATE KEY-----
|
||||
MIICXAIBAAKBgQCfmb1aGcWsqJPp+v6e72CPngE6P6FoCZCJESwDTXUXiJiAKshB
|
||||
WZ9nlN+IIpQr6h8gmxi8SWbofPWmSKZgTyU2MddxUanneuywPTTUj4HQ6/DUaDwF
|
||||
0Vr6Fk0x4yi1GKpBzhRFPyOYBWVXKIw23j0l+EWw6+JIpR9Kwu0TezHHFQIDAQAB
|
||||
AoGBAIVxegy3t7vhR6s/HV2uMEJkjgox4banKJ4yKWapgNmocDnZi454bPuiUjP1
|
||||
FYaSPmx4wXnMtap1sL7Tx6iHBEi10KB1AghDZP5wJHz6nm9ugcrGSSWhVJM74x6C
|
||||
ThVztAV/rydaZ6/DLaqsBg5lHlo1DjMKq4sxnqrw/LUMGtOBAkEAzt8bnM1Ij/rF
|
||||
i8oSP3gI0H914vf/0N2xxGGdQvMmJK0oijIm5j8dFDZf02Ft34SFeVJfXyVPapDE
|
||||
vQ6TRw+1HQJBAMWAwvbypbtl65Dn4qCj3oJUmmnjjxm5080idF1S6ufo34p2gHMF
|
||||
qLtWjmnGqjh2Shs7ahnfbLF+cxzgBg7yEFkCQFYGl1/0Sel9uepwpHf6PYgeiPJS
|
||||
ePHtTwBAK3pszH7R9xrI0j5LHpeKKP0o9dzOKgcShxB03nqNKxMHXZ1eXc0CQFhD
|
||||
Ot3xgm9FFQklF/qCQSYtePRpZcIxcZ3zD93E+IW7ZGsUmkLM/KL8A5jx15B0DJou
|
||||
6zO0zXZk9DyrQQnc08ECQGy5ti89HmQ6I6Wqq5ADglCY6YPISsNwScVyvu/WxQsX
|
||||
dEqG/38xFdYTFEekexcHnT/Vt1MW4/j76vBJHYWuAJU=
|
||||
-----END RSA PRIVATE KEY-----`;
|
||||
export function Decrypt(encryptedData: string, privateKey: string) {
|
||||
const decryptor = new JSEncrypt();
|
||||
decryptor.setPrivateKey(privateKey);
|
||||
const decrypted = decryptor.decrypt(encryptedData);
|
||||
return decrypted;
|
||||
}
|
||||
|
||||
|
||||
export function isAuthorized() {
|
||||
const token = getCookie("Sootzani_Token");
|
||||
if (token) {
|
||||
|
|
@ -60,3 +43,41 @@ export function isAuthorized() {
|
|||
false;
|
||||
}
|
||||
}
|
||||
|
||||
export function bytesToMB(bytes: number) {
|
||||
return Math.floor(bytes / (1024 * 1024));
|
||||
}
|
||||
|
||||
export function formatBytes(bytes: number, decimals = 2) {
|
||||
console.log(bytes);
|
||||
if (!+bytes) return "0 Bytes";
|
||||
|
||||
const k = 1024;
|
||||
const dm = decimals < 0 ? 0 : decimals;
|
||||
const sizes = [
|
||||
"Bytes",
|
||||
"KB",
|
||||
"MB",
|
||||
"GiB",
|
||||
"TiB",
|
||||
"PiB",
|
||||
"EiB",
|
||||
"ZiB",
|
||||
"YiB",
|
||||
];
|
||||
// const sizes = [
|
||||
// "Bytes",
|
||||
// "KB",
|
||||
// "MB",
|
||||
// "GiB",
|
||||
// "TiB",
|
||||
// "PiB",
|
||||
// "EiB",
|
||||
// "ZiB",
|
||||
// "YiB",
|
||||
// ];
|
||||
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
|
||||
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,8 +23,8 @@ axios.interceptors.response.use(
|
|||
originalRequest._retry = true;
|
||||
const accessToken = getCookie("Sootzani_Token");
|
||||
const refreshToken = getCookie("Sootzani_Refresh_Token");
|
||||
|
||||
if (accessToken && refreshToken && !isRefreshing) {
|
||||
//accessToken && refreshToken && !isRefreshing
|
||||
if (!isRefreshing) {
|
||||
isRefreshing = true;
|
||||
|
||||
try {
|
||||
|
|
|
|||
|
|
@ -43,3 +43,8 @@ export async function getNewsById(id: number) {
|
|||
const data = await axios.get(`api/Common/News/${id}`);
|
||||
return data.data;
|
||||
}
|
||||
|
||||
export async function getOptions() {
|
||||
const data = await axios.get("/api/Common/Options");
|
||||
return data.data;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue