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 (<> +
+ + > + ) +} + +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 (<> + + + > + ) +} + +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" && (