Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pagination on Campaigns page #1897

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 40 additions & 44 deletions src/components/client/campaigns/CampaignsList.tsx
Original file line number Diff line number Diff line change
@@ -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<boolean>(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<CampaignResponse[]>(() => {
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<unknown>, page: number) => {
setCurrentPage(page)
}

return (
<Grid
container
justifyContent="center"
spacing={4}
sx={(theme) => ({
width: `calc(100% + ${theme.spacing(1.5)})`,
marginLeft: `-${theme.spacing(2.75)}`,
})}>
<Grid container justifyContent="center" spacing={4}>
{campaigns?.map((campaign, index) => (
<Grid key={campaign.id} item xs={12} sm={6} lg={3}>
<Box
sx={{
textAlign: 'center',
}}>
<Box sx={{ textAlign: 'center' }}>
<CampaignCard index={index} campaign={campaign} />
</Box>
</Grid>
))}
{campaignToShow && campaignToShow?.length > numberOfMinimalShownCampaigns && (
<Grid container justifyContent="center">
<Button
onClick={() => setAll((prev) => !prev)}
sx={{
fontFamily: "'Lato', sans-serif",
fontSize: theme.typography.pxToRem(16),
fontWeight: 600,
color: theme.palette.common.black,
letterSpacing: '0.4px',
textDecoration: 'underline',
marginTop: 0,

'&:hover': {
backgroundColor: 'transparent',
textDecoration: 'underline',
},
}}>
{all ? t('campaigns:cta.see-less') : t('campaigns:cta.see-all')}
</Button>
{totalCampaigns > campaignsPerPage && (
<Grid container justifyContent="center" sx={{ mt: 5 }} alignItems="center">
<Pagination
count={totalPages}
page={currentPage}
onChange={handlePageChange}
siblingCount={1}
boundaryCount={1}
size="large"
/>
</Grid>
)}

<Grid item xs={12} textAlign="center">
<Box sx={{ my: 10 }}>
{mobile ? (
Expand Down
Loading