diff --git a/app/home/[slug]/page.js b/app/home/[slug]/page.js index 61766d2..50c51af 100644 --- a/app/home/[slug]/page.js +++ b/app/home/[slug]/page.js @@ -1,10 +1,12 @@ "use client"; -import useSWR from "swr"; +import useSWRInfinite from "swr/infinite"; import { ReleaseSection } from "../../components/ReleaseSection/ReleaseSection"; import { Spinner } from "../../components/Spinner/Spinner"; +import { useState, useEffect } from "react"; +import { useScrollPosition } from "@/app/hooks/useScrollPosition"; -const fetcher = async (...args) => { - const res = await fetch(...args); +const fetcher = async url => { + const res = await fetch(url); if (!res.ok) { const error = new Error("An error occurred while fetching the data."); @@ -24,11 +26,35 @@ const SectionTitleMapping = { }; export default function HomeStatus({ params }) { - const { data, error, isLoading } = useSWR( - `/api/home?status=${params.slug}`, - fetcher + const getKey = (pageIndex, previousPageData) => { + if (previousPageData && !previousPageData.content.length) return null; + return `/api/home?status=${params.slug}&page=${pageIndex}`; + }; + + const { data, error, isLoading, size, setSize } = useSWRInfinite( + getKey, + fetcher, + {"initialSize": 2, "revalidateFirstPage": false} ); + const [content, setContent] = useState(null); + useEffect(() => { + if (data) { + let allReleases = []; + for (let i = 0; i < data.length; i++) { + allReleases.push(...data[i].content); + } + setContent(allReleases); + } + }, [data]); + + const scrollPosition = useScrollPosition(); + useEffect(() => { + if (scrollPosition >= 98 && scrollPosition <= 99) { + setSize(size + 1) + } + }, [scrollPosition]); + if (error) return