From d7513850531e7abe05baf3d9dd4a1a2defc3dfe9 Mon Sep 17 00:00:00 2001 From: ani-kalpachka Date: Mon, 12 Aug 2024 19:31:09 +0300 Subject: [PATCH] update pagination using mui --- .../client/campaigns/CampaignsList.tsx | 147 ++++-------------- 1 file changed, 28 insertions(+), 119 deletions(-) diff --git a/src/components/client/campaigns/CampaignsList.tsx b/src/components/client/campaigns/CampaignsList.tsx index 221f916f9..3faff8c83 100644 --- a/src/components/client/campaigns/CampaignsList.tsx +++ b/src/components/client/campaigns/CampaignsList.tsx @@ -1,21 +1,25 @@ import { useMemo, useState, useEffect } from 'react' + import { CampaignResponse } from 'gql/campaigns' + import Image from 'next/image' -import { Box, Button, Grid, IconButton, Typography } from '@mui/material' -import KeyboardArrowLeftIcon from '@mui/icons-material/KeyboardArrowLeft' -import KeyboardArrowRightIcon from '@mui/icons-material/KeyboardArrowRight' + +import { Box, Grid, Pagination } from '@mui/material' + import useMobile from 'common/hooks/useMobile' -import theme from 'common/theme' import CampaignCard from './CampaignCard/CampaignCard' -type Props = { - campaignToShow: CampaignResponse[] +type Props = { + campaignToShow: CampaignResponse[] } export default function CampaignsList({ campaignToShow }: Props) { const { mobile } = useMobile() - const campaignsPerPage = 20 + const [currentPage, setCurrentPage] = useState(1) + + const campaignsPerPage = 20 + const totalCampaigns = campaignToShow?.length || 0 const totalPages = Math.ceil(totalCampaigns / campaignsPerPage) @@ -29,93 +33,8 @@ export default function CampaignsList({ campaignToShow }: Props) { return campaignToShow?.slice(startIndex, endIndex) ?? [] }, [campaignToShow, currentPage]) - const handleNextPage = () => { - if (currentPage < totalPages) { - setCurrentPage(prev => prev + 1) - } - } - - const handlePreviousPage = () => { - if (currentPage > 1) { - setCurrentPage(prev => prev - 1) - } - } - - const handlePageClick = (pageNumber: number) => { - setCurrentPage(pageNumber) - } - - const renderPageButtons = () => { - const renderPageButton = (pageNumber: number) => ( - - ) - - const pageButtons = [] - const startPage = Math.max(1, currentPage - 2) - const endPage = Math.min(totalPages, startPage + 2) - const showEllipsis = endPage < totalPages - - if (startPage > 1) { - pageButtons.push(renderPageButton(1)) - } - - for (let i = startPage; i <= endPage; i++) { - pageButtons.push(renderPageButton(i)) - } - - if (showEllipsis) { - pageButtons.push( - - ... - - ) - } - - if (showEllipsis || endPage < totalPages) { - pageButtons.push(renderPageButton(totalPages)) - } - - return pageButtons + const handlePageChange = (event: React.ChangeEvent, page: number) => { + setCurrentPage(page) } return ( @@ -129,37 +48,27 @@ export default function CampaignsList({ campaignToShow }: Props) { ))} {totalCampaigns > campaignsPerPage && ( - - - - - - {renderPageButtons()} - - - - + + )} {mobile ? ( - Information artboard mobile + Information artboard mobile ) : ( Information artboard )}