Skip to content

Commit 1f22453

Browse files
committed
Return the save promise in the submit function
1 parent d2b3036 commit 1f22453

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

packages/ra-core/src/dataProvider/useMutation.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import useDataProviderWithDeclarativeSideEffects from './useDataProviderWithDecl
3131
* @param {Object} options
3232
* @param {string} options.action Redux action type
3333
* @param {boolean} options.undoable Set to true to run the mutation locally before calling the dataProvider
34+
* @param {boolean} options.returnPromise Set to true to return the result promise of the mutation
3435
* @param {Function} options.onSuccess Side effect function to be executed upon success or failure, e.g. { onSuccess: response => refresh() } }
3536
* @param {Function} options.onFailure Side effect function to be executed upon failure, e.g. { onFailure: error => notify(error.message) } }
3637
* @param {boolean} options.withDeclarativeSideEffectsSupport Set to true to support legacy side effects (e.g. { onSuccess: { refresh: true } })
@@ -52,6 +53,7 @@ import useDataProviderWithDeclarativeSideEffects from './useDataProviderWithDecl
5253
* - {Object} options
5354
* - {string} options.action Redux action type
5455
* - {boolean} options.undoable Set to true to run the mutation locally before calling the dataProvider
56+
* - {boolean} options.returnPromise Set to true to return the result promise of the mutation
5557
* - {Function} options.onSuccess Side effect function to be executed upon success or failure, e.g. { onSuccess: response => refresh() } }
5658
* - {Function} options.onFailure Side effect function to be executed upon failure, e.g. { onFailure: error => notify(error.message) } }
5759
* - {boolean} withDeclarativeSideEffectsSupport Set to true to support legacy side effects (e.g. { onSuccess: { refresh: true } })
@@ -156,21 +158,27 @@ const useMutation = (
156158

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

159-
finalDataProvider[params.type]
161+
const returnPromise = options && options.returnPromise;
162+
163+
const promise = finalDataProvider[params.type]
160164
.apply(
161165
finalDataProvider,
162166
typeof params.resource !== 'undefined'
163167
? [params.resource, params.payload, params.options]
164168
: [params.payload, params.options]
165169
)
166-
.then(({ data, total }) => {
170+
.then(response => {
171+
const { data, total } = response;
167172
setState({
168173
data,
169174
error: null,
170175
loaded: true,
171176
loading: false,
172177
total,
173178
});
179+
if (returnPromise) {
180+
return response;
181+
}
174182
})
175183
.catch(errorFromResponse => {
176184
setState({
@@ -180,7 +188,14 @@ const useMutation = (
180188
loading: false,
181189
total: null,
182190
});
191+
if (returnPromise) {
192+
throw errorFromResponse;
193+
}
183194
});
195+
196+
if (returnPromise) {
197+
return promise;
198+
}
184199
},
185200
[
186201
// deep equality, see https://github.com/facebook/react/issues/14476#issuecomment-471199055
@@ -204,6 +219,7 @@ export interface Mutation {
204219
export interface MutationOptions {
205220
action?: string;
206221
undoable?: boolean;
222+
returnPromise?: boolean;
207223
onSuccess?: (response: any) => any | Object;
208224
onFailure?: (error?: any) => any | Object;
209225
withDeclarativeSideEffectsSupport?: boolean;

packages/ra-core/src/form/FormWithRedirect.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ const FormWithRedirect: FC<FormWithRedirectProps> = ({
111111
finalInitialValues,
112112
values
113113
);
114-
onSave.current(sanitizedValues, finalRedirect);
114+
return onSave.current(sanitizedValues, finalRedirect);
115115
} else {
116-
onSave.current(values, finalRedirect);
116+
return onSave.current(values, finalRedirect);
117117
}
118118
};
119119

0 commit comments

Comments
 (0)