import { useState } from "react"; import { useActivitiesByGroups, useGroups, } from "../../../hooks/masterHook/useMaster"; import ManageGroup from "./ManageGroup"; import ManageActivity from "./ManageActivity"; import { useMasterContext } from "../../../pages/master/MasterPage"; const ServiceGroups = ({ service }) => { const [openService, setOpenService] = useState(true); const [activeGroupId, setActiveGroupId] = useState(null); // track selected group const {setDeleletingServiceItem} =useMasterContext() const [isManageGroup, setManageGroup] = useState({ isOpen: false, group: null, serviceId: null, }); const [isManageActivity, setManageActivity] = useState({ isOpen: false, activity: null, // activity is either a single activity for editing or null for creating new activity groupId: null, // groupId for managing activities in specific groups }); // Fetch groups and activities data const { data: groups, isLoading } = useGroups(service?.id); // Fetch groups for the service const { data: activities, isLoading: actLoading } = useActivitiesByGroups(activeGroupId); // Fetch activities based on activeGroupId if (isLoading) return
Loading groups...
; // Show loading state while groups are being fetched // Handle toggling of group to view activities const toggleGroup = (groupId) => { setActiveGroupId((prev) => (prev === groupId ? null : groupId)); // If the same group is clicked again, close it }; return (
{/* Service Header */}

{service.name}

{/* Groups Section */}
{/* Show ManageGroup for creating a new group */} {isManageGroup.isOpen && isManageGroup.group === null ? ( setManageGroup({ isOpen: false, group: null, serviceId: service.id, }) } /> ) : (
{groups?.data?.map((group) => { const isOpen = activeGroupId === group.id; return (
{/* Show group toggle button only if ManageGroup is not open */}
{!isManageGroup.isOpen && ( toggleGroup(group.id)} className="text-end cursor-pointer" data-bs-toggle="collapse" data-bs-target={`#accordionGroup${group.id}`} aria-expanded={isOpen} aria-controls={`accordionGroup${group.id}`} > )}

{group.name}

{/* Create New Activity */} { setManageActivity({ isOpen: true, activity: null, // Indicating new activity creation groupId: group.id, // Set the groupId for the new activity }); }} /> {/* Edit Group */} setManageGroup({ isOpen: true, group: group, // Group selected for Editing serviceId: service.id, }) } /> {/* Delete Group */} setDeleletingServiceItem({isOpen:true,ItemId:group.id,whichItem:"group"})} />
{/* Only show ManageGroup for the specific group if it's open */} {isManageGroup.isOpen && isManageGroup.group?.id === group.id ? ( setManageGroup({ isOpen: false, group: null, serviceId: null, }) } /> ) : isManageActivity.isOpen && isManageActivity.groupId === group.id ? ( { setManageActivity({ isOpen: false, activity: null, groupId: null, }); }} /> ) : (
{isOpen && actLoading && (

Loading activities...

)} {isOpen && activities?.data?.length > 0 ? (
{/* Header Row */}
Activity Name Unit of Measurement Action
{/* Map through activities */} {activities.data.map((activity) => (
{/* Activity Name Column */}
{activity.activityName}
{/* Unit of Measurement Column */}
{activity.unitOfMeasurement}
{/* Action Column */}
{/* Edit Activity */} { setManageActivity({ isOpen: true, activity: activity, // Pass the specific activity for editing groupId: group.id, // Set groupId for the specific activity }); }} /> {/* Delete Activity */} setDeleletingServiceItem({isOpen:true,ItemId:activity.id,whichItem:"activity"})} />
))}
) : ( isOpen && (

No activities found

) )}
)}
); })}
)}
); }; export default ServiceGroups;