Skip to content

Commit f25d36c

Browse files
refactor: simplify pagination (#66)
* refactor: make `page` required in PaginateFn and let StickyNavigation handle page reset * test: adjust test to new implementation * chore: implement pr suggestions
1 parent ca30a46 commit f25d36c

File tree

4 files changed

+13
-20
lines changed

4 files changed

+13
-20
lines changed

src/components/line-list/__tests__/line-list.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ describe('LineList', () => {
163163
).getByText('50')
164164
await user.click(option50)
165165

166-
expect(onPaginate).toHaveBeenCalledWith({ pageSize: 50 })
166+
expect(onPaginate).toHaveBeenCalledWith({ page: 1, pageSize: 50 })
167167
})
168168

169169
it('displays pagination information correctly', async () => {

src/components/line-list/sticky-pagination.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const StickyPagination = ({
4040
)
4141
const onPageSizeChange = useCallback(
4242
(pageSize: number) => {
43-
onPaginate({ pageSize })
43+
onPaginate({ page: 1, pageSize })
4444
},
4545
[onPaginate]
4646
)

src/components/line-list/types.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ export type DataSortPayload = {
5252
dimension: string
5353
direction?: SortDirection
5454
}
55-
export type PaginatePayload = {
56-
page?: number
57-
pageSize?: number
58-
}
5955
export type DataSortFn = (payload: DataSortPayload) => void
60-
export type PaginateFn = (payload: PaginatePayload) => void
56+
export type PaginateFn = (payload: { page: number; pageSize?: number }) => void
6157
export type ColumnHeaderClickFn = (cleanedHeaderName: string) => void

src/components/plugin-wrapper/line-list-plugin.tsx

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import { useLineListAnalyticsData } from './hooks/use-line-list-analytics-data'
44
import type { MetadataInput } from '@components/app-wrapper/metadata-helpers'
55
import { LineList } from '@components/line-list'
66
import type { LineListAnalyticsData } from '@components/line-list'
7-
import type { DataSortFn, DataSortPayload } from '@components/line-list/types'
7+
import type {
8+
DataSortFn,
9+
DataSortPayload,
10+
PaginateFn,
11+
} from '@components/line-list/types'
812
import { transformVisualization } from '@modules/visualization'
913
import type { CurrentUser, CurrentVisualization, SortDirection } from '@types'
1014

@@ -19,9 +23,6 @@ type LineListPluginProps = {
1923
onResponseReceived?: (metadata: MetadataInput) => void
2024
}
2125

22-
const FIRST_PAGE: number = 1
23-
const PAGE_SIZE: number = 100
24-
2526
export const LineListPlugin: FC<LineListPluginProps> = ({
2627
displayProperty,
2728
visualization: originalVisualization,
@@ -52,8 +53,8 @@ export const LineListPlugin: FC<LineListPluginProps> = ({
5253
...newPagination,
5354
}),
5455
{
55-
page: FIRST_PAGE,
56-
pageSize: PAGE_SIZE,
56+
page: 1,
57+
pageSize: 100,
5758
}
5859
)
5960

@@ -62,15 +63,11 @@ export const LineListPlugin: FC<LineListPluginProps> = ({
6263
? visualization.sorting[0]
6364
: { dimension: undefined, direction: undefined }
6465

65-
const onPaginate = useCallback(({ page, pageSize }) => {
66+
const onPaginate = useCallback<PaginateFn>(({ page, pageSize }) => {
6667
if (pageSize) {
67-
setPagination({ page: pageSize ? FIRST_PAGE : page, pageSize })
68-
} else if (page) {
69-
setPagination({ page })
68+
setPagination({ page, pageSize })
7069
} else {
71-
throw new Error(
72-
'onPaginate was called with neither a page nor pageSize. At least one is expected'
73-
)
70+
setPagination({ page })
7471
}
7572
}, [])
7673

0 commit comments

Comments
 (0)