import Styles from "@/constants/Styles" import { apiGetDivisionGroup, apiGetDocumentInformasi, apiGetListDivisionByIdDivision } from "@/lib/api" import { useAuthSession } from "@/providers/AuthProvider" import { AntDesign } from "@expo/vector-icons" import { useEffect, useState } from "react" import { Pressable, ScrollView, ToastAndroid, View } from "react-native" import Text from "./Text"; import { ButtonForm } from "./buttonForm" import DrawerBottom from "./drawerBottom" type Props = { open: boolean close: (value: boolean) => void title: string category: 'share-division' | 'choose-division' choose: string onSelect: (value: any[]) => void value?: any item?: any } type CheckedState = { [key: string]: string[]; } type GroupData = { id: string; name: string; Division: { id: string; name: string; }[]; } export default function ModalSelectMultiple({ open, close, title, category, choose, onSelect, value, item }: Props) { const [isChoose, setChoose] = useState(choose) const { token, decryptToken } = useAuthSession() const [data, setData] = useState([]) const [checked, setChecked] = useState({}); const [selectedDivision, setSelectedDivision] = useState([]); async function handleLoadChooseDivision() { try { const hasil = await decryptToken(String(token?.current)) const response = await apiGetDivisionGroup({ user: hasil }) setData(response.data) if (value?.length > 0) { const formatArray = value.reduce((result: any, obj: any) => { result[obj.id] = obj.Division.map((item: any) => item.id); return result; }, {}) setChecked(formatArray) } } catch (error) { console.error(error) } } async function handleLoadShareDivision() { try { const hasil = await decryptToken(String(token?.current)) const response = await apiGetListDivisionByIdDivision({ user: hasil, search: '', division: value }) const response2 = await apiGetDocumentInformasi({ user: hasil, item: item, cat: 'share' }) setData(response.data.filter((i: any) => i.id != value)) setSelectedDivision(response2.data) } catch (error) { console.error(error) } } useEffect(() => { if (open) { if (category == 'choose-division') { handleLoadChooseDivision() } else if (category == 'share-division') { handleLoadShareDivision() } } }, [open]); 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); if (newChecked[groupId]?.length === 0) { delete newChecked[groupId]; } } else { newChecked[groupId].push(divisionId); } } else { newChecked[groupId] = [divisionId]; } setChecked(newChecked); }; const handleGroupCheck = (groupId: string) => { const newChecked = { ...checked }; if (newChecked[groupId]) { delete newChecked[groupId]; } else { if (data.find((item: { id: string }) => item.id === groupId)?.Division?.length == 0) { return ToastAndroid.show('Tidak ada divisi', ToastAndroid.SHORT) } newChecked[groupId] = data.find((item: { id: string }) => item.id === groupId)?.Division.map((item: { id: any }) => item.id) || []; } setChecked(newChecked); }; const handleDivisionClick = (index: number) => { if (selectedDivision.some((i: any) => i.id == data[index].id)) { setSelectedDivision( selectedDivision.filter((i: any) => i.id != data[index].id) ); } else { setSelectedDivision([ ...selectedDivision, { id: data[index].id, name: data[index].name }, ]); } }; const handleSubmit = () => { if (category == "choose-division") { const selectedGroups: GroupData[] = []; Object.keys(checked).forEach((groupId) => { const group = data.find((item: { id: string }) => item.id === groupId); if (group) { selectedGroups.push({ id: group.id, name: group.name, Division: group.Division.filter((division: { id: string }) => checked[groupId].includes(division.id)), }); } }); onSelect(selectedGroups); } else { onSelect(selectedDivision); } }; return ( { category == 'share-division' ? <> { data.map((item: any, index: number) => { return ( { handleDivisionClick(index) }}> {item.name} { selectedDivision.some((i: any) => i.id == item.id) ? : <> } ) }) } : data.map((item: any, index: number) => { return ( { handleGroupCheck(item.id) }}> {item.name} { checked[item.id] && checked[item.id]?.length === item.Division?.length ? : (checked[item.id] && checked[item.id]?.length > 0 && checked[item.id]?.length < item.Division?.length) ? : <> } { item.Division.map((child: any, v: number) => { return ( { handleCheck(item.id, child.id) }}> {child.name} { checked[item.id] && checked[item.id].includes(child.id) && } ) }) } ) }) } { handleSubmit() }} /> ) }