diff --git a/src/components/client/campaigns/CampaignsList.tsx b/src/components/client/campaigns/CampaignsList.tsx index a15e56593..3faff8c83 100644 --- a/src/components/client/campaigns/CampaignsList.tsx +++ b/src/components/client/campaigns/CampaignsList.tsx @@ -1,69 +1,65 @@ -import { useMemo, useState } from 'react' +import { useMemo, useState, useEffect } from 'react' + import { CampaignResponse } from 'gql/campaigns' + import Image from 'next/image' -import { useTranslation } from 'next-i18next' -import { Box, Button, Grid } from '@mui/material' +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 { t } = useTranslation() const { mobile } = useMobile() - const numberOfMinimalShownCampaigns = 12 - const [all, setAll] = useState(false) + + const [currentPage, setCurrentPage] = useState(1) + + const campaignsPerPage = 20 + + const totalCampaigns = campaignToShow?.length || 0 + const totalPages = Math.ceil(totalCampaigns / campaignsPerPage) + + useEffect(() => { + setCurrentPage(1) + }, [campaignToShow]) + const campaigns = useMemo(() => { - if (all) { - return campaignToShow ?? [] - } - return campaignToShow?.slice(0, numberOfMinimalShownCampaigns) ?? [] - }, [campaignToShow, all]) + const startIndex = (currentPage - 1) * campaignsPerPage + const endIndex = startIndex + campaignsPerPage + return campaignToShow?.slice(startIndex, endIndex) ?? [] + }, [campaignToShow, currentPage]) + + const handlePageChange = (event: React.ChangeEvent, page: number) => { + setCurrentPage(page) + } return ( - ({ - width: `calc(100% + ${theme.spacing(1.5)})`, - marginLeft: `-${theme.spacing(2.75)}`, - })}> + {campaigns?.map((campaign, index) => ( - + ))} - {campaignToShow && campaignToShow?.length > numberOfMinimalShownCampaigns && ( - - + {totalCampaigns > campaignsPerPage && ( + + )} + {mobile ? (