allow to add and update document category inside master
This commit is contained in:
parent
e2de5eba40
commit
e007c0e8da
156
src/components/master/ManageDocumentCategory.jsx
Normal file
156
src/components/master/ManageDocumentCategory.jsx
Normal file
@ -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 (
|
||||
<FormProvider {...methods}>
|
||||
{loading ? (
|
||||
<div>Loading...</div>
|
||||
) : (
|
||||
<div>
|
||||
<div>
|
||||
<p className="fw-semibold">
|
||||
{data ? "Update Document Category" : "Add Document Category"}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<form
|
||||
className="row g-2 text-start"
|
||||
onSubmit={handleSubmit(onSubmit)}
|
||||
>
|
||||
<div className="col-12">
|
||||
<label className="form-label">Category Name</label>
|
||||
<input
|
||||
type="text"
|
||||
{...register("name")}
|
||||
className={`form-control form-control-sm `}
|
||||
/>
|
||||
{errors.name && (
|
||||
<p className="danger-text">{errors.name.message}</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="col-12">
|
||||
<label className="form-label">Select Entity</label>
|
||||
<select
|
||||
className="form-select form-select-sm"
|
||||
{...register("entityTypeId")}
|
||||
>
|
||||
<option value="" disabled>Select entity</option>
|
||||
{Document_Entity.map((entity) => (
|
||||
<option key={entity.key} value={entity.value}>
|
||||
{entity.key}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
{errors.entityTypeId && (
|
||||
<p className="danger-text">{errors.entityTypeId.message}</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="col-12">
|
||||
<label className="form-label">Description</label>
|
||||
<textarea
|
||||
rows="3"
|
||||
{...register("description")}
|
||||
className={`form-control form-control-sm`}
|
||||
/>
|
||||
{errors.description && (
|
||||
<p className="danger-text">{errors.description.message}</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="col-12 text-center">
|
||||
<button
|
||||
type="submit"
|
||||
className="btn btn-sm btn-primary me-3"
|
||||
disabled={isPending || Updating}
|
||||
>
|
||||
{isPending || Updating
|
||||
? "Please Wait..."
|
||||
: data
|
||||
? "Update"
|
||||
: "Submit"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-sm btn-secondary"
|
||||
onClick={onClose}
|
||||
disabled={isPending || Updating}
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
)}
|
||||
</FormProvider>
|
||||
);
|
||||
};
|
||||
|
||||
export default ManageDocumentCategory;
|
@ -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":<ManagePaymentMode onClose={closeModal}/>,
|
||||
"Edit-Payment Mode":<ManagePaymentMode data={item} onClose={closeModal}/>,
|
||||
"Expense Status":<ManageExpenseStatus onClose={closeModal}/>,
|
||||
"Edit-Expense Status":<ManageExpenseStatus data={item} onClose={closeModal}/>
|
||||
"Edit-Expense Status":<ManageExpenseStatus data={item} onClose={closeModal}/>,
|
||||
"Document Category":<ManageDocumentCategory onClose={closeModal}/>,
|
||||
"Edit-Document Category":<ManageDocumentCategory data={item} onClose={closeModal}/>
|
||||
};
|
||||
|
||||
return modalComponents[modalType] || null;
|
||||
|
@ -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();
|
||||
|
@ -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),
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user