From f2b1eee5ad59baa21a92dcb9ccf699c15a5ef81f Mon Sep 17 00:00:00 2001 From: Pramod Mahajan Date: Mon, 19 May 2025 22:28:26 +0530 Subject: [PATCH] created new component for EditContactTag --- src/components/master/EditContactTag.jsx | 126 +++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 src/components/master/EditContactTag.jsx diff --git a/src/components/master/EditContactTag.jsx b/src/components/master/EditContactTag.jsx new file mode 100644 index 00000000..e40d7e61 --- /dev/null +++ b/src/components/master/EditContactTag.jsx @@ -0,0 +1,126 @@ +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: "Tag name is required" }), + description: z.string().min(1, { message: "Description is required" }) + .max(255, { message: "Description cannot exceed 255 characters" }), +}); + +const EditContactTag= ({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.updateContactTag(data?.id,result).then((resp)=>{ + setIsLoading(false) + showToast("Contact Tag Updated successfully.", "success"); + const cachedData = getCachedData("Contact Tag"); + if (cachedData) { + + const updatedData = cachedData.map((category) => + category.id === data?.id ? { ...category, ...resp.data } : category + ); + cacheData("Contact Tag", updatedData); + } + + 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 EditContactTag; \ No newline at end of file