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'; import {useCreateContactCategory} from '../../hooks/masterHook/useMaster'; import Label from '../common/Label'; 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 CreateContactCategory = ({ onClose }) => { const { register, handleSubmit, formState: { errors }, reset, } = useForm({ resolver: zodResolver(schema), defaultValues: { name: "", description: "", }, }); const [descriptionLength, setDescriptionLength] = useState(0); const maxDescriptionLength = 255; const { mutate: createContactCategory, isPending: isLoading } = useCreateContactCategory(() => onClose?.()); const onSubmit = (payload) => { createContactCategory(payload); }; // const onSubmit = (data) => { // setIsLoading(true) // MasterRespository.createContactCategory(data).then((resp)=>{ // setIsLoading(false) // resetForm() // const cachedData = getCachedData("Contact Category"); // const updatedData = [...cachedData, resp?.data]; // cacheData("Contact Category", updatedData); // showToast("Contact 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(); }, []); return (<>
{errors.name &&

{errors.name.message}

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

{errors.description.message}

)}
) } export default CreateContactCategory;