diff --git a/src/components/master/CreateContactTag.jsx b/src/components/master/CreateContactTag.jsx new file mode 100644 index 00000000..dc92b789 --- /dev/null +++ b/src/components/master/CreateContactTag.jsx @@ -0,0 +1,114 @@ +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 CreateContactTag = ({onClose}) => { + + const[isLoading,setIsLoading] = useState(false) + const { + register, + handleSubmit, + formState: { errors },reset + + } = useForm({ + resolver: zodResolver(schema), + defaultValues: { + name: "", + description: "", + + }, + }); + + const onSubmit = (data) => { + setIsLoading(true) + MasterRespository.createContactTag(data).then((resp)=>{ + setIsLoading(false) + resetForm() + debugger + const cachedData = getCachedData("Contact Tag"); + const updatedData = [...cachedData, resp?.data]; + cacheData("Contact Tag", updatedData); + showToast("Contact Tag Added successfully.", "success"); + console.log(getCachedData("Contact Tag")) + 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 CreateContactTag; \ No newline at end of file