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

- -
- {/* Category */} -
- - - {errors.documentCategoryId && ( -
- {errors.documentCategoryId.message} -
- )} -
- - {/* Type */} - {categoryId && ( -
- - - {errors.documentTypeId && ( -
- {errors.documentTypeId.message} -
+ /> + + {errors.documentCategoryId && ( + + {errors.documentCategoryId.message} + )}
- )} - - {/* Document ID */} -
- - - {errors.documentId && ( -
{errors.documentId.message}
- )} -
- {/* Document Name */} -
- - - {errors.name && ( -
{errors.name.message}
- )} -
- - - - {/* Upload */} -
-
- - -
document.getElementById("attachment").click()} - > - - - Click to select or click here to browse - - - ({selectedType?.allowedContentType || "PDF/JPG/PNG"}, max{" "} - {selectedType?.maxSizeAllowedInMB ?? 25}MB) - - - { - onFileChange(e); - e.target.value = ""; // reset input - }} + {/* Type */} + {categoryId && ( +
+ ( + + )} /> + + {errors.documentTypeId && ( + + {errors.documentTypeId.message} + + )}
- {errors.attachment && ( - - {errors.attachment.message - ? errors.attachment.message - : errors.attachment.fileName?.message || + )} + + + {/* Document ID */} +
+ + + {errors.documentId && ( +
{errors.documentId.message}
+ )} +
+ + {/* Document Name */} +
+ + + {errors.name && ( +
{errors.name.message}
+ )} +
+ + + + {/* Upload */} +
+
+ + +
document.getElementById("attachment").click()} + > + + + Click to select or click here to browse + + + ({selectedType?.allowedContentType || "PDF/JPG/PNG"}, max{" "} + {selectedType?.maxSizeAllowedInMB ?? 25}MB) + + + { + onFileChange(e); + e.target.value = ""; // reset input + }} + /> +
+ + {errors.attachment && ( + + {errors.attachment.message + ? errors.attachment.message + : errors.attachment.fileName?.message || errors.attachment.base64Data?.message || errors.attachment.contentType?.message || errors.attachment.fileSize?.message} - - )} +
+ )} - {file?.base64Data && ( -
-
- - {file.fileName} - - - {(file.fileSize / 1024).toFixed(1)} KB - + {file?.base64Data && ( +
+
+ + {file.fileName} + + + {(file.fileSize / 1024).toFixed(1)} KB + +
+
- -
+ )} +
+
+
+ + {errors.tags && ( + {errors.tags.message} )}
-
-
- - {errors.tags && ( - {errors.tags.message} - )} -
- {/* Description */} -
- - - {errors.description && ( -
{errors.description.message}
- )} -
+ {/* Description */} +
+ + + {errors.description && ( +
{errors.description.message}
+ )} +
- {/* Buttons */} -
- - -
- - -
+ {/* Buttons */} +
+ + +
+ + +
+ ); }; diff --git a/src/components/Employee/ManageEmployee.jsx b/src/components/Employee/ManageEmployee.jsx index 53e294d4..f81d6c60 100644 --- a/src/components/Employee/ManageEmployee.jsx +++ b/src/components/Employee/ManageEmployee.jsx @@ -517,39 +517,35 @@ const ManageEmployee = ({ employeeId, onClosed }) => {
-
- -
- -
+
+ ( + + a?.name?.localeCompare(b?.name) + )} + placeholder="Select Role" + labelKeyKey="name" + valueKeyKey="id" + value={field.value} + onChange={field.onChange} + className="m-0" + /> + )} + /> + {errors.jobRoleId && ( -
+
{errors.jobRoleId.message}
)}
-
+ +
@@ -570,7 +566,7 @@ const ManageEmployee = ({ employeeId, onClosed }) => {
)}
-
+
diff --git a/src/components/Project/ProjectPermission.jsx b/src/components/Project/ProjectPermission.jsx index a043603e..d3bf7238 100644 --- a/src/components/Project/ProjectPermission.jsx +++ b/src/components/Project/ProjectPermission.jsx @@ -10,6 +10,8 @@ import { useForm, Controller } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import showToast from "../../services/toastService"; +import { AppFormController } from "../../hooks/appHooks/useAppForm"; +import SelectField from "../common/Forms/SelectField"; export const ProjectPermissionSchema = z.object({ employeeId: z.string().min(1, "Employee is required"), @@ -46,26 +48,26 @@ const ProjectPermission = () => { ); useEffect(() => { - if (!selectedEmployee) return; + if (!selectedEmployee) return; - const enabledPerms = + const enabledPerms = + selectedEmpPermissions?.permissions + ?.filter((perm) => perm.isEnabled) + ?.map((perm) => perm.id) || []; + + setValue("selectedPermissions", enabledPerms, { shouldValidate: true }); + }, [selectedEmpPermissions, setValue, selectedEmployee]); + + const selectedPermissions = watch("selectedPermissions") || []; + + const existingEnabledIds = selectedEmpPermissions?.permissions - ?.filter((perm) => perm.isEnabled) - ?.map((perm) => perm.id) || []; + ?.filter((p) => p.isEnabled) + ?.map((p) => p.id) || []; - setValue("selectedPermissions", enabledPerms, { shouldValidate: true }); -}, [selectedEmpPermissions, setValue, selectedEmployee]); - -const selectedPermissions = watch("selectedPermissions") || []; - -const existingEnabledIds = - selectedEmpPermissions?.permissions - ?.filter((p) => p.isEnabled) - ?.map((p) => p.id) || []; - -const hasChanges = - selectedPermissions.length !== existingEnabledIds.length || - selectedPermissions.some((id) => !existingEnabledIds.includes(id)); + const hasChanges = + selectedPermissions.length !== existingEnabledIds.length || + selectedPermissions.some((id) => !existingEnabledIds.includes(id)); const { mutate: updatePermission, isPending } = useUpdateProjectLevelEmployeePermission(); @@ -115,35 +117,42 @@ const hasChanges =
-
+ {/*
-
-
- {" "} - + /> + {errors.employeeId && (
{errors.employeeId.message} @@ -152,6 +161,7 @@ const hasChanges =
+
{hasChanges && (