Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix useUpdateMany does not support returnPromise option #7740

Merged
merged 1 commit into from
May 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions packages/ra-core/src/dataProvider/useUpdateMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const useUpdateMany = <
resource?: string,
params: Partial<UpdateManyParams<Partial<RecordType>>> = {},
options: UseUpdateManyOptions<RecordType, MutationError> = {}
): UseUpdateManyResult<RecordType, MutationError> => {
): UseUpdateManyResult<RecordType, boolean, MutationError> => {
const dataProvider = useDataProvider();
const queryClient = useQueryClient();
const { ids, data, meta } = params;
Expand Down Expand Up @@ -276,9 +276,15 @@ export const useUpdateMany = <
unknown,
Partial<UseUpdateManyMutateParams<RecordType>>,
unknown
> & { mutationMode?: MutationMode } = {}
> & { mutationMode?: MutationMode; returnPromise?: boolean } = {}
) => {
const { mutationMode, onSuccess, onSettled, onError } = updateOptions;
const {
mutationMode,
returnPromise,
onSuccess,
onSettled,
onError,
} = updateOptions;

// store the hook time params *at the moment of the call*
// because they may change afterwards, which would break the undoable mode
Expand All @@ -289,7 +295,19 @@ export const useUpdateMany = <
mode.current = mutationMode;
}

if (returnPromise && mode.current !== 'pessimistic') {
console.warn(
'The returnPromise parameter can only be used if the mutationMode is set to pessimistic'
);
}

if (mode.current === 'pessimistic') {
if (returnPromise) {
return mutation.mutateAsync(
{ resource: callTimeResource, ...callTimeParams },
{ onSuccess, onSettled, onError }
);
}
return mutation.mutate(
{ resource: callTimeResource, ...callTimeParams },
{ onSuccess, onSettled, onError }
Expand Down Expand Up @@ -418,6 +436,7 @@ export type UseUpdateManyOptions<

export type UseUpdateManyResult<
RecordType extends RaRecord = any,
TReturnPromise extends boolean = boolean,
MutationError = unknown
> = [
(
Expand All @@ -428,8 +447,8 @@ export type UseUpdateManyResult<
MutationError,
Partial<UseUpdateManyMutateParams<RecordType>>,
unknown
> & { mutationMode?: MutationMode }
) => Promise<void>,
> & { mutationMode?: MutationMode; returnPromise?: TReturnPromise }
) => Promise<TReturnPromise extends true ? Array<RecordType['id']> : void>,
UseMutationResult<
Array<RecordType['id']>,
MutationError,
Expand Down