Skip to content

Commit

Permalink
Return the save promise in the submit function
Browse files Browse the repository at this point in the history
  • Loading branch information
alanpoulain committed Jan 14, 2021
1 parent d2b3036 commit 1f22453
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
20 changes: 18 additions & 2 deletions packages/ra-core/src/dataProvider/useMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import useDataProviderWithDeclarativeSideEffects from './useDataProviderWithDecl
* @param {Object} options
* @param {string} options.action Redux action type
* @param {boolean} options.undoable Set to true to run the mutation locally before calling the dataProvider
* @param {boolean} options.returnPromise Set to true to return the result promise of the mutation
* @param {Function} options.onSuccess Side effect function to be executed upon success or failure, e.g. { onSuccess: response => refresh() } }
* @param {Function} options.onFailure Side effect function to be executed upon failure, e.g. { onFailure: error => notify(error.message) } }
* @param {boolean} options.withDeclarativeSideEffectsSupport Set to true to support legacy side effects (e.g. { onSuccess: { refresh: true } })
Expand All @@ -52,6 +53,7 @@ import useDataProviderWithDeclarativeSideEffects from './useDataProviderWithDecl
* - {Object} options
* - {string} options.action Redux action type
* - {boolean} options.undoable Set to true to run the mutation locally before calling the dataProvider
* - {boolean} options.returnPromise Set to true to return the result promise of the mutation
* - {Function} options.onSuccess Side effect function to be executed upon success or failure, e.g. { onSuccess: response => refresh() } }
* - {Function} options.onFailure Side effect function to be executed upon failure, e.g. { onFailure: error => notify(error.message) } }
* - {boolean} withDeclarativeSideEffectsSupport Set to true to support legacy side effects (e.g. { onSuccess: { refresh: true } })
Expand Down Expand Up @@ -156,21 +158,27 @@ const useMutation = (

setState(prevState => ({ ...prevState, loading: true }));

finalDataProvider[params.type]
const returnPromise = options && options.returnPromise;

const promise = finalDataProvider[params.type]
.apply(
finalDataProvider,
typeof params.resource !== 'undefined'
? [params.resource, params.payload, params.options]
: [params.payload, params.options]
)
.then(({ data, total }) => {
.then(response => {
const { data, total } = response;
setState({
data,
error: null,
loaded: true,
loading: false,
total,
});
if (returnPromise) {
return response;
}
})
.catch(errorFromResponse => {
setState({
Expand All @@ -180,7 +188,14 @@ const useMutation = (
loading: false,
total: null,
});
if (returnPromise) {
throw errorFromResponse;
}
});

if (returnPromise) {
return promise;
}
},
[
// deep equality, see https://github.com/facebook/react/issues/14476#issuecomment-471199055
Expand All @@ -204,6 +219,7 @@ export interface Mutation {
export interface MutationOptions {
action?: string;
undoable?: boolean;
returnPromise?: boolean;
onSuccess?: (response: any) => any | Object;
onFailure?: (error?: any) => any | Object;
withDeclarativeSideEffectsSupport?: boolean;
Expand Down
4 changes: 2 additions & 2 deletions packages/ra-core/src/form/FormWithRedirect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ const FormWithRedirect: FC<FormWithRedirectProps> = ({
finalInitialValues,
values
);
onSave.current(sanitizedValues, finalRedirect);
return onSave.current(sanitizedValues, finalRedirect);
} else {
onSave.current(values, finalRedirect);
return onSave.current(values, finalRedirect);
}
};

Expand Down

0 comments on commit 1f22453

Please sign in to comment.