diff --git a/src/components/Documents/ManageDocument.jsx b/src/components/Documents/ManageDocument.jsx index 6ff050d4..ba8d8621 100644 --- a/src/components/Documents/ManageDocument.jsx +++ b/src/components/Documents/ManageDocument.jsx @@ -17,6 +17,8 @@ import { import showToast from "../../services/toastService"; import { useDocumentContext } from "./Documents"; import { isPending } from "@reduxjs/toolkit"; +import { AppFormController, AppFormProvider } from "../../hooks/appHooks/useAppForm"; +import SelectField from "../common/Forms/SelectField"; const toBase64 = (file) => new Promise((resolve, reject) => { @@ -72,9 +74,12 @@ const ManageDocument = ({ closeModal, Document_Entity, Entity }) => { handleSubmit, watch, setValue, + control, reset, formState: { errors }, } = methods; + + const { mutate: UploadDocument, isPending: isUploading } = useUploadDocument( () => { showToast("Document Uploaded Successfully", "success"); @@ -88,33 +93,33 @@ const ManageDocument = ({ closeModal, Document_Entity, Entity }) => { } ); - + const onSubmit = (data) => { - const normalizeAttachment = (attachment) => { - if (!attachment) return null; - return { - ...attachment, - fileSize: Math.ceil(attachment.fileSize / 1024), + const normalizeAttachment = (attachment) => { + if (!attachment) return null; + return { + ...attachment, + fileSize: Math.ceil(attachment.fileSize / 1024), + }; }; - }; - const payload = { - ...data, - attachment: normalizeAttachment(data.attachment), - }; - - if (ManageDoc?.document) { - const DocumentPayload = { - ...payload, - id: DocData.id, - tags: MergedTagsWithExistenStatus(data?.tags, DocData?.tags), + const payload = { + ...data, + attachment: normalizeAttachment(data.attachment), }; - UpdateDocument({ documentId: DocData?.id, DocumentPayload }); - } else { - const DocumentPayload = { ...payload, entityId: Entity }; - UploadDocument(DocumentPayload); - } -}; + + if (ManageDoc?.document) { + const DocumentPayload = { + ...payload, + id: DocData.id, + tags: MergedTagsWithExistenStatus(data?.tags, DocData?.tags), + }; + UpdateDocument({ documentId: DocData?.id, DocumentPayload }); + } else { + const DocumentPayload = { ...payload, entityId: Entity }; + UploadDocument(DocumentPayload); + } + }; const { data: DocData, @@ -134,7 +139,7 @@ const ManageDocument = ({ closeModal, Document_Entity, Entity }) => { const { DocumentTypes, isLoading: isTypeLoading } = useDocumentTypes( categoryId || null ); - const {data:DocumentTags} = useDocumentTags() + const { data: DocumentTags } = useDocumentTags() // Update schema whenever document type changes useEffect(() => { @@ -144,7 +149,7 @@ const ManageDocument = ({ closeModal, Document_Entity, Entity }) => { (t) => String(t.id) === String(documentTypeId) ); if (!type) return; - setSelectedType(type) + setSelectedType(type) const newSchema = DocumentPayloadSchema({ isMandatory: type.isMandatory ?? false, regexExpression: type.regexExpression ?? null, @@ -200,10 +205,10 @@ const ManageDocument = ({ closeModal, Document_Entity, Entity }) => { t === "application/pdf" ? ".pdf" : t === "image/jpeg" - ? ".jpg,.jpeg" - : t === "image/png" - ? ".png" - : "" + ? ".jpg,.jpeg" + : t === "image/png" + ? ".png" + : "" ) .join(",") || ""; @@ -231,200 +236,209 @@ const ManageDocument = ({ closeModal, Document_Entity, Entity }) => { const isPending = isUploading || isUpdating; return ( -
Upload New Document
-