From a4fefbe9ad2c09f59795a0c18d082c16b4ab9169 Mon Sep 17 00:00:00 2001 From: dDogge <> Date: Fri, 15 Dec 2023 12:41:02 +0100 Subject: [PATCH] Added comments to PostsContainer --- src/components/PostsContainer.tsx | 41 +++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/components/PostsContainer.tsx b/src/components/PostsContainer.tsx index 46011ab..5da65f6 100644 --- a/src/components/PostsContainer.tsx +++ b/src/components/PostsContainer.tsx @@ -8,6 +8,14 @@ import { VirtualizedList } from "react-native"; const WINDOW_SIZE = 20; +/** + * PostsContainer component for displaying a list of posts. + * + * @component + * @param {object} props - React component props. + * @param {object} props.navigation - Navigation object for navigating between screens. + * @returns {JSX.Element} - Rendered PostsContainer component. + */ export function PostsContainer({ navigation }): JSX.Element { const [postData, setPostData] = React.useState([]); const [offset, setOffset] = React.useState(0); @@ -18,6 +26,11 @@ export function PostsContainer({ navigation }): JSX.Element { fetchMorePosts(); }; + /** + * Handles the pull-to-refresh action. + * @function + * @returns {void} + */ function onRefresh(): void { refreshPosts(); } @@ -26,22 +39,35 @@ export function PostsContainer({ navigation }): JSX.Element { refreshPosts(); }, []); - // Full refresh + /** + * Full refresh of posts. + * @function + * @returns {void} + */ function refreshPosts(): void { setRefreshing(true); setPostData([]); setOffset(WINDOW_SIZE); + + // Fetches posts from the API and updates the state. async function fetchPosts(): Promise { setPostData(await getPostsInterval(0, WINDOW_SIZE)); } + fetchPosts(); setRefreshing(false); } + /** + * Fetches more posts from the API and updates the state. + * @function + * @returns {void} + */ function fetchMorePosts(): void { async function fetchPosts(): Promise { setPostData([...postData, ...(await getPostsInterval(offset, WINDOW_SIZE))]); } + fetchPosts(); } @@ -52,8 +78,10 @@ export function PostsContainer({ navigation }): JSX.Element { return ( <> + {/* Navigation button to create a new post */} navigation.navigate("New")} text="New Post" /> + {/* VirtualizedList to render the list of posts */} ; -} +} \ No newline at end of file