From 05414013ce7ed5749868a7306f622ba897d0c477 Mon Sep 17 00:00:00 2001 From: daniyal Date: Wed, 18 Sep 2024 21:41:55 +0500 Subject: [PATCH] feat: implemented logic for games page --- src/components/Pagination.tsx | 98 +++++++++++++++++++++++++++++++++++ src/pages/games.tsx | 93 +++++++++++++-------------------- 2 files changed, 134 insertions(+), 57 deletions(-) diff --git a/src/components/Pagination.tsx b/src/components/Pagination.tsx index 5e6e878..591ad3c 100644 --- a/src/components/Pagination.tsx +++ b/src/components/Pagination.tsx @@ -38,3 +38,101 @@ export const Pagination = React.memo( ) } ) + +type PaginationWithPageNumbersProps = { + currentPage: number + totalPages: number + handlePageChange: (page: number) => void +} + +export const PaginationWithPageNumbers = ({ + currentPage, + totalPages, + handlePageChange +}: PaginationWithPageNumbersProps) => { + // Function to render the pagination controls with page numbers + const renderPagination = () => { + const pagesToShow = 5 // Number of page numbers to show around the current page + const pageNumbers: (number | string)[] = [] // Array to store page numbers and ellipses + + // Case when the total number of pages is less than or equal to the limit + if (totalPages <= pagesToShow + 2) { + for (let i = 1; i <= totalPages; i++) { + pageNumbers.push(i) // Add all pages to the pagination + } + } else { + // Add the first page (always visible) + pageNumbers.push(1) + + // Calculate the range of pages to show around the current page + const startPage = Math.max(2, currentPage - Math.floor(pagesToShow / 2)) + const endPage = Math.min( + totalPages - 1, + currentPage + Math.floor(pagesToShow / 2) + ) + + // Add ellipsis if there are pages between the first page and the startPage + if (startPage > 2) pageNumbers.push('...') + + // Add the pages around the current page + for (let i = startPage; i <= endPage; i++) { + pageNumbers.push(i) + } + + // Add ellipsis if there are pages between the endPage and the last page + if (endPage < totalPages - 1) pageNumbers.push('...') + + // Add the last page (always visible) + pageNumbers.push(totalPages) + } + + // Map over the array and render each page number or ellipsis + return pageNumbers.map((page, index) => { + if (typeof page === 'number') { + // For actual page numbers, render clickable boxes + return ( +
handlePageChange(page)} // Navigate to the selected page + > +

{page}

+
+ ) + } else { + // For ellipses, render non-clickable dots + return ( +

+ ... +

+ ) + } + }) + } + + return ( +
+
+
+
handlePageChange(currentPage - 1)} + > + +
+
+ {renderPagination()} +
+
handlePageChange(currentPage + 1)} + > + +
+
+
+
+ ) +} diff --git a/src/pages/games.tsx b/src/pages/games.tsx index d2ed7bc..4174648 100644 --- a/src/pages/games.tsx +++ b/src/pages/games.tsx @@ -1,9 +1,29 @@ -import '../styles/pagination.css' -import '../styles/styles.css' -import '../styles/search.css' +import { PaginationWithPageNumbers } from 'components/Pagination' +import { MAX_GAMES_PER_PAGE } from 'constants.ts' +import { useGames } from 'hooks' +import { useState } from 'react' import { GameCard } from '../components/GameCard' +import '../styles/pagination.css' +import '../styles/search.css' +import '../styles/styles.css' export const GamesPage = () => { + const games = useGames() + const [currentPage, setCurrentPage] = useState(1) + + // Pagination logic + const totalGames = games.length + const totalPages = Math.ceil(totalGames / MAX_GAMES_PER_PAGE) + const startIndex = (currentPage - 1) * MAX_GAMES_PER_PAGE + const endIndex = startIndex + MAX_GAMES_PER_PAGE + const currentGames = games.slice(startIndex, endIndex) + + const handlePageChange = (page: number) => { + if (page >= 1 && page <= totalPages) { + setCurrentPage(page) + } + } + return (
@@ -11,7 +31,7 @@ export const GamesPage = () => {
-

Games (WIP)

+

Games

@@ -35,61 +55,20 @@ export const GamesPage = () => {
- - - - - -
-
-
-
- + {currentGames.map((game) => ( + + ))}
+