"use client" import { LayoutNavbarNew, SkeletonList, TEMA } from '@/module/_global'; import LayoutModal from '@/module/_global/layout/layout_modal'; import { funGetUserByCookies } from '@/module/auth'; import { funGetAllmember, TypeUser } from '@/module/user'; import { useHookstate } from '@hookstate/core'; import { Carousel } from '@mantine/carousel'; import { ActionIcon, Avatar, Box, Button, Center, Divider, Flex, Grid, Indicator, rem, Stack, Text, TextInput } from '@mantine/core'; import { useMediaQuery, useShallowEffect } from '@mantine/hooks'; import { useParams, useRouter } from 'next/navigation'; import { useState } from 'react'; import toast from 'react-hot-toast'; import { FaCheck } from 'react-icons/fa6'; import { HiMagnifyingGlass } from 'react-icons/hi2'; import { IoArrowBackOutline, IoClose } from 'react-icons/io5'; import { funAddDivisionMember, funGetDivisionById } from '../lib/api_division'; import { IDataMemberDivision } from '../lib/type_division'; export default function CreateAnggotaDivision() { const router = useRouter() const [selectedFiles, setSelectedFiles] = useState([]); const [dataMember, setDataMember] = useState([]) const [memberDb, setMemberDb] = useState([]) const [group, setGroup] = useState("") const [isOpen, setOpen] = useState(false) const param = useParams<{ id: string }>() const [loading, setLoading] = useState(true) const [onClickSearch, setOnClickSearch] = useState(false) const tema = useHookstate(TEMA) const isMobile2 = useMediaQuery("(max-width: 438px)") const [loadingModal, setLoadingModal] = useState(false) const handleFileClick = (index: number) => { if (selectedFiles.some((i: any) => i.idUser == dataMember[index].id)) { setSelectedFiles(selectedFiles.filter((i: any) => i.idUser != dataMember[index].id)) } else { setSelectedFiles([...selectedFiles, { idUser: dataMember[index].id, name: dataMember[index].name, img: dataMember[index].img }]) } }; function handleXMember(id: number) { setSelectedFiles(selectedFiles.filter((i: any) => i.idUser != id)) } async function loadMember(group: string, search: string) { try { setLoading(true) const res = await funGetAllmember('?active=true&group=' + group + '&search=' + search); const user = await funGetUserByCookies(); if (res.success) { const dariUserLogin = res.data.filter((i: any) => i.id != user.id) const fixListUser = dariUserLogin.filter((i: any) => i.idUserRole == 'coadmin' || i.idUserRole == 'user') setDataMember(fixListUser) } else { toast.error(res.message) } } catch (error) { console.error(error) toast.error("Gagal memuat data, coba lagi nanti") } finally { setLoading(false) } } async function loadFirst() { const respon = await funGetDivisionById(param.id); if (respon.success) { setMemberDb(respon.data.member) setGroup(respon.data.division.idGroup) loadMember(respon.data.division.idGroup, "") } else { toast.error(respon.message); } } async function addMember() { try { setLoadingModal(true) const res = await funAddDivisionMember(param.id, selectedFiles) if (res.success) { toast.success(res.message) router.push("/division/info/" + param.id) } else { toast.error(res.message) } } catch (error) { console.error(error); toast.error("Gagal menambahkan anggota divisi, coba lagi nanti"); } finally { setLoadingModal(false) setOpen(false) } } function onCheck() { if (selectedFiles.length == 0) { return toast.error("Error! silahkan pilih anggota") } setOpen(true) } useShallowEffect(() => { loadFirst() }, []); const handleSearchClick = () => { setOnClickSearch(true); }; const handleClose = () => { setOnClickSearch(false); }; return ( } /> {onClickSearch ? ( loadMember(group, e.target.value)} /> ) : null } {selectedFiles.length > 0 ? ( {selectedFiles.map((v: any, i: any) => { return ( { handleXMember(v.idUser) }} >
}>
{v.name}
) })}
) : ( Tidak ada anggota yang dipilih )}
{loading ? Array(8) .fill(null) .map((_, i) => ( )) : (dataMember.length === 0) ? Tidak ada anggota : {dataMember.map((v: any, index: any) => { const isSelected = selectedFiles.some((i: any) => i.idUser == dataMember[index].id) const found = memberDb.some((i: any) => i.idUser == v.id) return ( (!found) ? handleFileClick(index) : null}> {v.name} {(found) ? "sudah menjadi anggota divisi" : ""} {isSelected ? : null} ) })} } setOpen(false)} description="Apakah Anda yakin ingin menambahkan anggota divisi?" onYes={(val) => { if (val) { addMember() } else { setOpen(false) } }} />
); }