"use client"; import { LayoutNavbarNew, WARNA } from '@/module/_global'; import { funGetGroupDivision } from '@/module/group/lib/api_group'; import { Box, Button, Divider, Flex, Group, Stack, Text } from '@mantine/core'; import { useShallowEffect } from '@mantine/hooks'; import React, { useState } from 'react'; import { FaCheck } from 'react-icons/fa'; import { GroupData } from '../lib/type_announcement'; import { useHookstate } from '@hookstate/core'; import { globalMemberAnnouncement } from '../lib/val_announcement'; interface CheckedState { [key: string]: string[]; } export default function CreateUsersAnnouncement() { const [checked, setChecked] = useState({}); const [selectAll, setSelectAll] = useState(false); const [isData, setIsData] = useState([]) const memberGroup = useHookstate(globalMemberAnnouncement) const handleCheck = (groupId: string, divisionId: string) => { const newChecked = { ...checked }; if (newChecked[groupId]) { if (newChecked[groupId].includes(divisionId)) { newChecked[groupId] = newChecked[groupId].filter(item => item !== divisionId); } else { newChecked[groupId].push(divisionId); } } else { newChecked[groupId] = [divisionId]; } setChecked(newChecked); console.log(newChecked) }; const handleGroupCheck = (groupId: string) => { const newChecked = { ...checked }; if (newChecked[groupId]) { delete newChecked[groupId]; } else { newChecked[groupId] = isData.find(item => item.id === groupId)?.Division.map(item => item.id) || []; } setChecked(newChecked); console.log(newChecked) }; const handleSelectAll = () => { setSelectAll(!selectAll); if (!selectAll) { const newChecked: CheckedState = {}; isData.forEach(item => { newChecked[item.id] = item.Division.map(division => division.id); }); setChecked(newChecked); console.log(newChecked) } else { setChecked({}); } }; async function getData() { const response = await funGetGroupDivision() console.log(response) setIsData(response.data) } const handleSubmit = () => { const selectedGroups: GroupData[] = []; Object.keys(checked).forEach((groupId) => { if (checked[groupId]) { selectedGroups.push(); } }); memberGroup.set(selectedGroups); }; useShallowEffect(() => { getData() }, []) return (
} /> Pilih Semua {isData.map((item) => ( handleGroupCheck(item.id)} justify='space-between' align='center'> {item.name} {checked[item.id] && checked[item.id].length === item.Division.length ? : ""} {item.Division.map((division) => ( handleCheck(item.id, division.id)} justify='space-between' align='center'> {division.name} {checked[item.id] && checked[item.id].includes(division.id) ? : ""} ))} ))}
); }