diff --git a/src/components/master/CreateActivity.jsx b/src/components/master/CreateActivity.jsx index 604adeea..8493c2ac 100644 --- a/src/components/master/CreateActivity.jsx +++ b/src/components/master/CreateActivity.jsx @@ -1,26 +1,30 @@ -import React,{useState,useEffect} from 'react' -import {useFieldArray, useForm } from 'react-hook-form'; -import { z } from 'zod'; -import {zodResolver} from '@hookform/resolvers/zod'; +import React, { useState, useEffect } from "react"; +import { useFieldArray, 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 { MasterRespository } from "../../repositories/MastersRepository"; +import { clearApiCacheKey } from "../../slices/apiCacheSlice"; +import { getCachedData, cacheData } from "../../slices/apiDataManager"; +import showToast from "../../services/toastService"; const schema = z.object({ activityName: z.string().min(1, { message: "Role is required" }), unitOfMeasurement: z.string().min(1, { message: "Description is required" }), checkList: z - .array(z.string().min(1, { message: "Checklist item cannot be empty" })) - .optional(), - }); - -const CreateActivity = ({onClose}) => -{ + .array( + z.object({ + check: z.string().min(1, { message: "Checklist item cannot be empty" }), + isMandatory: z.boolean().default(false), + id: z.any().default(0), + }) + ) + .optional(), +}); + +const CreateActivity = ({ onClose }) => { + const [isLoading, setIsLoading] = useState(false); - const [ isLoading, setIsLoading ] = useState( false ) - const { register, handleSubmit, @@ -34,147 +38,197 @@ const CreateActivity = ({onClose}) => } = useForm({ resolver: zodResolver(schema), defaultValues: { - activityName: '', - unitOfMeasurement: '', + activityName: "", + unitOfMeasurement: "", checkList: [], }, }); - // Setting up field array for checkList (dynamic fields) + const { fields: checkListItems, append, remove, } = useFieldArray({ control, - name: 'checkList', + name: "checkList", }); // Form submission handler - const onSubmit = ( data ) => - { - console.log(data) - setIsLoading(true) - - - MasterRespository.updateActivity(data).then((resp)=>{ - setIsLoading(false) - - const cachedData = getCachedData("Activity"); - const updatedData = [...cachedData, resp?.data]; - cacheData("Activity", updatedData); - showToast("Activity Added successfully.", "success"); + const onSubmit = (data) => { + console.log(data); + setIsLoading(true); - onClose() - }).catch((error)=>{ - showToast(error.message, "error"); - setIsLoading(false) - }) + MasterRespository.createActivity(data) + .then( ( resp ) => + { + + const cachedData = getCachedData("Activity"); + const updatedData = [ ...cachedData, resp?.data ]; + cacheData("Activity", updatedData); + showToast("Activity Successfully Added.", "success"); + setIsLoading(false); + handleClose() + }) + .catch((error) => { + showToast(error.message, "error"); + setIsLoading(false); + }); }; - // Add a new checklist item - const addChecklistItem = () => { - const values = getValues('checkList'); - const lastIndex = checkListItems.length - 1; - - if (checkListItems.length > 0 && (!values?.[lastIndex] || values[lastIndex].trim() === '')) { - setError(`checkList.${lastIndex}`, { - type: 'manual', - message: 'Please fill this checklist item before adding another.', + const addChecklistItem = () => { + const values = getValues("checkList"); + const lastIndex = checkListItems.length - 1; + if ( + checkListItems.length > 0 && + (!values?.[lastIndex] || values[lastIndex].check.trim() === "") + ) { + setError(`checkList.${lastIndex}.check`, { + type: "manual", + message: "Please fill this checklist item before adding another.", }); return; } - - clearErrors(`checkList.${lastIndex}`); // Clear the error if the input is filled. - append(''); // Add a new empty checklist input + clearErrors(`checkList.${lastIndex}.check`); + append({ + id: 0, + check: "", + isMandatory: false, + }); }; - const removeChecklistItem = (index) => { - remove(index); + remove(index); }; - // Handle checklist item input change + const handleChecklistChange = (index, value) => { - setValue(`checkList.${index}`, value); + setValue(`checkList.${index}`, value); }; - const handleClose = () => - { - reset() - onClose() - } + const handleClose = () => { + reset(); + onClose(); + }; return (