Skip to content

Commit

Permalink
Merge pull request #57 from icssc/47-list-of-items-pagination
Browse files Browse the repository at this point in the history
47 list of items pagination
  • Loading branch information
NwinNwin authored Apr 15, 2024
2 parents 570e770 + cf5472f commit 1999d6b
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/components/ResultsBar/ResultsBar.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useContext, useCallback } from "react";
import { useContext, useCallback, useState } from "react";
import "./ResultsBar.css";
import ResultCard from "../ResultCard/ResultCard";
import { Box, Flex, Text } from "@chakra-ui/react";
import { Box, Flex, Text, Button } from "@chakra-ui/react";
import DataContext from "../../context/DataContext";
import { UserAuth } from "../../context/AuthContext";
export default function ResultsBar({
Expand All @@ -14,6 +14,8 @@ export default function ResultsBar({
const { data, setData } = useContext(DataContext);
const { user } = UserAuth();

const [itemsonScreenLimit, setItemsOnScreenLimit] = useState(10);

// Define callback function to return filtered items (filtered according to search bar and filter markers)
const filterItem = useCallback(
(item) => {
Expand Down Expand Up @@ -68,8 +70,35 @@ export default function ResultsBar({
[setFocusLocation, onResultsBarClose, setData, setLeaderboard]
);

// Callback function that increases the number of items displayed on the screen by 10
const handleLoadMore = useCallback(() => {
setItemsOnScreenLimit(itemsonScreenLimit + 10);
}, [itemsonScreenLimit]);

const loadMoreButton = (
<Button
onClick={handleLoadMore}
variant="outline"
colorScheme="blue"
width="100%"
height={"80px"}
marginTop="10px"
marginBottom="10px"
fontSize={"xl"}
>
Load More
</Button>
);

// Retrieve all items that meet the filter criteria
const allResults = data.filter(filterItem).map(mapItem);

// Display only the first 10 items on the screen, all items if there are less than 10 items left to be loaded
const viewableResults = allResults.slice(
0,
Math.min(itemsonScreenLimit, allResults.length)
);

// Define JSX for empty results bar (no result cards)
const noResults = (
<Flex height="80%" width="100%" justifyContent="center" alignItems="center">
Expand All @@ -87,7 +116,8 @@ export default function ResultsBar({
overflowY="scroll"
overflowX={"hidden"}
>
{allResults.length > 0 ? allResults : noResults}
{allResults.length > 0 ? viewableResults : noResults}
{viewableResults.length < allResults.length && loadMoreButton}
</Box>
);
}

0 comments on commit 1999d6b

Please sign in to comment.