Skip to content

Commit 20aba12

Browse files
committed
changes
1 parent 710e7ba commit 20aba12

File tree

3 files changed

+16
-23
lines changed

3 files changed

+16
-23
lines changed

src/Common/Hooks/useStateFilters/types.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ export interface UseStateFiltersReturnType<T>
2929
| 'changePageSize'
3030
| 'offset'
3131
| 'pageSize'
32-
> {
33-
/*
34-
* Handler for resetting the pagination to the initial state
35-
*/
36-
resetPagination: () => void
37-
}
32+
> {}
3833

39-
export interface PaginationType {
40-
pageSize: number
41-
offset: number
42-
}
34+
export interface PaginationType<T> extends Pick<UseUrlFiltersReturnType<T>, 'pageSize' | 'pageNumber'> {}

src/Common/Hooks/useStateFilters/useStateFilters.tsx

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import { useState } from 'react'
1818
import { DEFAULT_BASE_PAGE_SIZE, SortingOrder } from '../../Constants'
1919
import { PaginationType, UseStateFiltersProps, UseStateFiltersReturnType } from './types'
20+
import { DEFAULT_PAGE_NUMBER } from '../useUrlFilters/constants'
2021

2122
/**
2223
* Generic hook for implementing state based pagination, search, sorting.
@@ -40,24 +41,23 @@ const useStateFilters = <T = string,>({
4041
})
4142
const { sortBy, sortOrder } = sortingConfig
4243

43-
const [pagination, setPagination] = useState<PaginationType>({
44+
const [pagination, setPagination] = useState<PaginationType<T>>({
4445
pageSize: DEFAULT_BASE_PAGE_SIZE,
45-
offset: 0,
46+
pageNumber: DEFAULT_PAGE_NUMBER,
4647
})
48+
const offset = pagination.pageSize * (pagination.pageNumber - 1)
4749

4850
const changePage = (pageNo: number): void => {
49-
const offset = pagination.pageSize * (pageNo - 1)
5051
setPagination({
5152
...pagination,
52-
offset,
53+
pageNumber: pageNo,
5354
})
5455
}
5556

5657
const changePageSize = (_pageSize: number): void => {
5758
setPagination({
5859
...pagination,
5960
pageSize: _pageSize,
60-
offset: 0,
6161
})
6262
}
6363

@@ -75,6 +75,10 @@ const useStateFilters = <T = string,>({
7575
sortBy: _sortBy,
7676
sortOrder: order,
7777
}))
78+
setPagination({
79+
...pagination,
80+
pageNumber: DEFAULT_PAGE_NUMBER,
81+
})
7882
}
7983

8084
const clearFilters = () => {
@@ -84,21 +88,14 @@ const useStateFilters = <T = string,>({
8488
})
8589
}
8690

87-
const resetPagination = () => {
88-
setPagination({
89-
pageSize: DEFAULT_BASE_PAGE_SIZE,
90-
offset: 0,
91-
})
92-
}
93-
9491
return {
9592
...sortingConfig,
9693
handleSorting,
9794
clearFilters,
9895
...pagination,
9996
changePage,
10097
changePageSize,
101-
resetPagination,
98+
offset,
10299
}
103100
}
104101

src/Common/Hooks/useUrlFilters/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export type UseUrlFiltersReturnType<T, K = unknown> = K & {
3232
* Currently applied page size
3333
*/
3434
pageSize: number
35+
/**
36+
* PageNumber for the current page
37+
*/
38+
pageNumber?: number
3539
/**
3640
* Handler for updating the current page
3741
*/

0 commit comments

Comments
 (0)