From b337822a2d3f289986ed859cc987389ce50222a7 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 18 Dec 2023 02:27:33 -0500 Subject: [PATCH] fix: use a initialLoad state and activeContent to better track when we should be fetching data --- app/components/Feed.tsx | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/app/components/Feed.tsx b/app/components/Feed.tsx index 6093607..cd4157b 100644 --- a/app/components/Feed.tsx +++ b/app/components/Feed.tsx @@ -24,39 +24,52 @@ const Feed = ({ const [selectedFilter, setSelectedFilter] = useState<(typeof filters)[number]>("latest"); const [currentPage, setCurrentPage] = useState(0); + const [initialLoad, setIsInitialLoad] = useState(true); const fetcher = useFetcher(); const Icon = ICON_DICT[icon]; useEffect(() => { - if (currentPage !== 0) { + if (!initialLoad) { // Fetch data for subsequent pages fetcher.load(`/api/feed?filter=${selectedFilter}&page=${currentPage}`); } - }, [selectedFilter, currentPage]); + }, [initialLoad, selectedFilter, currentPage]); useEffect(() => { - if (fetcher.data && currentPage !== 0) { + if (fetcher.data) { setContent((prevContent) => [...prevContent, ...fetcher.data.data]); } - }, [fetcher.data, currentPage]); + }, [initialLoad, fetcher.data]); const handleFilterChange = (filter: (typeof filters)[number]) => { + setIsInitialLoad(false); + setContent([]); setSelectedFilter(filter); setCurrentPage(0); }; const handleLoadMore = () => { + setIsInitialLoad(false); setCurrentPage((prevPage) => prevPage + 1); }; - if (currentPage) { - if (fetcher.state === "loading") { - return
Loading...
; - } + let activeContent = content; - if (!fetcher.data) { - return
Failed to load
; + if (!initialLoad) { + if (fetcher.state === "loading") { + activeContent = [ + { + createdAt: new Date(), + updatedAt: new Date(), + title: "Loading", + id: 0, + slug: "loading", + cid: "", + url: "", + siteKey: "", + }, + ]; } } @@ -90,7 +103,7 @@ const Feed = ({ >
- {content.map((item, index) => { + {activeContent.map((item, index) => { return (