diff --git a/src/components/master/CreateWorkCategory.jsx b/src/components/master/CreateWorkCategory.jsx new file mode 100644 index 00000000..06d08041 --- /dev/null +++ b/src/components/master/CreateWorkCategory.jsx @@ -0,0 +1,121 @@ +import React, { useEffect,useState } from 'react' +import { useForm } from 'react-hook-form'; +import { z } from 'zod'; +import { zodResolver } from '@hookform/resolvers/zod'; +import { MasterRespository } from '../../repositories/MastersRepository'; +import { clearApiCacheKey } from '../../slices/apiCacheSlice'; +import { getCachedData,cacheData } from '../../slices/apiDataManager'; +import showToast from '../../services/toastService'; + + +const schema = z.object({ + name: z.string().min(1, { message: "Category name is required" }), + description: z.string().min(1, { message: "Description is required" }) + .max(255, { message: "Description cannot exceed 255 characters" }), +}); + +const CreateWorkCategory = ({onClose}) => { + + const[isLoading,setIsLoading] = useState(false) + const { + register, + handleSubmit, + formState: { errors },reset + + } = useForm({ + resolver: zodResolver(schema), + defaultValues: { + name: "", + description: "", + + }, + }); + + const onSubmit = (data) => { + setIsLoading(true) + // const result = { + // name: data.name, + // description: data.description, + // }; + + MasterRespository.createWorkCategory(data).then((resp)=>{ + setIsLoading(false) + resetForm() + const cachedData = getCachedData("Work Category"); + const updatedData = [...cachedData, resp?.data]; + cacheData("Work Category", updatedData); + showToast("Work Category Added successfully.", "success"); + + onClose() + }).catch((error)=>{ + showToast(error?.response?.data?.message, "error"); + setIsLoading(false) + }) + + + }; + const resetForm = () => { + reset({ + name: "", + description: "" + }); + setDescriptionLength(0); + } + + useEffect(()=>{ + return ()=>resetForm() + },[]) + + const [descriptionLength, setDescriptionLength] = useState(0); + const maxDescriptionLength = 255; + return (<> +
+ +
+ + + {errors.name &&

{errors.name.message}

} +
+
+ + +
+ {maxDescriptionLength - descriptionLength} characters left +
+ {errors.description && ( +

{errors.description.message}

+ )} +
+ +
+ + +
+ +
+ + + ) +} + +export default CreateWorkCategory; \ No newline at end of file diff --git a/src/components/master/EditJobRole.jsx b/src/components/master/EditJobRole.jsx index 55d9228c..c13fc0d0 100644 --- a/src/components/master/EditJobRole.jsx +++ b/src/components/master/EditJobRole.jsx @@ -56,7 +56,6 @@ const EditJobRole = ({data,onClose}) => { onClose() }).catch((error)=>{ - showToast("JobRole added successfully.", "success"); showToast(error.message, "error") setIsLoading(false) }) diff --git a/src/components/master/EditWorkCategory.jsx b/src/components/master/EditWorkCategory.jsx new file mode 100644 index 00000000..124f2d65 --- /dev/null +++ b/src/components/master/EditWorkCategory.jsx @@ -0,0 +1,128 @@ +import React, { useEffect,useState } from 'react' +import { useForm ,Controller} from 'react-hook-form'; +import { set, z } from 'zod'; +import { zodResolver } from '@hookform/resolvers/zod'; +import { MasterRespository } from '../../repositories/MastersRepository'; +import { cacheData,getCachedData } from '../../slices/apiDataManager'; +import showToast from '../../services/toastService'; + + + +const schema = z.object({ + name: z.string().min(1, { message: "Work Category is required" }), + description: z.string().min(1, { message: "Description is required" }) + .max(255, { message: "Description cannot exceed 255 characters" }), +}); + + + +const EditWorkCategory = ({data,onClose}) => { + + const[isLoading,setIsLoading] = useState(false) + const { + register, + handleSubmit, + formState: { errors } ,reset + } = useForm({ + resolver: zodResolver(schema), + defaultValues: { + name: data?.name || "", + description:data?.description || "", + + }, + }); + + const onSubmit = (formdata) => { + setIsLoading(true) + const result = { + id:data?.id, + name: formdata?.name, + description: formdata.description, + }; + + + + MasterRespository.updateWorkCategory(data?.id,result).then((resp)=>{ + setIsLoading(false) + showToast("Work Category Update successfully.", "success"); + const cachedData = getCachedData("Work Category"); + if (cachedData) { + + const updatedData = cachedData.map((category) => + category.id === data?.id ? { ...category, ...resp.data } : category + ); + cacheData("Work Category", updatedData); + } + + onClose() + }).catch((error)=>{ + showToast(error?.response?.data?.message, "error") + setIsLoading(false) + }) + + }; + + + useEffect(() => { + reset({ + name: data?.name, + description: data?.description + }); + setDescriptionLength(data?.description?.length || 0); + }, [data, reset]); + + const [descriptionLength, setDescriptionLength] = useState(data?.description?.length || 0); + const maxDescriptionLength = 255; + + + return (<> +
+
+ + + {errors.name &&

{errors.name.message}

} +
+
+ + +
+ {maxDescriptionLength - descriptionLength} characters left +
+ {errors.description && ( +

{errors.description.message}

+ )} + +
+ +
+ + +
+ +
+ + + ) +} + +export default EditWorkCategory; \ No newline at end of file diff --git a/src/components/master/MasterModal.jsx b/src/components/master/MasterModal.jsx index 130873f1..056451a4 100644 --- a/src/components/master/MasterModal.jsx +++ b/src/components/master/MasterModal.jsx @@ -11,6 +11,8 @@ import ConfirmModal from "../common/ConfirmModal"; import {MasterRespository} from "../../repositories/MastersRepository"; import {cacheData, getCachedData} from "../../slices/apiDataManager"; import showToast from "../../services/toastService"; +import CreateWorkCategory from "./CreateWorkCategory"; +import EditWorkCategory from "./EditWorkCategory"; const MasterModal = ({ modaldata, closeModal }) => { @@ -117,6 +119,12 @@ const MasterModal = ({ modaldata, closeModal }) => { {modaldata.modalType === "Edit-Activity" && ( )} + {modaldata.modalType === "Work Category" && ( + + )} + {modaldata.modalType === "Edit-Work Category" && ( + + )} diff --git a/src/data/masters.js b/src/data/masters.js index c1036ed7..0f302184 100644 --- a/src/data/masters.js +++ b/src/data/masters.js @@ -1,5 +1,5 @@ // it important ------ -export const mastersList = [ {id: 1, name: "Application Role"}, {id: 2, name: "Job Role"}, {id: 3, name: "Activity"} ] +export const mastersList = [ {id: 1, name: "Application Role"}, {id: 2, name: "Job Role"}, {id: 3, name: "Activity"},{id: 4, name:"Work Category"} ] // ------------------- export const dailyTask = [ diff --git a/src/hooks/masterHook/useMaster.js b/src/hooks/masterHook/useMaster.js index 6bbb6d0e..33e284cc 100644 --- a/src/hooks/masterHook/useMaster.js +++ b/src/hooks/masterHook/useMaster.js @@ -47,6 +47,10 @@ const useMaster = (isMa) => { response = await MasterRespository.getActivites(); response = response.data break; + case "Work Category": + response = await MasterRespository.getWorkCategory(); + response = response.data + break; case "Status": response = [{description: null,featurePermission: null,id: "02dd4761-363c-49ed-8851-3d2489a3e98d",status:"status 1"},{description: null,featurePermission: null,id: "03dy9761-363c-49ed-8851-3d2489a3e98d",status:"status 2"},{description: null,featurePermission: null,id: "03dy7761-263c-49ed-8851-3d2489a3e98d",status:"Status 3"}]; break; diff --git a/src/pages/master/MasterTable.jsx b/src/pages/master/MasterTable.jsx index bb380872..6f92be63 100644 --- a/src/pages/master/MasterTable.jsx +++ b/src/pages/master/MasterTable.jsx @@ -2,6 +2,7 @@ import React, { useEffect, useState } from "react"; import { useSelector } from "react-redux"; import { useHasUserPermission } from "../../hooks/useHasUserPermission"; import { MANAGE_MASTER } from "../../utils/constants"; +import showToast from "../../services/toastService"; const MasterTable = ({ data, columns, loading, handleModalData }) => { const hasMasterPermission = useHasUserPermission(MANAGE_MASTER); @@ -58,6 +59,12 @@ const MasterTable = ({ data, columns, loading, handleModalData }) => { : col.label, })); + const handleSystemDefined = (message) =>{ + if(message){ + showToast(`The system-defined item ${selectedMaster} cannot be ${message}.`) + } + } + return (
{loading ? ( @@ -108,8 +115,28 @@ const MasterTable = ({ data, columns, loading, handleModalData }) => { ))} - {selectedMaster === "Application Role" && item?.isSystem ? ( - "--" + {(selectedMaster === "Application Role" || selectedMaster === "Work Category") && item?.isSystem ? ( + <> + + + + ) : ( <>