From 7385f59bbc63c02c5dca3f78aa2e7cc5c504070e Mon Sep 17 00:00:00 2001 From: "ashutosh.nehete" Date: Fri, 27 Jun 2025 16:36:25 +0530 Subject: [PATCH] Enhanced CreateActivity Component to Select Service and Activity Group --- src/components/master/CreateActivity.jsx | 284 ++++++++++++++++------- 1 file changed, 205 insertions(+), 79 deletions(-) diff --git a/src/components/master/CreateActivity.jsx b/src/components/master/CreateActivity.jsx index 62cca207..4d581e6e 100644 --- a/src/components/master/CreateActivity.jsx +++ b/src/components/master/CreateActivity.jsx @@ -10,11 +10,19 @@ import showToast from "../../services/toastService"; const schema = z.object({ activityName: z.string().min(1, { message: "Activity Name is required" }), - unitOfMeasurement: z.string().min(1, { message: "Unit of Measurement is required" }), + unitOfMeasurement: z + .string() + .min(1, { message: "Unit of 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({ - description: z.string().min(1, { message: "descriptionlist item cannot be empty" }), + description: z + .string() + .min(1, { message: "description list item cannot be empty" }), isMandatory: z.boolean().default(false), id: z.any().default(null), }) @@ -24,6 +32,12 @@ const schema = z.object({ const CreateActivity = ({ 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, @@ -41,10 +55,11 @@ const CreateActivity = ({ onClose }) => { activityName: "", unitOfMeasurement: "", checkList: [], + serviceId: "", + actitvityGroupId: "", }, }); - const { fields: checkListItems, append, @@ -59,15 +74,13 @@ const CreateActivity = ({ onClose }) => { setIsLoading(true); MasterRespository.createActivity(data) - .then( ( resp ) => - { - + .then((resp) => { const cachedData = getCachedData("Activity"); - const updatedData = [ ...cachedData, resp?.data ]; + const updatedData = [...cachedData, resp?.data]; cacheData("Activity", updatedData); showToast("Activity Successfully Added.", "success"); setIsLoading(false); - handleClose() + handleClose(); }) .catch((error) => { showToast(error.message, "error"); @@ -75,7 +88,6 @@ const CreateActivity = ({ onClose }) => { }); }; - const addChecklistItem = () => { const values = getValues("checkList"); const lastIndex = checkListItems.length - 1; @@ -97,11 +109,33 @@ const CreateActivity = ({ onClose }) => { }); }; + 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; + const service = services.find((b) => b.id === String(value)); + reset((prev) => ({ + ...prev, + actitvityGroupId: String(value), + })); + }; + const removeChecklistItem = (index) => { remove(index); }; - const handleChecklistChange = (index, value) => { setValue(`checkList.${index}`, value); }; @@ -111,11 +145,23 @@ const CreateActivity = ({ onClose }) => { onClose(); }; - - // for tooltip useEffect(() => { - const tooltipTriggerList = Array.from(document.querySelectorAll('[data-bs-toggle="tooltip"]')); + if (!services) { + MasterRespository.getServices().then((res) => { + setServices(res?.data); + cacheData("Services", res?.data); + }); + } + if (!activityGroups) { + MasterRespository.getActivityGroups().then((res) => { + setActivityGroups(res?.data); + cacheData("Activity Group", res?.data); + }); + } + const tooltipTriggerList = Array.from( + document.querySelectorAll('[data-bs-toggle="tooltip"]') + ); tooltipTriggerList.forEach((el) => new bootstrap.Tooltip(el)); }, []); return ( @@ -150,89 +196,169 @@ const CreateActivity = ({ onClose }) => { )} -
-

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

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

{errors.serviceId.message}

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

{errors.actitvityGroupId.message}

+ )} +
+ )} + +
+

+ {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} + + )} + + + + +
)}
-