"use client" import { currentScroll, globalNotifPage, globalRole, ReloadButtonTop, SkeletonList, TEMA } from '@/module/_global'; import { useHookstate } from '@hookstate/core'; import { ActionIcon, Avatar, Badge, 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, HiMiniPresentationChartBar, HiOutlineListBullet, HiSquares2X2 } from 'react-icons/hi2'; import { MdAccountCircle } from 'react-icons/md'; import { funGetAllProject } from '../lib/api_project'; import { IDataProject } from '../lib/type_project'; export default function ListProject() { const [isList, setIsList] = useState(false) const router = useRouter() const [isData, setData] = useState([]) const [loading, setLoading] = useState(true); const searchParams = useSearchParams() const status = searchParams.get('status') const group = searchParams.get('group') const kategori = searchParams.get('cat') const [searchQuery, setSearchQuery] = useState('') const roleLogin = useHookstate(globalRole) const [nameGroup, setNameGroup] = useState('') const tema = useHookstate(TEMA) const { value: containerRef } = useHookstate(currentScroll) const [isPage, setPage] = useState(1) const [totalData, setTotalData] = useState(0) const isMobile = useMediaQuery('(max-width: 369px)'); const paddingLift = useMediaQuery('(max-width: 505px)') const [isRefresh, setRefresh] = useState(false) const notifLoadPage = useHookstate(globalNotifPage) const handleList = () => { setIsList(!isList) } const fetchData = async (loading: boolean) => { try { if (loading) setLoading(true) if (isPage == 1) { setData([]) } const response = await funGetAllProject('?status=' + status + '&search=' + searchQuery + '&group=' + group + '&page=' + isPage + '&cat=' + kategori); if (response.success) { setNameGroup(response.filter.name) setTotalData(response.total) if (isPage == 1) { setData(response.data) } else { setData((isData) => [...isData, ...response.data]) } } else { toast.error(response.message); } } catch (error) { toast.error("Gagal mendapatkan kegiatan, coba lagi nanti"); console.error(error); } finally { setLoading(false); } }; useShallowEffect(() => { setPage(1) fetchData(true); }, [status, searchQuery, kategori]); 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 + 1 >= scrollHeight) { setPage(isPage + 1) } } }; const container = containerRef?.current; container?.addEventListener("scroll", handleScroll); return () => { container?.removeEventListener("scroll", handleScroll); }; }, [containerRef, isPage]); useShallowEffect(() => { if (notifLoadPage.get().category == 'project' && notifLoadPage.get().load == true) { setRefresh(true) } }, [notifLoadPage.get().load]) function onRefresh() { notifLoadPage.set({ category: '', load: false }) setRefresh(false) setPage(1) setTimeout(() => { fetchData(true) }, 500) } return ( { isRefresh && { onRefresh() }} title='UPDATE' /> } } placeholder="Pencarian" onChange={(event) => setSearchQuery(event.currentTarget.value)} value={searchQuery} /> {isList ? ( ) : ( )} {roleLogin.get() == 'supadmin' && Filter : {nameGroup}} {(roleLogin.get() == 'user' || roleLogin.get() == 'coadmin') && Filter : {(kategori == null || kategori == undefined || kategori == '') ? 'Kegiatan Saya' : 'Semua Kegiatan'}} Total Kegiatan {loading ? 0 : totalData} {isList ? ( {loading ? Array(3) .fill(null) .map((_, i) => ( )) : _.isEmpty(isData) ? Tidak ada Kegiatan : isData.map((v, i) => { return ( router.push(`/project/${v.id}`)}>
{v.title}
); }) }
) : ( {loading ? Array(3) .fill(null) .map((_, i) => ( )) : _.isEmpty(isData) ? Tidak ada Kegiatan : isData.map((v, i) => { return ( router.push(`/project/${v.id}`)}> {v.title} {v.desc} { v.status === 0 ? 'Segera' : v.status === 1 ? 'Dikerjakan' : v.status === 2 ? 'Selesai' : v.status === 3 ? 'Dibatalkan' : "" } {(v.member == 0) ? "0" : "+" + (v.member - 1)} ); }) } )}
); }