From b46d0776d608090b01b939d69256367faeb323e8 Mon Sep 17 00:00:00 2001 From: "ashutosh.nehete" Date: Fri, 27 Jun 2025 17:07:02 +0530 Subject: [PATCH] Enhance EditActivity Component to Select Service and Activity Group --- src/components/master/EditActivity.jsx | 404 ++++++++++++++++++------- 1 file changed, 295 insertions(+), 109 deletions(-) diff --git a/src/components/master/EditActivity.jsx b/src/components/master/EditActivity.jsx index 503bf8c0..a601bf07 100644 --- a/src/components/master/EditActivity.jsx +++ b/src/components/master/EditActivity.jsx @@ -2,28 +2,38 @@ import React, { useEffect, useState } from "react"; import { useForm, useFieldArray } from "react-hook-form"; import { z } from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; -import {MasterRespository} from "../../repositories/MastersRepository"; +import { MasterRespository } from "../../repositories/MastersRepository"; import showToast from "../../services/toastService"; -import {getCachedData,cacheData} from "../../slices/apiDataManager"; - +import { getCachedData, cacheData } from "../../slices/apiDataManager"; const schema = z.object({ activityName: z.string().min(1, { message: "Activity name is required" }), unitOfMeasurement: z.string().min(1, { message: "Measurement is required" }), + serviceId: z.string().min(1, { message: "A service selection is required." }), + actitvityGroupId: z + .string() + .min(1, { message: "A activity group selection is required." }), checkList: z .array( z.object({ id: z.any().default(null), - description: z.string().min(1, { message: "Checklist item cannot be empty" }), + description: z + .string() + .min(1, { message: "Checklist item cannot be empty" }), isMandatory: z.boolean().default(false), }) ) .optional(), }); - const UpdateActivity = ({ activityData, onClose }) => { const [isLoading, setIsLoading] = useState(false); + const [services, setServices] = useState(getCachedData("Services")); + const [selectedService, setSelectedService] = useState(null); + const [activityGroups, setActivityGroups] = useState( + getCachedData("Activity Group") + ); + const [activityGroupList, setActivityGroupList] = useState(null); const { register, @@ -35,26 +45,80 @@ const UpdateActivity = ({ activityData, onClose }) => { clearErrors, getValues, formState: { errors }, + watch, } = useForm({ resolver: zodResolver(schema), defaultValues: { - id:activityData.id, - activityName: activityData.activityName, - unitOfMeasurement: activityData.unitOfMeasurement, - checkLists: activityData.checkLists || [], + id: activityData.id, + serviceId: activityData.serviceId || "", + actitvityGroupId: activityData.actitvityGroupId || "", + activityName: activityData.activityName, + unitOfMeasurement: activityData.unitOfMeasurement, + checkLists: activityData.checkLists || [], }, }); - const { fields: checkListItems, append, remove } = useFieldArray({ + const selectedServiceId = watch("serviceId"); + const [selectedActitvityGroupId, setSelectedActitvityGroupId] = useState( + watch("actitvityGroupId") + ); + + const { + fields: checkListItems, + append, + remove, + } = useFieldArray({ control, name: "checkList", }); // Load initial data useEffect(() => { + if (!services) { + MasterRespository.getServices().then((res) => { + setServices(res?.data); + cacheData("Services", res?.data); + }); + } + if (!activityGroups) { + MasterRespository.getActivityGroups().then((res) => { + setActivityGroups(res?.data); + const selectedActivityGroups = res?.data.filter( + (item) => item.serviceId == selectedServiceId + ); + setActivityGroupList(selectedActivityGroups); + cacheData("Activity Group", res?.data); + const isExist = selectedActivityGroups.some( + (item) => item.id === String(watch("actitvityGroupId")) + ); + + if (!isExist) { + reset((prev) => ({ + ...prev, + actitvityGroupId: "", + })); + } + }); + } else { + const selectedActivityGroups = activityGroups.filter( + (item) => item.serviceId == selectedServiceId + ); + setActivityGroupList(selectedActivityGroups); + const isExist = selectedActivityGroups.some( + (item) => item.id === String(watch("actitvityGroupId")) + ); + if (!isExist) { + reset((prev) => ({ + ...prev, + actitvityGroupId: "", + })); + } + } if (activityData) { - reset( { - id:activityData.id, + reset({ + id: activityData.id, + serviceId: activityData.serviceId || "", + actitvityGroupId: activityData.actitvityGroupId || "", activityName: activityData.activityName, unitOfMeasurement: activityData.unitOfMeasurement, checkList: activityData.checkLists || [], @@ -62,42 +126,66 @@ const UpdateActivity = ({ activityData, onClose }) => { } }, [activityData]); - + const handleServicesChange = (e) => { + const { value } = e.target; + const service = services.find((b) => b.id === String(value)); + const selectedActivityGroups = activityGroups.filter( + (ag) => ag.serviceId == service.id + ); + setSelectedService(service.id); + setActivityGroupList(selectedActivityGroups); + reset((prev) => ({ + ...prev, + serviceId: String(value), + })); + }; + + const handleActivityGroupsChange = (e) => { + const { value } = e.target; + setValue("actitvityGroupId", String(value)); + // reset((prev) => ({ + // ...prev, + // actitvityGroupId: String(value), + // })); + setSelectedActitvityGroupId(String(value)); + }; + const handleChecklistChange = (index, value) => { setValue(`checkList.${index}`, value); }; // Submit handler - const onSubmit = async(data) => { + const onSubmit = async (data) => { setIsLoading(true); - - const Activity = {...data, id:activityData.id} - try - { - const response = await MasterRespository.updateActivity( activityData?.id, Activity ); + + const Activity = { ...data, id: activityData.id }; + try { + const response = await MasterRespository.updateActivity( + activityData?.id, + Activity + ); const updatedActivity = response.data; - const cachedData = getCachedData("Activity") - + const cachedData = getCachedData("Activity"); + if (cachedData) { const updatedActivities = cachedData.map((activity) => - activity.id === updatedActivity.id ? { ...activity, ...updatedActivity } : activity + activity.id === updatedActivity.id + ? { ...activity, ...updatedActivity } + : activity ); - - cacheData( "Activity", updatedActivities ); - onClose() + + cacheData("Activity", updatedActivities); + onClose(); } - setIsLoading( false ) + setIsLoading(false); showToast("Activity Successfully Updated", "success"); - } catch ( err ) - { - setIsLoading( false ) + } catch (err) { + setIsLoading(false); showToast("error.message", "error"); } }; - - // Add new checklist item const addChecklistItem = () => { const values = getValues("checkList"); @@ -122,12 +210,13 @@ const UpdateActivity = ({ activityData, onClose }) => { remove(index); }; - - // for tooltip - useEffect(() => { - const tooltipTriggerList = Array.from(document.querySelectorAll('[data-bs-toggle="tooltip"]')); - tooltipTriggerList.forEach((el) => new bootstrap.Tooltip(el)); - }, []); + // for tooltip + useEffect(() => { + const tooltipTriggerList = Array.from( + document.querySelectorAll('[data-bs-toggle="tooltip"]') + ); + tooltipTriggerList.forEach((el) => new bootstrap.Tooltip(el)); + }, []); return (
@@ -135,11 +224,13 @@ const UpdateActivity = ({ activityData, onClose }) => {
{/* Activity Name */}
- + {errors.activityName && (
{errors.activityName.message}
@@ -148,93 +239,188 @@ const UpdateActivity = ({ activityData, onClose }) => { {/* Unit of Measurement */}
- + {errors.unitOfMeasurement && ( -
{errors.unitOfMeasurement.message}
+
+ {errors.unitOfMeasurement.message} +
+ )} +
+ + {/* Services */} +
+ + {activityData.serviceId != null ? ( + + ) : ( + + )} + {errors.serviceId && ( +

{errors.serviceId.message}

+ )} +
+ + {/* Actitvity Group */} +
+ + + {errors.actitvityGroupId && ( +

{errors.actitvityGroupId.message}

)}
{/* Checklist */}
-

{checkListItems.length > 0 ? "Check List" : "Add Check List"}

+

+ {checkListItems.length > 0 ? "Check List" : "Add Check List"} +

{checkListItems.length > 0 && ( - - - - - - - - - - {checkListItems.map((item, index) => ( - - - - - - ))} - -
- Name - - Is Mandatory - Action
- - - handleChecklistChange(index, e.target.value) - } - /> - {errors.checkList?.[index]?.description && ( - - {errors.checkList[index]?.description?.message} - - )} - - - - -
+ + + + + + + + + + {checkListItems.map((item, index) => ( + + + + + + ))} + +
+ Name + + Is Mandatory + Action
+ + + handleChecklistChange(index, e.target.value) + } + /> + {errors.checkList?.[index]?.description && ( + + {errors.checkList[index]?.description?.message} + + )} + + + + +
)} -