MCPcopy
hub / github.com/marmelab/react-admin / useSelectAll

Function useSelectAll

packages/ra-core/src/controller/list/useSelectAll.tsx:43–118  ·  view source on GitHub ↗
(
    params: UseSelectAllParams
)

Source from the content-addressed store, hash-verified

41 * );
42 */
43export const useSelectAll = (
44 params: UseSelectAllParams
45): UseSelectAllResult => {
46 const { sort, filter, storeKey, disableSyncWithStore } = params;
47 const resource = useResourceContext(params);
48 if (!resource) {
49 throw new Error(
50 'useSelectAll should be used inside a ResourceContextProvider or passed a resource prop'
51 );
52 }
53 const dataProvider = useDataProvider();
54 const queryClient = useQueryClient();
55 const [, { select }] = useRecordSelection({
56 resource,
57 storeKey,
58 disableSyncWithStore,
59 });
60 const notify = useNotify();
61
62 const handleSelectAll = useEvent(
63 async ({
64 queryOptions = {},
65 limit = 250,
66 }: HandleSelectAllParams = {}) => {
67 const { meta, onSuccess, onError, ...otherQueryOptions } =
68 queryOptions;
69 try {
70 const results = await queryClient.fetchQuery({
71 queryKey: [
72 resource,
73 'getList',
74 {
75 pagination: { page: 1, perPage: limit },
76 sort,
77 filter,
78 meta,
79 },
80 ],
81 queryFn: () =>
82 dataProvider.getList(resource, {
83 pagination: {
84 page: 1,
85 perPage: limit,
86 },
87 sort,
88 filter,
89 meta,
90 }),
91 ...otherQueryOptions,
92 });
93
94 const allIds = results.data?.map(({ id }) => id) || [];
95 select(allIds);
96 if (allIds.length === limit) {
97 notify('ra.message.select_all_limit_reached', {
98 messageArgs: { max: limit },
99 type: 'warning',
100 });

Callers 3

SelectAllButtonFunction · 0.90
useListControllerFunction · 0.90

Calls 7

useResourceContextFunction · 0.90
useDataProviderFunction · 0.90
useRecordSelectionFunction · 0.90
useNotifyFunction · 0.90
useEventFunction · 0.90
onErrorFunction · 0.85
onSuccessFunction · 0.50

Tested by

no test coverage detected