Skip to content

Commit 924b2d5

Browse files
committed
add logic to store access different pagination for each stores
1 parent bd5c527 commit 924b2d5

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

packages/ra-core/src/controller/list/useList.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import isEqual from 'lodash/isEqual';
44

55
import { removeEmpty } from '../../util';
66
import { FilterPayload, RaRecord, SortPayload } from '../../types';
7-
import { ResourceContextValue, useResourceContext } from '../../core';
7+
import { useResourceContext } from '../../core';
88
import usePaginationState from '../usePaginationState';
99
import useSortState from '../useSortState';
1010
import { useRecordSelection } from './useRecordSelection';
@@ -92,12 +92,45 @@ export const useList = <RecordType extends RaRecord = any, ErrorType = Error>(
9292
total: data ? data.length : undefined,
9393
}));
9494

95+
// Store pagination states for each storeKey
96+
const storeKeyPaginationRef = useRef<{
97+
[key: string]: { page: number; perPage: number };
98+
}>({});
99+
95100
// pagination logic
96101
const { page, setPage, perPage, setPerPage } = usePaginationState({
97102
page: initialPage,
98103
perPage: initialPerPage,
99104
});
100105

106+
useEffect(() => {
107+
if (!resource) return;
108+
// Check if storeKey exists in the pagination store
109+
const currentPagination = storeKeyPaginationRef.current[resource];
110+
if (currentPagination) {
111+
// Restore existing pagination state for the storeKey
112+
if (
113+
page !== currentPagination.page ||
114+
perPage !== currentPagination.perPage
115+
) {
116+
setPage(currentPagination.page);
117+
setPerPage(currentPagination.perPage);
118+
}
119+
} else {
120+
setPage(initialPage);
121+
setPerPage(initialPerPage);
122+
}
123+
storeKeyPaginationRef.current[resource] = { page, perPage };
124+
}, [
125+
resource,
126+
setPage,
127+
setPerPage,
128+
initialPage,
129+
initialPerPage,
130+
page,
131+
perPage,
132+
]);
133+
101134
// sort logic
102135
const { sort, setSort: setSortState } = useSortState(initialSort);
103136
const setSort = useCallback(

0 commit comments

Comments
 (0)