diff --git a/src/components/master/CreateActivity.jsx b/src/components/master/CreateActivity.jsx index bb207b53..604adeea 100644 --- a/src/components/master/CreateActivity.jsx +++ b/src/components/master/CreateActivity.jsx @@ -16,59 +16,11 @@ const schema = z.object({ .optional(), }); -const CreateActivity = () => +const CreateActivity = ({onClose}) => { const [ isLoading, setIsLoading ] = useState( false ) - - // const { - // register, - // handleSubmit, - // formState: { errors },reset - // } = useForm({ - // resolver: zodResolver(schema), - // defaultValues: { - // activityName: "", - // unitOfMeasurement: "", - // checkList: [''] - // }, - // }); - - // const onSubmit = (data) => { - // setIsLoading(true) - // const result = { - // name: data.activityName, - // description: data.unitOfMeasurement, - // }; - // console.log( result ) - // reset() - // MasterRespository.createJobRole(result).then((resp)=>{ - // setIsLoading(false) - // resetForm() - // const cachedData = getCachedData("Job Role"); - // const updatedData = [...cachedData, resp?.data]; - // cacheData("Job Role", updatedData); - // showToast("JobRole Added successfully.", "success"); - - // onClose() - // }).catch((error)=>{ - // showToast(error.message, "error"); - // setIsLoading(false) - // }) - - // }; - // const resetForm =()=>{ - // reset({ - // activityName:"", - // unitOfMeasurement:"" - // }) - // } - - // useEffect(()=>{ - // return ()=>resetForm() - // }, [] ) - const { register, handleSubmit, @@ -99,8 +51,25 @@ const CreateActivity = () => }); // Form submission handler - const onSubmit = (data) => { - console.log('Submitted:', data); + 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"); + + onClose() + }).catch((error)=>{ + showToast(error.message, "error"); + setIsLoading(false) + }) }; // Add a new checklist item @@ -130,6 +99,12 @@ const CreateActivity = () => const handleChecklistChange = (index, value) => { setValue(`checkList.${index}`, value); }; + + const handleClose = () => + { + reset() + onClose() + } return (
@@ -187,12 +162,12 @@ const CreateActivity = () =>
diff --git a/src/components/master/EditActivity.jsx b/src/components/master/EditActivity.jsx index de255d22..bb829bf9 100644 --- a/src/components/master/EditActivity.jsx +++ b/src/components/master/EditActivity.jsx @@ -2,6 +2,10 @@ 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 axios from 'axios'; +import showToast from '../../services/toastService'; +import {cacheData, getCachedData} from '../../slices/apiDataManager'; // Zod Schema for validation const schema = z.object({ @@ -12,16 +16,8 @@ const schema = z.object({ .optional(), } ); -// for demo data -let initialData = { - activityName: "Item", - unitOfMeasurement: "Item-2", - checkList: [ - 'item 3', - 'Item 4' - ] -} -const EditActivity = ({onClose}) => { +let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOlsiOGFmNjFlNDgtYzRkMy00MTYzLWI4NjAtMmEyZWNiNjQ3NDZiIiwiYjUxMzcwOWEtYmZiZS00OTM1LTlmNWMtOGVjN2IwMzFjNTFlIl0sInN1YiI6InZpa2FzQG1hcmNvYWlvdC5jb20iLCJUZW5hbnRJZCI6IjIiLCJleHAiOjE3NDQzNzQyNzAsImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6NTI0NiIsImF1ZCI6Imh0dHA6Ly9sb2NhbGhvc3Q6NTI0NiJ9.reQlePmwDpBL-_mcGOrWwADLJrxmUse5Gd7A-OgDi9s" +const EditActivity = ({activityData,onClose}) => { const [isLoading, setIsLoading] = useState(false); const { @@ -55,19 +51,45 @@ const EditActivity = ({onClose}) => { // Pre-populating the form with initial data when component mounts or initialData changes useEffect(() => { - if (initialData) { + if (activityData) { reset({ - activityName: initialData.activityName, - unitOfMeasurement: initialData.unitOfMeasurement, - checkList: initialData.checkList || [], + activityName: activityData.activityName, + unitOfMeasurement: activityData.unitOfMeasurement, + checkList: activityData.checkList || [], }); } - }, [initialData, reset]); + }, [activityData, reset]); // Form submission handler - const onSubmit = (data) => { - console.log('Submitted:', data); - }; + const onSubmit = async( data ) => + { + console.log(data) + setIsLoading(true) + MasterRespository.updateActivity(activityData?.id,data).then((resp)=>{ + setIsLoading(false) + const cachedData = getCachedData("Activity"); + if (cachedData) { + const updatedData = cachedData.map((activity) => + activity.id === activityData?.id + ? { + ...activity, + activityName: resp.data.activityName, + unitOfMeasurement: resp.data.unitOfMeasurement, + } + : activity + ); + + cacheData("Activity", updatedData); + } + showToast("Activity Added successfully.", "success"); + onClose() + }).catch((error)=>{ + showToast(error.message, "error"); + setIsLoading(false) + }) + + + }; // Add a new checklist item const addChecklistItem = () => { diff --git a/src/components/master/MasterModal.jsx b/src/components/master/MasterModal.jsx index ef976fbe..19aea645 100644 --- a/src/components/master/MasterModal.jsx +++ b/src/components/master/MasterModal.jsx @@ -55,7 +55,7 @@ const MasterModal = ({ modaldata, closeModal }) => { ) } {modaldata?.modalType === 'Edit-Activity' && ( - + )}
diff --git a/src/repositories/MastersRepository.jsx b/src/repositories/MastersRepository.jsx index 6557ea93..a785c152 100644 --- a/src/repositories/MastersRepository.jsx +++ b/src/repositories/MastersRepository.jsx @@ -30,8 +30,8 @@ export const MasterRespository = { updateJobRole: ( id, data ) => api.put( `/api/roles/jobrole/${ id }`, data ), getActivites: () => api.get( 'api/master/activities' ), - createActivities: () => api.post( 'api/master/activity' ), - updateActivity:(id) =>api.post(`api/master/edit/${id}`), + createActivity: (data) => api.post( 'api/master/activity',data ), + updateActivity:(id,data) =>api.post(`api/master/edit/${id}`,data), } \ No newline at end of file