(
params: UseSelectAllParams
)
| 41 | * ); |
| 42 | */ |
| 43 | export 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 | }); |
no test coverage detected