Skip to content

Commit

Permalink
feat(trends): better pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
ahonestla committed Jan 17, 2025
1 parent 761b22d commit 8950b0e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
32 changes: 21 additions & 11 deletions client/src/pages/trends/components/panel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ function TrendsViewItems() {
}
}, [inView, fetchNextPage])

const navigateToPage = (page: number) => {
handlePageChange(page)
window.scrollTo(0, scrollYTop)
}

if (isFetching && !isFetchingNextPage) return <BaseSkeleton height="500px" />
if (!trends?.pages || error) return <div>no data</div>

const shouldChangePage = trends.pageParams.length >= scrollPerPage
const totalPages = Math.ceil((trends?.pages?.[0]?.total || 0) / (itemsPerScroll * scrollPerPage))

console.log(totalPages)

return (
<>
<div className="fr-accordions-group">
Expand All @@ -44,36 +47,43 @@ function TrendsViewItems() {
{!isFetchingNextPage && shouldChangePage && (
<Container fluid className="fr-mt-2w">
<ButtonGroup isInlineFrom="xs" align="center">
<Button variant="tertiary" onClick={() => handlePageChange(currentPage - 1)} disabled={currentPage === 1}>
<Button
variant="tertiary"
icon="arrow-left-s-line"
iconPosition="left"
onClick={() => navigateToPage(currentPage - 1)}
disabled={currentPage === 1}
>
{intl.formatMessage({ id: "trends.views.page-previous" })}
</Button>
{currentPage > 1 && (
<Button variant="tertiary" onClick={() => handlePageChange(1)}>
<Button variant="tertiary" onClick={() => navigateToPage(1)}>
{1}
</Button>
)}
{currentPage > 3 && <Button variant="tertiary">{"..."}</Button>}
{currentPage > 2 && (
<Button variant="tertiary" onClick={() => handlePageChange(currentPage - 1)}>
<Button variant="tertiary" onClick={() => navigateToPage(currentPage - 1)}>
{currentPage - 1}
</Button>
)}
<Button variant="secondary">{currentPage}</Button>
{currentPage < totalPages - 1 && (
<Button variant="tertiary" onClick={() => handlePageChange(currentPage + 1)}>
<Button variant="tertiary" onClick={() => navigateToPage(currentPage + 1)}>
{currentPage + 1}
</Button>
)}
{currentPage < totalPages - 2 && <Button variant="tertiary">{"..."}</Button>}
{currentPage < totalPages && (
<Button variant="tertiary" onClick={() => handlePageChange(totalPages)}>
<Button variant="tertiary" onClick={() => navigateToPage(totalPages)}>
{totalPages}
</Button>
)}
<Button
variant="tertiary"
onClick={() => {
handlePageChange(currentPage + 1)
window.scrollTo(0, scrollYTop)
}}
icon="arrow-right-s-line"
iconPosition="right"
onClick={() => navigateToPage(currentPage + 1)}
disabled={!hasNextPage}
>
{intl.formatMessage({ id: "trends.views.page-next" })}
Expand Down
4 changes: 2 additions & 2 deletions client/src/pages/trends/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
"trends.views.header.count": "Volume ({max})",
"trends.views.header.diff": "Variation (1 year)",
"trends.views.header.trend": "Trend ({count} years)",
"trends.views.page-next": "Next page",
"trends.views.page-previous": "Previous page",
"trends.views.page-next": "Next",
"trends.views.page-previous": "Back",
"trends.line-chart.xAxis.accessibility.description": "Years from {min, number} to {max, number}",
"trends.line-chart.yAxis.accessibility.description": "Number of {source}",
"trends.line-chart.yAxis.title.text": "Number of {source}",
Expand Down
4 changes: 2 additions & 2 deletions client/src/pages/trends/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
"trends.views.header.count": "Volume ({max})",
"trends.views.header.diff": "Variation (1 an)",
"trends.views.header.trend": "Tendance ({count} ans)",
"trends.views.page-next": "Page suivante",
"trends.views.page-previous": "Page précédente",
"trends.views.page-next": "Suivante",
"trends.views.page-previous": "Précédent",
"trends.line-chart.xAxis.accessibility.description": "Années de {min, number} à {max, number}",
"trends.line-chart.yAxis.accessibility.description": "Nombre de {source}",
"trends.line-chart.yAxis.title.text": "Nombre de {source}",
Expand Down

0 comments on commit 8950b0e

Please sign in to comment.