Skip to content

Commit da0b9e1

Browse files
committed
Fix pagination issues
1 parent 59c8f95 commit da0b9e1

10 files changed

Lines changed: 65 additions & 17 deletions

File tree

src/components/Master/Master/Master.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ const Master = ({
7575
onCloseForm,
7676
onDataSubmit,
7777
onCofirmDeleteMaster,
78+
searchStr,
79+
setSearchStr,
7880
} = useMaster({
7981
defaultLimit: Array.isArray(limits) && limits.length > 0 ? limits[0] : DEFAULT_LIMIT,
8082
routes,
@@ -115,6 +117,8 @@ const Master = ({
115117
canDelete={permissions?.destroy}
116118
getMastersList={getMastersList}
117119
canPartialUpdate={permissions?.partialUpdate}
120+
searchStr={searchStr}
121+
setSearchStr={setSearchStr}
118122
>
119123
{children ? (
120124
children

src/components/Master/MasterSearch/MasterSearch.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Input } from "../../../components/Common"
33
import { useMasterState } from "../../../context/MasterContext"
44

55
const MasterSearch = () => {
6-
const { getMastersList, t } = useMasterState()
6+
const { setSearchStr, t, setCurrentPage } = useMasterState()
77
const callerRef = useRef<NodeJS.Timeout | null>(null)
88
const [search, setSearch] = useState<string>("")
99

@@ -12,7 +12,8 @@ const MasterSearch = () => {
1212
if (callerRef.current) clearTimeout(callerRef.current)
1313

1414
callerRef.current = setTimeout(() => {
15-
getMastersList(str)
15+
setSearchStr(str)
16+
setCurrentPage(1)
1617
}, 300)
1718
}
1819

src/components/SubMaster/SubMaster/SubMaster.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ const SubMaster = ({
8787
onCofirmDeleteMaster,
8888
onImageUpload,
8989
onImageRemove,
90+
// pagination
91+
searchStr,
92+
setSearchStr,
9093
} = useSubMaster({
9194
defaultLimit: Array.isArray(limits) && limits.length > 0 ? limits[0] : DEFAULT_LIMIT,
9295
routes,
@@ -134,6 +137,9 @@ const SubMaster = ({
134137
canPartialUpdate={permissions?.partialUpdate}
135138
onChangeSequence={onChangeSequence}
136139
onConfirmSequence={onConfirmSequence}
140+
// Pagination
141+
searchStr={searchStr}
142+
setSearchStr={setSearchStr}
137143
>
138144
{children ? (
139145
children

src/components/SubMaster/SubMasterSearch/SubMasterSearch.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { useSubMasterState } from "../../../context/SubMasterContext"
55

66
const SubMasterSearch = () => {
77
const { selectedMaster } = useProviderState()
8-
const { getSubMastersList, t } = useSubMasterState()
8+
const { setSearchStr, t, setCurrentPage } = useSubMasterState()
99
const callerRef = useRef<NodeJS.Timeout | null>(null)
1010
const [search, setSearch] = useState<string>("")
1111

@@ -18,7 +18,9 @@ const SubMasterSearch = () => {
1818
if (callerRef.current) clearTimeout(callerRef.current)
1919

2020
callerRef.current = setTimeout(() => {
21-
getSubMastersList(str)
21+
// setCurrentPage internally calls getSubMastersList
22+
setSearchStr(str)
23+
setCurrentPage(1)
2224
}, 300)
2325
}
2426

src/context/MasterContext.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const MasterContextProvider = ({
4242
canDelete = false,
4343
canPartialUpdate = false,
4444
loader = undefined,
45+
searchStr = "",
46+
setSearchStr = () => {},
4547
// other
4648
children,
4749
}: MasterContextProviderProps) => {
@@ -78,6 +80,9 @@ const MasterContextProvider = ({
7880
canDelete,
7981
canPartialUpdate,
8082
loader,
83+
// pagination
84+
searchStr,
85+
setSearchStr,
8186
}}
8287
>
8388
{children}

src/context/SubMasterContext.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ const SubMasterContextProvider = ({
4848
sequencing = false,
4949
setSequencing = () => {},
5050
onConfirmSequence = () => {},
51+
// Pagination
52+
searchStr = "",
53+
setSearchStr = () => {},
5154
// other
5255
children,
5356
}: SubMasterContextProviderProps) => {
@@ -90,6 +93,9 @@ const SubMasterContextProvider = ({
9093
loader,
9194
onChangeSequence,
9295
onConfirmSequence,
96+
// Pagination
97+
searchStr,
98+
setSearchStr,
9399
}}
94100
>
95101
{children}

src/hook/useMaster.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const useMaster = ({ defaultLimit, routes, defaultSort = ["createdAt", 1], preCo
2222
const sortConfigRef = useRef<SortConfigType>(defaultSort)
2323

2424
const { baseUrl, token, dataGetter, paginationGetter, onError, onLogout, onSuccess } = useProviderState()
25-
const { currentPage, offsetRef, limitRef, currentPageRef } = usePagination({
25+
const { currentPage, offsetRef, limitRef, currentPageRef, searchStr, setSearchStr, searchRef } = usePagination({
2626
defaultLimit,
2727
})
2828

@@ -60,7 +60,6 @@ const useMaster = ({ defaultLimit, routes, defaultSort = ["createdAt", 1], preCo
6060
})
6161
if (response?.code === "SUCCESS") {
6262
setLoading(false)
63-
if (search) currentPageRef.current = 1
6463
setTotalPages(paginationGetter(response).totalPages)
6564
setTotalRecords(paginationGetter(response).totalDocs)
6665
return setList(dataGetter(response))
@@ -205,7 +204,7 @@ const useMaster = ({ defaultLimit, routes, defaultSort = ["createdAt", 1], preCo
205204
}
206205
const onChangeCurrentPage = (page: number): void => {
207206
currentPageRef.current = page
208-
getMastersList()
207+
getMastersList(searchRef.current)
209208
}
210209
useEffect(() => {
211210
getMastersList()
@@ -237,6 +236,10 @@ const useMaster = ({ defaultLimit, routes, defaultSort = ["createdAt", 1], preCo
237236
onCloseForm,
238237
onDataSubmit,
239238
onCofirmDeleteMaster,
239+
240+
// Search
241+
searchStr,
242+
setSearchStr,
240243
}
241244
}
242245

src/hook/usePagination.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const usePagination = ({ defaultLimit }: UsePaginationProps) => {
1010
const limitRef = useRef<number>(defaultLimit || constants.DEFAULT_LIMIT)
1111
const currentPageRef = useRef<number>(constants.DEFAULT_CURRENT_PAGE)
1212
const tempLimitRef = useRef<number>(constants.DEFAULT_OFFSET_PAYLOAD)
13+
const searchRef = useRef<string>("")
1314

1415
const setTempLimit = (value: number) => {
1516
tempLimitRef.current = value
@@ -19,14 +20,15 @@ const usePagination = ({ defaultLimit }: UsePaginationProps) => {
1920
limitRef.current = Number.parseInt(String(value), constants.DECIMAL_REDIX)
2021
offsetRef.current = constants.DEFAULT_OFFSET_PAYLOAD
2122
currentPageRef.current = constants.DEFAULT_CURRENT_PAGE
22-
console.log(value, limitRef.current)
2323
}
2424

2525
const changeCurrentPage = (value: number) => {
2626
offsetRef.current = Math.max(value - 1, 1) * limitRef.current
2727
currentPageRef.current = constants.DEFAULT_CURRENT_PAGE
2828
}
2929

30+
const setSearchStr = (value: string) => (searchRef.current = value)
31+
3032
return {
3133
tempSize: tempLimitRef.current,
3234
setTempSize: setTempLimit,
@@ -36,9 +38,12 @@ const usePagination = ({ defaultLimit }: UsePaginationProps) => {
3638
limitRef,
3739
tempLimitRef,
3840
currentPageRef,
41+
searchRef,
3942
setPageSize,
4043
currentPage: currentPageRef.current,
4144
setCurrentPage: changeCurrentPage,
45+
searchStr: searchRef.current,
46+
setSearchStr: setSearchStr,
4247
}
4348
}
4449

src/hook/useSubMaster.tsx

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,17 @@ const useSubMaster = ({ defaultLimit, routes, defaultSort = ["seq", 1], preConfi
2525

2626
const { baseUrl, token, dataGetter, paginationGetter, onError, onSuccess, onLogout, selectedMaster } =
2727
useProviderState()
28-
const { pageSize, offsetRef, currentPage, limitRef, currentPageRef, tempLimitRef } = usePagination({
28+
const {
29+
pageSize,
30+
offsetRef,
31+
currentPage,
32+
limitRef,
33+
currentPageRef,
34+
tempLimitRef,
35+
searchStr,
36+
setSearchStr,
37+
searchRef,
38+
} = usePagination({
2939
defaultLimit,
3040
})
3141

@@ -68,7 +78,6 @@ const useSubMaster = ({ defaultLimit, routes, defaultSort = ["seq", 1], preConfi
6878
})
6979
if (response?.code === "SUCCESS") {
7080
setLoading(false)
71-
currentPageRef.current = 1
7281
setTotalPages(paginationGetter(response).totalPages)
7382
setTotalRecords(paginationGetter(response).totalDocs)
7483
return setList(dataGetter(response))
@@ -326,17 +335,14 @@ const useSubMaster = ({ defaultLimit, routes, defaultSort = ["seq", 1], preConfi
326335
const onChangeCurrentPage = (page: number): void => {
327336
currentPageRef.current = page
328337
setSequencing(false)
329-
getSubMastersList()
338+
getSubMastersList(searchRef.current)
330339
}
331340

332341
useEffect(() => {
333342
if (selectedMaster) {
334-
if (currentPageRef.current === 1) {
335-
setSequencing(false)
336-
getSubMastersList()
337-
} else {
338-
currentPageRef.current = 1
339-
}
343+
currentPageRef.current = 1
344+
setSequencing(false)
345+
getSubMastersList()
340346
}
341347
}, [selectedMaster])
342348

@@ -372,6 +378,10 @@ const useSubMaster = ({ defaultLimit, routes, defaultSort = ["seq", 1], preConfi
372378
onCofirmDeleteMaster,
373379
onImageUpload,
374380
onImageRemove,
381+
382+
// Search
383+
searchStr,
384+
setSearchStr,
375385
}
376386
}
377387

types/global.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ interface MasterContextType {
198198
canDelete?: boolean
199199
canPartialUpdate?: boolean
200200
getMastersList: (search?: string) => Promise<void>
201+
// Pagination
202+
searchStr: string
203+
setSearchStr: (val: string) => void
201204
}
202205
interface SubMasterContextType {
203206
t: (key: string) => string
@@ -236,6 +239,9 @@ interface SubMasterContextType {
236239
sequencing: boolean
237240
setSequencing: (status: boolean) => void
238241
onConfirmSequence: () => void
242+
// Pagination
243+
searchStr: string
244+
setSearchStr: (val: string) => void
239245
}
240246
type onDelete = ({ data, confirmDelete }: { data: any; confirmDelete: () => void }) => any
241247
// \ End of Context

0 commit comments

Comments
 (0)