upd: caching data
Deskripsi: - update caching pada fitur utama -yg fitur divisi belom
This commit is contained in:
@@ -11,8 +11,9 @@ import { apiGetDiscussionGeneral } from "@/lib/api";
|
||||
import { useAuthSession } from "@/providers/AuthProvider";
|
||||
import { useTheme } from "@/providers/ThemeProvider";
|
||||
import { AntDesign, Feather, Ionicons, MaterialIcons } from "@expo/vector-icons";
|
||||
import { useInfiniteQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { router, useLocalSearchParams } from "expo-router";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { RefreshControl, View, VirtualizedList } from "react-native";
|
||||
import { useSelector } from "react-redux";
|
||||
|
||||
@@ -32,70 +33,76 @@ export default function Discussion() {
|
||||
const { colors } = useTheme();
|
||||
const { active, group } = useLocalSearchParams<{ active?: string, group?: string }>()
|
||||
const [search, setSearch] = useState('')
|
||||
const [nameGroup, setNameGroup] = useState('')
|
||||
const [data, setData] = useState<Props[]>([])
|
||||
const update = useSelector((state: any) => state.discussionGeneralDetailUpdate)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const arrSkeleton = Array.from({ length: 5 }, (_, index) => index)
|
||||
const [status, setStatus] = useState<'true' | 'false'>('true')
|
||||
const [page, setPage] = useState(1)
|
||||
const [waiting, setWaiting] = useState(false)
|
||||
const queryClient = useQueryClient()
|
||||
const [status, setStatus] = useState<'true' | 'false'>(active == 'false' ? 'false' : 'true')
|
||||
const [refreshing, setRefreshing] = useState(false)
|
||||
|
||||
async function handleLoad(loading: boolean, thisPage: number) {
|
||||
try {
|
||||
setWaiting(true)
|
||||
setLoading(loading)
|
||||
setPage(thisPage)
|
||||
// TanStack Query for Discussions with Infinite Scroll
|
||||
const {
|
||||
data,
|
||||
fetchNextPage,
|
||||
hasNextPage,
|
||||
isFetchingNextPage,
|
||||
isLoading,
|
||||
refetch
|
||||
} = useInfiniteQuery({
|
||||
queryKey: ['discussions', { status, search, group }],
|
||||
queryFn: async ({ pageParam = 1 }) => {
|
||||
const hasil = await decryptToken(String(token?.current))
|
||||
const response = await apiGetDiscussionGeneral({ user: hasil, active: status, search: search, group: String(group), page: thisPage })
|
||||
if (thisPage == 1) {
|
||||
setData(response.data)
|
||||
} else if (thisPage > 1 && response.data.length > 0) {
|
||||
setData([...data, ...response.data])
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
setNameGroup(response.filter.name)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
} finally {
|
||||
setLoading(false)
|
||||
setWaiting(false)
|
||||
}
|
||||
}
|
||||
const response = await apiGetDiscussionGeneral({
|
||||
user: hasil,
|
||||
active: status,
|
||||
search: search,
|
||||
group: String(group),
|
||||
page: pageParam
|
||||
})
|
||||
return response;
|
||||
},
|
||||
initialPageParam: 1,
|
||||
getNextPageParam: (lastPage, allPages) => {
|
||||
return lastPage.data.length > 0 ? allPages.length + 1 : undefined;
|
||||
},
|
||||
enabled: !!token?.current,
|
||||
staleTime: 0,
|
||||
})
|
||||
|
||||
// Flatten pages into a single data array
|
||||
const flatData = useMemo(() => {
|
||||
return data?.pages.flatMap(page => page.data) || [];
|
||||
}, [data])
|
||||
|
||||
// Get nameGroup from the first available page
|
||||
const nameGroup = useMemo(() => {
|
||||
return data?.pages[0]?.filter?.name || "";
|
||||
}, [data])
|
||||
|
||||
// Refetch when manual update state changes
|
||||
useEffect(() => {
|
||||
handleLoad(false, 1)
|
||||
}, [update])
|
||||
|
||||
useEffect(() => {
|
||||
handleLoad(true, 1)
|
||||
}, [status, search, group])
|
||||
|
||||
|
||||
const loadMoreData = () => {
|
||||
if (waiting) return
|
||||
setTimeout(() => {
|
||||
handleLoad(false, page + 1)
|
||||
}, 1000);
|
||||
};
|
||||
refetch()
|
||||
}, [update, refetch])
|
||||
|
||||
const handleRefresh = async () => {
|
||||
setRefreshing(true)
|
||||
handleLoad(false, 1)
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
await queryClient.invalidateQueries({ queryKey: ['discussions'] })
|
||||
setRefreshing(false)
|
||||
};
|
||||
|
||||
const loadMoreData = () => {
|
||||
if (hasNextPage && !isFetchingNextPage) {
|
||||
fetchNextPage()
|
||||
}
|
||||
};
|
||||
|
||||
const arrSkeleton = [0, 1, 2, 3, 4]
|
||||
|
||||
const getItem = (_data: unknown, index: number): Props => ({
|
||||
id: data[index].id,
|
||||
title: data[index].title,
|
||||
desc: data[index].desc,
|
||||
status: data[index].status,
|
||||
total_komentar: data[index].total_komentar,
|
||||
createdAt: data[index].createdAt,
|
||||
id: flatData[index]?.id,
|
||||
title: flatData[index]?.title,
|
||||
desc: flatData[index]?.desc,
|
||||
status: flatData[index]?.status,
|
||||
total_komentar: flatData[index]?.total_komentar,
|
||||
createdAt: flatData[index]?.createdAt,
|
||||
})
|
||||
|
||||
return (
|
||||
@@ -132,18 +139,18 @@ export default function Discussion() {
|
||||
</View>
|
||||
<View style={[Styles.flex2, Styles.mt05]}>
|
||||
{
|
||||
loading ?
|
||||
isLoading ?
|
||||
arrSkeleton.map((item: any, i: number) => {
|
||||
return (
|
||||
<SkeletonContent key={i} />
|
||||
)
|
||||
})
|
||||
:
|
||||
data.length > 0
|
||||
flatData.length > 0
|
||||
?
|
||||
<VirtualizedList
|
||||
data={data}
|
||||
getItemCount={() => data.length}
|
||||
data={flatData}
|
||||
getItemCount={() => flatData.length}
|
||||
getItem={getItem}
|
||||
renderItem={({ item, index }: { item: Props, index: number }) => {
|
||||
return (
|
||||
@@ -153,16 +160,14 @@ export default function Discussion() {
|
||||
onPress={() => { router.push(`/discussion/${item.id}`) }}
|
||||
borderType="bottom"
|
||||
icon={
|
||||
// <View style={[Styles.iconContent]}>
|
||||
<MaterialIcons name="chat" size={25} color={colors.text} />
|
||||
// </View>
|
||||
}
|
||||
title={item.title}
|
||||
subtitle={
|
||||
status != "false" && <LabelStatus category={item.status === 1 ? "success" : "error"} text={item.status === 1 ? "BUKA" : "TUTUP"} size="small" />
|
||||
}
|
||||
rightTopInfo={item.createdAt}
|
||||
desc={item.desc.replace(/<[^>]*>?/gm, ' ').replace(/\r?\n|\r/g, ' ')}
|
||||
desc={item.desc?.replace(/<[^>]*>?/gm, ' ').replace(/\r?\n|\r/g, ' ')}
|
||||
leftBottomInfo={
|
||||
<View style={[Styles.rowItemsCenter]}>
|
||||
<Ionicons name="chatbox-ellipses-outline" size={18} color={colors.dimmed} style={Styles.mr05} />
|
||||
|
||||
Reference in New Issue
Block a user