Added comments to PostsContainer

This commit is contained in:
dDogge 2023-12-15 12:41:02 +01:00
parent 7b01c9e69a
commit a4fefbe9ad

View file

@ -8,6 +8,14 @@ import { VirtualizedList } from "react-native";
const WINDOW_SIZE = 20; 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 { export function PostsContainer({ navigation }): JSX.Element {
const [postData, setPostData] = React.useState<Post[]>([]); const [postData, setPostData] = React.useState<Post[]>([]);
const [offset, setOffset] = React.useState<number>(0); const [offset, setOffset] = React.useState<number>(0);
@ -18,6 +26,11 @@ export function PostsContainer({ navigation }): JSX.Element {
fetchMorePosts(); fetchMorePosts();
}; };
/**
* Handles the pull-to-refresh action.
* @function
* @returns {void}
*/
function onRefresh(): void { function onRefresh(): void {
refreshPosts(); refreshPosts();
} }
@ -26,22 +39,35 @@ export function PostsContainer({ navigation }): JSX.Element {
refreshPosts(); refreshPosts();
}, []); }, []);
// Full refresh /**
* Full refresh of posts.
* @function
* @returns {void}
*/
function refreshPosts(): void { function refreshPosts(): void {
setRefreshing(true); setRefreshing(true);
setPostData([]); setPostData([]);
setOffset(WINDOW_SIZE); setOffset(WINDOW_SIZE);
// Fetches posts from the API and updates the state.
async function fetchPosts(): Promise<void> { async function fetchPosts(): Promise<void> {
setPostData(await getPostsInterval(0, WINDOW_SIZE)); setPostData(await getPostsInterval(0, WINDOW_SIZE));
} }
fetchPosts(); fetchPosts();
setRefreshing(false); setRefreshing(false);
} }
/**
* Fetches more posts from the API and updates the state.
* @function
* @returns {void}
*/
function fetchMorePosts(): void { function fetchMorePosts(): void {
async function fetchPosts(): Promise<void> { async function fetchPosts(): Promise<void> {
setPostData([...postData, ...(await getPostsInterval(offset, WINDOW_SIZE))]); setPostData([...postData, ...(await getPostsInterval(offset, WINDOW_SIZE))]);
} }
fetchPosts(); fetchPosts();
} }
@ -52,8 +78,10 @@ export function PostsContainer({ navigation }): JSX.Element {
return ( return (
<> <>
{/* Navigation button to create a new post */}
<NavButton onPress={() => navigation.navigate("New")} text="New Post" /> <NavButton onPress={() => navigation.navigate("New")} text="New Post" />
<SafeAreaView style={style.postsContainer}> <SafeAreaView style={style.postsContainer}>
{/* VirtualizedList to render the list of posts */}
<VirtualizedList <VirtualizedList
data={postData} data={postData}
initialNumToRender={4} initialNumToRender={4}
@ -76,6 +104,15 @@ export function PostsContainer({ navigation }): JSX.Element {
); );
} }
/**
* NavButton component for rendering a navigation button.
*
* @component
* @param {object} props - React component props.
* @param {Function} props.onPress - Function to be called on button press.
* @param {string} props.text - Text to be displayed on the button.
* @returns {JSX.Element} - Rendered NavButton component.
*/
function NavButton({ function NavButton({
onPress, onPress,
text, text,
@ -84,4 +121,4 @@ function NavButton({
text: string; text: string;
}): JSX.Element { }): JSX.Element {
return <Button title={text} onPress={onPress} />; return <Button title={text} onPress={onPress} />;
} }