Skip to content

Commit 8ca75ac

Browse files
committed
fix counter inconsistency, move logic into use-pagination-hook
1 parent a167b91 commit 8ca75ac

File tree

2 files changed

+19
-31
lines changed

2 files changed

+19
-31
lines changed

components/reports/reports-view.tsx

+14-20
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function ReportsView({ reports }: IPageData) {
3434
sortingOptions.createdNewestFirst.value,
3535
);
3636
let filteredReports = useMemo(() => reports, [reports]);
37-
const itemsPerPage = 10;
37+
const itemsPerPage = 4;
3838

3939
const filterOptions = useMemo(() => {
4040
return createFilterOptions(reports);
@@ -169,28 +169,22 @@ export function ReportsView({ reports }: IPageData) {
169169
}
170170
/>
171171
</PaginationItem>
172-
{pageNumbers
173-
.filter((pageNum) =>
174-
[currentPage - 1, currentPage, currentPage + 1].includes(
175-
pageNum,
176-
),
177-
)
178-
.map((pageNum, index) => (
179-
<PaginationItem
180-
onClick={() => loadPage(pageNum)}
181-
className="hover:cursor-pointer"
182-
key={`page-${pageNum}`}
183-
>
184-
<PaginationLink isActive={currentPage === pageNum}>
185-
{pageNum}
186-
</PaginationLink>
187-
</PaginationItem>
188-
))}
189-
{maxPage > 3 && (
172+
{pageNumbers.map((pageNum, index) => (
173+
<PaginationItem
174+
onClick={() => loadPage(pageNum)}
175+
className="hover:cursor-pointer"
176+
key={`page-${pageNum}`}
177+
>
178+
<PaginationLink isActive={currentPage === pageNum}>
179+
{pageNum}
180+
</PaginationLink>
181+
</PaginationItem>
182+
))}
183+
{maxPage > 3 && currentPage < maxPage - 1 ? (
190184
<PaginationItem>
191185
<PaginationEllipsis />
192186
</PaginationItem>
193-
)}
187+
) : null}
194188
<PaginationItem className="hover:cursor-pointer">
195189
<PaginationNext
196190
onClick={() =>

hooks/use-pagination.ts

+5-11
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,12 @@ export const usePagination = <GT,>(items: GT[], itemsPerPage: number) => {
3131
() => items.length > itemsPerPage,
3232
[items.length, itemsPerPage],
3333
);
34-
const pageNumbers = useMemo(
35-
() => Array.from({ length: maxPage }, (_, i) => i + 1),
36-
[maxPage],
37-
);
38-
39-
// const currentPageItems = useMemo(() => {
40-
// const start = (currentPage - 1) * itemsPerPage;
41-
// const end = start + itemsPerPage;
42-
// return needsPagination ? items.slice(start, end) : items;
43-
// }, [currentPage, items, itemsPerPage, needsPagination]);
34+
const pageNumbers = useMemo(() => {
35+
const range = Array.from({ length: 3 }, (_, i) => currentPage + i);
36+
return currentPage === 1 ? range : currentPage === maxPage ? range.map(num => num - 2) : range.map(num => num - 1);
37+
}, [maxPage, currentPage]);
4438

45-
// removing these from useMemo(), need recalculation to render correct reports
39+
// no 'useMemo' for these, we need to recalculate to render correct reports
4640
// when filter/sort options are updated
4741
const start = (currentPage - 1) * itemsPerPage;
4842
const end = start + itemsPerPage;

0 commit comments

Comments
 (0)