/* eslint-disable react-hooks/exhaustive-deps */ import { AlertDefaultSystem, BackButton, BaseBox, DrawerCustom, LoaderCustom, MenuDrawerDynamicGrid, NewWrapper, ScrollableCustom, StackCustom, TextCustom, } from "@/components"; import { IconDot } from "@/components/_Icon/IconComponent"; import ListSkeletonComponent from "@/components/_ShareComponent/ListSkeletonComponent"; import NoDataText from "@/components/_ShareComponent/NoDataText"; import { AccentColor, MainColor } from "@/constants/color-palet"; import { ICON_SIZE_SMALL } from "@/constants/constans-value"; import { useAuth } from "@/hooks/use-auth"; import { useNotificationStore } from "@/hooks/use-notification-store"; import { apiGetNotificationsById } from "@/service/api-notifications"; import { listOfcategoriesAppNotification } from "@/types/type-notification-category"; import { formatChatTime } from "@/utils/formatChatTime"; import { Ionicons } from "@expo/vector-icons"; import { router, Stack, useFocusEffect, useLocalSearchParams, } from "expo-router"; import _ from "lodash"; import { useCallback, useEffect, useState } from "react"; import { RefreshControl, View } from "react-native"; const selectedCategory = (value: string) => { const category = listOfcategoriesAppNotification.find( (c) => c.value === value, ); return category?.label; }; const fixPath = ({ deepLink, categoryApp, }: { deepLink: string; categoryApp: string; }) => { if (categoryApp === "OTHER") { return deepLink; } const separator = deepLink.includes("?") ? "&" : "?"; const fixedPath = `${deepLink}${separator}from=notifications&category=${_.lowerCase( categoryApp, )}`; console.log("Fix Path", fixedPath); return fixedPath; }; const BoxNotification = ({ data, activeCategory, setListData, }: { data: any; activeCategory: string | null; setListData: (data: any) => void; }) => { // console.log("DATA NOTIFICATION", JSON.stringify(data, null, 2)); const { markAsRead } = useNotificationStore(); return ( <> { // console.log( // "Notification >", // selectedCategory(activeCategory as string) // ); const newPath = fixPath({ deepLink: data.deepLink, categoryApp: data.kategoriApp, }); router.navigate(newPath as any); selectedCategory(activeCategory as string); if (!data.isRead) { markAsRead(data.id); const updatedData = { ...data, isRead: true, }; console.log("Updated Data", updatedData); setListData((prev: any) => prev.map((item: any) => item.id === data.id ? updatedData : item, ), ); } }} > {data.title} {data.pesan} {formatChatTime(data.createdAt)} ); }; export default function ScreenNotification_V1() { const { user } = useAuth(); const { category } = useLocalSearchParams<{ category?: string }>(); const [activeCategory, setActiveCategory] = useState( category || "event", ); const [listData, setListData] = useState([]); const [refreshing, setRefreshing] = useState(false); const [loading, setLoading] = useState(false); const [loadingMore, setLoadingMore] = useState(false); const [page, setPage] = useState(1); const [hasMore, setHasMore] = useState(true); const [openDrawer, setOpenDrawer] = useState(false); const { markAsReadAll } = useNotificationStore(); const handlePress = (item: any) => { setActiveCategory(item.value); // Reset pagination saat kategori berubah setListData([]); setPage(1); setHasMore(true); }; // useFocusEffect( // useCallback(() => { // fecthData(1, true); // }, [activeCategory]) // ); useEffect(() => { fecthData(1, true); }, [activeCategory]); const fecthData = async (pageNum: number, clear: boolean = false) => { if (pageNum === 1 && !clear) return; // Hindari duplicate call untuk page 1 try { if (pageNum === 1) { setLoading(true); } else { setLoadingMore(true); } const response = await apiGetNotificationsById({ id: user?.id as any, category: activeCategory as any, page: String(pageNum), }); if (response.success) { if (clear || pageNum === 1) { setListData(response.data); } else { setListData((prev) => [...prev, ...response.data]); } // Jika data yang dikembalikan kurang dari ukuran halaman, maka tidak ada halaman berikutnya setHasMore(response.data.length >= 10); // Asumsikan ukuran halaman 10 } else { if (pageNum === 1) { setListData([]); } } } catch (error) { console.log("Error Notification", error); if (pageNum === 1) { setListData([]); } } finally { if (pageNum === 1) { setLoading(false); } else { setLoadingMore(false); } } }; const onRefresh = async () => { setRefreshing(true); await fecthData(1, true); setRefreshing(false); }; const loadMore = () => { if (!loadingMore && hasMore) { const nextPage = page + 1; setPage(nextPage); fecthData(nextPage, false); } }; return ( <> , headerRight: () => ( setOpenDrawer(true)} /> ), }} /> ({ id: i, label: e.label, value: e.value, }))} onButtonPress={handlePress} activeId={activeCategory as string} /> } listData={listData} renderItem={({ item }) => ( )} refreshControl={ } onEndReached={loadMore} // onEndReachedThreshold={0.1} ListEmptyComponent={ loading ? ( ) : ( ) } ListFooterComponent={ loadingMore ? ( ) : null } /> setOpenDrawer(false)} height={"auto"} > ), path: "", }, ]} onPressItem={(item: any) => { console.log("Item", item.value); if (item.value === "read-all") { AlertDefaultSystem({ title: "Tandai Semua Dibaca", message: "Apakah Anda yakin ingin menandai semua notifikasi dibaca?", textLeft: "Batal", textRight: "Ya", onPressRight: () => { markAsReadAll(user?.id as any); const data = _.cloneDeep(listData); data.forEach((e) => { e.isRead = true; }); setListData(data); onRefresh(); setOpenDrawer(false); }, }); } }} /> ); }