import { zodResolver } from "@hookform/resolvers/zod"; import React, { useEffect, useState } from "react"; import { useForm, FormProvider } from "react-hook-form"; import { defaultDocumentValues, DocumentPayloadSchema } from "./DocumentSchema"; import Label from "../common/Label"; import { useDocumentCategories, useDocumentTypes, } from "../../hooks/masterHook/useMaster"; import TagInput from "../common/TagInput"; import { useUploadDocument } from "../../hooks/useDocument"; import showToast from "../../services/toastService"; const toBase64 = (file) => new Promise((resolve, reject) => { const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = () => resolve(reader.result); reader.onerror = (err) => reject(err); }); const NewDocument = ({closeModal,Document_Entity,Entity}) => { const [selectedType, setSelectedType] = useState(null); const [selectedCategory, setSelectedCategory] = useState(null); const [schema, setSchema] = useState(() => DocumentPayloadSchema({})); const methods = useForm({ resolver: zodResolver(schema), defaultValues: defaultDocumentValues, }); const { register, handleSubmit, watch, setValue, reset, formState: { errors }, } = methods; const { mutate: UploadDocument, isPending } = useUploadDocument(() => { showToast("Document Uploaded Successfully", "success"); closeModal(); }); const onSubmit = (data) => { const DocumentPayload = { ...data, entityId: Entity }; UploadDocument(DocumentPayload); }; const file = watch("attachment"); const documentTypeId = watch("documentTypeId"); // This hooks calling api base Entity(Employee) and Category const { DocumentCategories, isLoading } = useDocumentCategories( Document_Entity ); const categoryId = watch("documentCategoryId"); const { DocumentTypes, isLoading: isTypeLoading } = useDocumentTypes( categoryId || null ); // Update schema whenever document type changes useEffect(() => { if (!documentTypeId) return; const type = DocumentTypes?.find((t) => t.id === documentTypeId); setSelectedType(type || null); if (type) { const newSchema = DocumentPayloadSchema({ isMandatory: type.isMandatory ?? false, regexExpression: type.regexExpression ?? null, allowedContentType: type.allowedContentType ?? [ "application/pdf", "image/jpeg", "image/png", ], maxSizeAllowedInMB: type.maxSizeAllowedInMB ?? 25, }); setSchema(() => newSchema); reset({ ...methods.getValues() }, { keepValues: true }); } }, [documentTypeId, DocumentTypes, reset]); // File Upload const onFileChange = async (e) => { const uploaded = e.target.files[0]; if (!uploaded) return; const base64Data = await toBase64(uploaded); const parsedFile = { fileName: uploaded.name, base64Data, contentType: uploaded.type, fileSize: uploaded.size, description: "", isActive: true, }; setValue("attachment", parsedFile, { shouldDirty: true, shouldValidate: true, }); }; const removeFile = () => { setValue("attachment", null, { shouldDirty: true, shouldValidate: true, }); }; // build dynamic file accept string const fileAccept = selectedType?.allowedContentType ?.split(",") .map((t) => t === "application/pdf" ? ".pdf" : t === "image/jpeg" ? ".jpg,.jpeg" : t === "image/png" ? ".png" : "" ) .join(",") || ""; return (
Upload New Document