Skip to content

Commit a3e5c31

Browse files
committed
add logic to store access different pagination for each stores
1 parent 125477e commit a3e5c31

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
@@ -3,7 +3,7 @@ import get from 'lodash/get';
33
import isEqual from 'lodash/isEqual';
44
import { removeEmpty } from '../../util';
55
import { FilterPayload, RaRecord, SortPayload } from '../../types';
6-
import { ResourceContextValue, useResourceContext } from '../../core';
6+
import { useResourceContext } from '../../core';
77
import usePaginationState from '../usePaginationState';
88
import useSortState from '../useSortState';
99
import { useRecordSelection } from './useRecordSelection';
@@ -91,12 +91,45 @@ export const useList = <RecordType extends RaRecord = any>(
9191
total: data ? data.length : undefined,
9292
}));
9393

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

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

0 commit comments

Comments
 (0)