-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from cdrxiv/Shane98c/pagination
noscript functionality and seo improvements
- Loading branch information
Showing
11 changed files
with
337 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { useSearchParams } from 'next/navigation' | ||
import { Box, Flex } from 'theme-ui' | ||
import { Link } from '../components' | ||
|
||
type PaginationProps = { | ||
totalCount: number | undefined | ||
itemsPerPage: number | undefined | ||
currentPage: number | ||
hasNextPage: boolean | ||
} | ||
|
||
const generatePaginationLinks = ( | ||
totalCount: number | undefined, | ||
itemsPerPage: number | undefined, | ||
currentPage: number, | ||
) => { | ||
if (!totalCount || !itemsPerPage) return [] | ||
const totalPages = Math.ceil(totalCount / itemsPerPage) | ||
let pages = [] | ||
|
||
if (totalPages < 8) { | ||
pages = Array(totalPages) | ||
.fill(null) | ||
.map((d, i) => i + 1) | ||
} else if (currentPage <= 4) { | ||
pages = [1, 2, 3, 4, 5, 6, '...', totalPages] | ||
} else if (totalPages - currentPage <= 4) { | ||
pages = [ | ||
1, | ||
'...', | ||
...Array(6) | ||
.fill(null) | ||
.map((d, i) => totalPages - 5 + i), | ||
] | ||
} else { | ||
pages = [ | ||
1, | ||
'...', | ||
...Array(5) | ||
.fill(null) | ||
.map((d, i) => currentPage - 2 + i), | ||
'...', | ||
totalPages, | ||
] | ||
} | ||
return pages | ||
} | ||
|
||
const Pagination = ({ | ||
totalCount, | ||
itemsPerPage, | ||
currentPage, | ||
hasNextPage, | ||
}: PaginationProps) => { | ||
const paginationLinks = generatePaginationLinks( | ||
totalCount, | ||
itemsPerPage, | ||
currentPage, | ||
) | ||
const searchParams = useSearchParams() | ||
|
||
const createPageUrl = (page: number) => | ||
`?${new URLSearchParams({ ...Object.fromEntries(searchParams), page: page.toString() })}` | ||
|
||
if (paginationLinks.length === 0 && !hasNextPage && currentPage <= 1) { | ||
return null | ||
} | ||
|
||
return ( | ||
<Flex sx={{ justifyContent: 'center', gap: 2, mt: 5 }}> | ||
{currentPage > 1 && ( | ||
<Link href={createPageUrl(currentPage - 1)}>Previous</Link> | ||
)} | ||
{paginationLinks.map((page, i) => | ||
page === '...' || page === currentPage ? ( | ||
<Box as='span' sx={{ mt: '1px' }} key={i}> | ||
{page} | ||
</Box> | ||
) : ( | ||
<Link key={i} href={createPageUrl(page as number)}> | ||
{page} | ||
</Link> | ||
), | ||
)} | ||
{hasNextPage && <Link href={createPageUrl(currentPage + 1)}>Next</Link>} | ||
</Flex> | ||
) | ||
} | ||
|
||
export default Pagination |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.