diff --git a/src/components/master/ManageDocumentCategory.jsx b/src/components/master/ManageDocumentCategory.jsx new file mode 100644 index 00000000..8387b1cd --- /dev/null +++ b/src/components/master/ManageDocumentCategory.jsx @@ -0,0 +1,156 @@ +import React, { useEffect } from "react"; +import { useForm, FormProvider } from "react-hook-form"; +import { z } from "zod"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { useFeatures } from "../../hooks/useMasterRole"; +import { DOCUMENTS_ENTITIES, EXPENSE_MANAGEMENT } from "../../utils/constants"; +import { + useCreateDocumentCatgory, + useUpdateDocumentCategory, +} from "../../hooks/masterHook/useMaster"; + +export const Document_Entity = Object.entries(DOCUMENTS_ENTITIES).map( + ([key, value]) => ({ key, value }) +); + +const ExpenseStatusSchema = z.object({ + name: z.string().min(1, { message: "Name is required" }), + description: z.string().min(1, { message: "Description is required" }), + entityTypeId: z.string().min(1, { message: "Entity is required" }), +}); + +const ManageDocumentCategory = ({ data, onClose }) => { + const methods = useForm({ + resolver: zodResolver(ExpenseStatusSchema), + defaultValues: { + name: "", + description: "", + entityTypeId: "", + }, + }); + + const { + register, + handleSubmit, + reset, + formState: { errors }, + } = methods; + + const { masterFeatures, loading } = useFeatures(); + + const ExpenseFeature = masterFeatures?.find( + (appfeature) => appfeature.id === EXPENSE_MANAGEMENT + ); + + const { mutate: CreateDocumentCategory, isPending } = + useCreateDocumentCatgory(() => onClose?.()); + const { mutate: UpdateDocumentCategory, isPending: Updating } = + useUpdateDocumentCategory(() => onClose?.()); + + const onSubmit = (payload) => { + if (data) { + UpdateDocumentCategory({ + id: data.id, + payload: { ...payload, id: data.id }, + }); + } else { + CreateDocumentCategory(payload); + } + }; + + useEffect(() => { + if (data) { + reset({ + name: data.name ?? "", + description: data.description ?? "", + entityTypeId: data.entityTypeId ?? "", + }); + } + }, [data, reset]); + return ( + + {loading ? ( + Loading... + ) : ( + + + + {data ? "Update Document Category" : "Add Document Category"} + + + + + + Category Name + + {errors.name && ( + {errors.name.message} + )} + + + + Select Entity + + Select entity + {Document_Entity.map((entity) => ( + + {entity.key} + + ))} + + {errors.entityTypeId && ( + {errors.entityTypeId.message} + )} + + + + Description + + {errors.description && ( + {errors.description.message} + )} + + + + + {isPending || Updating + ? "Please Wait..." + : data + ? "Update" + : "Submit"} + + + Cancel + + + + + )} + + ); +}; + +export default ManageDocumentCategory; diff --git a/src/components/master/MasterModal.jsx b/src/components/master/MasterModal.jsx index 44282be8..3f644420 100644 --- a/src/components/master/MasterModal.jsx +++ b/src/components/master/MasterModal.jsx @@ -20,6 +20,7 @@ import { useDeleteMasterItem } from "../../hooks/masterHook/useMaster"; import ManageExpenseType from "./ManageExpenseType"; import ManagePaymentMode from "./ManagePaymentMode"; import ManageExpenseStatus from "./ManageExpenseStatus"; +import ManageDocumentCategory from "./ManageDocumentCategory"; const MasterModal = ({ modaldata, closeModal }) => { @@ -96,7 +97,9 @@ const MasterModal = ({ modaldata, closeModal }) => { "Payment Mode":, "Edit-Payment Mode":, "Expense Status":, - "Edit-Expense Status": + "Edit-Expense Status":, + "Document Category":, + "Edit-Document Category": }; return modalComponents[modalType] || null; diff --git a/src/hooks/masterHook/useMaster.js b/src/hooks/masterHook/useMaster.js index 74a0108d..c7d23bc9 100644 --- a/src/hooks/masterHook/useMaster.js +++ b/src/hooks/masterHook/useMaster.js @@ -688,6 +688,7 @@ export const useUpdatePaymentMode = (onSuccessCallback)=>{ } }) } + // -------------------Expense Status---------------------------------- export const useCreateExpenseStatus =(onSuccessCallback)=>{ const queryClient = useQueryClient(); @@ -731,6 +732,53 @@ export const useUpdateExpenseStatus = (onSuccessCallback)=>{ } }) } + + + +// --------------------Document-Category-------------------------------- +export const useCreateDocumentCatgory =(onSuccessCallback)=>{ + const queryClient = useQueryClient(); + + return useMutation( { + mutationFn: async ( payload ) => + { + const resp = await MasterRespository.createDocumenyCategory(payload); + return resp.data; + }, + onSuccess: ( data ) => + { + queryClient.invalidateQueries( {queryKey:[ "masterData", "Document Category" ]} ) + showToast( "Document Category added successfully", "success" ); + if(onSuccessCallback) onSuccessCallback(data) + }, + onError: ( error ) => + { + showToast(error.message || "Something went wrong", "error"); + } + }) +} + +export const useUpdateDocumentCategory =(onSuccessCallback)=>{ + const queryClient = useQueryClient(); + + return useMutation( { + mutationFn: async ( {id,payload} ) => + { + const resp = await MasterRespository.updateDocumentCategory(id,payload); + return resp.data; + }, + onSuccess: ( data ) => + { + queryClient.invalidateQueries( {queryKey:[ "masterData", "Document Category" ]} ) + showToast( "Document Category Updated successfully", "success" ); + if(onSuccessCallback) onSuccessCallback(data) + }, + onError: ( error ) => + { + showToast(error.message || "Something went wrong", "error"); + } + }) +} // -Delete Master -------- export const useDeleteMasterItem = () => { const queryClient = useQueryClient(); diff --git a/src/repositories/MastersRepository.jsx b/src/repositories/MastersRepository.jsx index f8a885aa..2ac3337e 100644 --- a/src/repositories/MastersRepository.jsx +++ b/src/repositories/MastersRepository.jsx @@ -18,7 +18,7 @@ export const RolesRepository = { }; export const MasterRespository = { - getMasterMenus:()=>api.get("/api/AppMenu/get/master-list"), + getMasterMenus: () => api.get("/api/AppMenu/get/master-list"), getRoles: () => api.get("/api/roles"), createRole: (data) => api.post("/api/roles", data), @@ -58,7 +58,7 @@ export const MasterRespository = { createContactCategory: (data) => api.post(`/api/master/contact-category`, data), updateContactCategory: (id, data) => - api.post(`/api/master/contact-category/edit/${id}`, data), + api.put(`/api/master/contact-category/edit/${id}`, data), getContactTag: () => api.get(`/api/master/contact-tags`), createContactTag: (data) => api.post(`/api/master/contact-tag`, data), @@ -82,11 +82,25 @@ export const MasterRespository = { updateExepnseStatus: (id, data) => api.put(`/api/Master/expenses-status/edit/${id}`, data), + getDocumentCategories: (entityType) => + api.get( + `/api/Master/document-category/list${ + entityType ? `?entityTypeId=${entityType}` : "" + }` + ), + createDocumenyCategory: (data) => + api.post(`/api/Master/document-category`, data), + updateDocumentCategory: (id, data) => + api.put(`/api/Master/document-category/edit/${id}`, data), - getDocumentCategories: (entityType) => - api.get(`/api/Master/document-category/list${entityType ? `?entityTypeId=${entityType}` : ""}`), - -getDocumentTypes: (category) => - api.get(`/api/Master/document-type/list${category ? `?documentCategoryId=${category}` : ""}`), + getDocumentTypes: (category) => + api.get( + `/api/Master/document-type/list${ + category ? `?documentCategoryId=${category}` : "" + }` + ), + createDocumentType: (data) => api.post(`/api/Master/document-type`, data), + updateDocumentType: (id, data) => + api.put(`/api/Master/document-type/edit/${id}`, data), };
+ {data ? "Update Document Category" : "Add Document Category"} +
{errors.name.message}
{errors.entityTypeId.message}
{errors.description.message}