From b59456505aa688a5e480ac7141b25ea34f35bb3d Mon Sep 17 00:00:00 2001 From: Imbus Date: Thu, 14 Dec 2023 23:14:33 +0100 Subject: [PATCH] Fixing infinite scroll --- src/components/PostsContainer.tsx | 62 ++++++++++++++++++++++++------- src/util/api.ts | 8 +++- 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/src/components/PostsContainer.tsx b/src/components/PostsContainer.tsx index a67ceb5..71da5eb 100644 --- a/src/components/PostsContainer.tsx +++ b/src/components/PostsContainer.tsx @@ -1,26 +1,62 @@ import React from "react"; -import { View, Text } from "react-native"; -import { getPosts, Post } from "../util/api"; +import { RefreshControl } from "react-native"; +import { getPostsInterval, Post } from "../util/api"; import { PostView } from "./PostView"; import { style } from "../util/style"; +import { SafeAreaView } from "react-native"; +import { VirtualizedList } from "react-native"; export function PostsContainer(): JSX.Element { - const [posts, setPosts] = React.useState([]); + const [postData, setPostData] = React.useState([]); + const [page, setPage] = React.useState(0); + const [refreshing, setRefreshing] = React.useState(false); + + const handleEndReached = (): void => { + setPage((prevPage) => prevPage + 1); + }; React.useEffect(() => { + refreshPosts(); + }, [page]); + + function refreshPosts(): void { async function fetchPosts(): Promise { - const posts = await getPosts(); - setPosts(posts); + const newPosts = await getPostsInterval(page*10, 10) + setPostData((oldPosts: Post[]) => [...oldPosts, ...newPosts]); } fetchPosts(); - }, []); + } + function onRefresh(): void { + setRefreshing(true); + setPage(0); + setPostData(postData.slice(0, 10)); + setRefreshing(false); + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const getItemCount = (_data: unknown): number => postData.length; + + const getItem = (_data: unknown, index: number): Post => postData[index]; return ( - - Hello World! - {posts.map((post) => ( - - ))} - + + } + keyExtractor={(item: Post) => item.id} + getItemCount={getItemCount} + getItem={getItem} + onEndReached={handleEndReached} + onEndReachedThreshold={0.4} + refreshControl={ + + } + /> + ); -} \ No newline at end of file +} diff --git a/src/util/api.ts b/src/util/api.ts index a72078b..bd3c62e 100644 --- a/src/util/api.ts +++ b/src/util/api.ts @@ -56,6 +56,12 @@ export async function getPosts(): Promise { return data; } +export async function getPostsInterval(offset: number, limit: number): Promise { + const res = await fetch(URL + `/api/posts?offset=${offset}&limit=${limit}`); + const data = await res.json(); + return data; +} + /** * Fetches a specific post by its ID from the API. * @async @@ -63,7 +69,7 @@ export async function getPosts(): Promise { * @param {string} id - The ID of the post to fetch. * @returns {Promise} A promise that resolves to the requested post. */ -export async function getPost(id: string): Promise { +export async function getPost(id: number): Promise { const res = await fetch(URL + `/api/posts/${id}`); const data = await res.json(); return data;