'use client' import { currentScroll, globalNotifPage, globalRole, ReloadButtonTop, SkeletonList, TEMA } from '@/module/_global'; import { useHookstate } from '@hookstate/core'; import { ActionIcon, Avatar, Box, Card, Center, Divider, Flex, Grid, Group, Skeleton, Text, TextInput, Title } from '@mantine/core'; import { useMediaQuery, useShallowEffect } from '@mantine/hooks'; import _ from 'lodash'; import { useRouter, useSearchParams } from 'next/navigation'; import { useEffect, useState } from 'react'; import toast from 'react-hot-toast'; import { HiMagnifyingGlass, HiMiniUserGroup, HiOutlineListBullet, HiSquares2X2 } from 'react-icons/hi2'; import { MdAccountCircle } from 'react-icons/md'; import { funGetAllDivision } from '../lib/api_division'; import { IDataDivison } from '../lib/type_division'; export default function ListDivision() { const [isList, setIsList] = useState(false) const router = useRouter() const [data, setData] = useState([]) const [jumlah, setJumlah] = useState(0) const [searchQuery, setSearchQuery] = useState('') const searchParams = useSearchParams() const group = searchParams.get('group') const [loading, setLoading] = useState(true) const [nameGroup, setNameGroup] = useState('') const roleLogin = useHookstate(globalRole) const tema = useHookstate(TEMA) const { value: containerRef } = useHookstate(currentScroll); const [isPage, setPage] = useState(1) const paddingLift = useMediaQuery('(max-width: 505px)') const [isRefresh, setRefresh] = useState(false) const notifLoadPage = useHookstate(globalNotifPage) const status = searchParams.get('active') const handleList = () => { setIsList(!isList) } const fetchData = async (loading: boolean) => { try { setLoading(loading); if (isPage == 1) { setData([]) } const response = await funGetAllDivision('?active=' + status + '&search=' + searchQuery + '&group=' + group + '&page=' + isPage) if (response.success) { setJumlah(response.total) setNameGroup(response.filter.name) if (isPage == 1) { setData(response.data) } else { setData((data) => [...data, ...response.data]) } } else { toast.error(response.message); } } catch (error) { toast.error("Gagal mendapatkan divisi, coba lagi nanti"); console.error(error); } finally { setLoading(false); } }; useShallowEffect(() => { setPage(1) fetchData(true) }, [status, searchQuery]) useShallowEffect(() => { fetchData(false) }, [isPage]) useEffect(() => { const handleScroll = async () => { if (containerRef && containerRef.current) { const scrollTop = containerRef.current.scrollTop; const containerHeight = containerRef.current.clientHeight; const scrollHeight = containerRef.current.scrollHeight; if (scrollTop + containerHeight >= scrollHeight) { setPage(isPage + 1) } } }; const container = containerRef?.current; container?.addEventListener("scroll", handleScroll); return () => { container?.removeEventListener("scroll", handleScroll); }; }, [containerRef, isPage]); useShallowEffect(() => { if (notifLoadPage.get().category == 'division' && notifLoadPage.get().load == true) { setRefresh(true) } }, [notifLoadPage.get().load]) function onRefresh() { notifLoadPage.set({ category: '', load: false }) setRefresh(false) setPage(1) setTimeout(() => { fetchData(false) }, 500) } return ( { isRefresh && { onRefresh() }} title='UPDATE' /> } } placeholder="Pencarian" value={searchQuery} onChange={(val) => { setSearchQuery(val.target.value) }} /> {isList ? ( ) : ( )} {roleLogin.get() == 'supadmin' && Filter by: {nameGroup}} Total Divisi {jumlah} {isList ? ( {loading ? Array(6) .fill(null) .map((_, i) => ( )) : _.isEmpty(data) ? Tidak ada Divisi : data?.map((v: any, i: any) => { return ( router.push(`/division/${v.id}`)}>
{v.name}
); }) }
) : ( {loading ? Array(6) .fill(null) .map((_, i) => ( )) : _.isEmpty(data) ? Tidak ada Divisi : data?.map((v: any, i: any) => { return ( router.push(`/division/${v.id}`)}> {v.name} {v.desc} { (v.jumlah_member == 0) ? "0" : "+" + (v.jumlah_member - 1) } ); }) } )}
); }