"use client"; import { currentScroll, TEMA } from "@/module/_global"; import { useHookstate } from "@hookstate/core"; import { ActionIcon, Box, Grid, Skeleton, Spoiler, Text } from "@mantine/core"; import { useMediaQuery, useShallowEffect } from "@mantine/hooks"; import { useRouter } from "next/navigation"; import { useEffect, useState } from "react"; import toast from "react-hot-toast"; import { FaBell } from "react-icons/fa6"; import { funGetAllNotification, funReadNotification, } from "../lib/api_notification"; import { IListNotification } from "../lib/type_notification"; export default function ListNotification() { const router = useRouter(); const isMobile = useMediaQuery("(max-width: 369px)"); const isMobile2 = useMediaQuery("(max-width: 575px)"); const [isData, setData] = useState([]); const tema = useHookstate(TEMA); const { value: containerRef } = useHookstate(currentScroll); const [isPage, setPage] = useState(1); const [loading, setLoading] = useState(true); async function fetchData(loading: boolean) { try { if (loading) setLoading(true); const res = await funGetAllNotification("?page=" + isPage); if (res.success) { if (isPage == 1) { setData(res.data); } else { setData([...isData, ...res.data]); } } else { toast.error(res.message); } } catch (error) { console.error(error); toast.error("Gagal memuat data, coba lagi nanti"); } finally { setLoading(false); } } useShallowEffect(() => { fetchData(true); }, []); 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]); async function onReadNotif(category: string, idContent: string, idData: string) { try { const response = await funReadNotification({ id: idData }); if (response.success) { router.push(`/${category}/${idContent}`); } else { toast.error(response.message); } } catch (error) { console.error(error); toast.error("Gagal memuat data, coba lagi nanti"); } } return ( {loading ? ( Array(6) .fill(null) .map((_, i) => ( )) ) : ( {isData.length == 0 ? ( Tidak ada Notifikasi ) : ( isData.map((v, i) => { return ( { onReadNotif(v.category, v.idContent, v.id); }} > {/*
*/} {/*
*/}
{v.title} {v.createdAt}
{ onReadNotif(v.category, v.idContent, v.id); }} > {v.desc}
); }) )}
)} { }
); }