@@ -31,6 +31,7 @@ import useDataProviderWithDeclarativeSideEffects from './useDataProviderWithDecl
31
31
* @param {Object } options
32
32
* @param {string } options.action Redux action type
33
33
* @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
34
35
* @param {Function } options.onSuccess Side effect function to be executed upon success or failure, e.g. { onSuccess: response => refresh() } }
35
36
* @param {Function } options.onFailure Side effect function to be executed upon failure, e.g. { onFailure: error => notify(error.message) } }
36
37
* @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
52
53
* - {Object} options
53
54
* - {string} options.action Redux action type
54
55
* - {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
55
57
* - {Function} options.onSuccess Side effect function to be executed upon success or failure, e.g. { onSuccess: response => refresh() } }
56
58
* - {Function} options.onFailure Side effect function to be executed upon failure, e.g. { onFailure: error => notify(error.message) } }
57
59
* - {boolean} withDeclarativeSideEffectsSupport Set to true to support legacy side effects (e.g. { onSuccess: { refresh: true } })
@@ -156,21 +158,27 @@ const useMutation = (
156
158
157
159
setState ( prevState => ( { ...prevState , loading : true } ) ) ;
158
160
159
- finalDataProvider [ params . type ]
161
+ const returnPromise = options && options . returnPromise ;
162
+
163
+ const promise = finalDataProvider [ params . type ]
160
164
. apply (
161
165
finalDataProvider ,
162
166
typeof params . resource !== 'undefined'
163
167
? [ params . resource , params . payload , params . options ]
164
168
: [ params . payload , params . options ]
165
169
)
166
- . then ( ( { data, total } ) => {
170
+ . then ( response => {
171
+ const { data, total } = response ;
167
172
setState ( {
168
173
data,
169
174
error : null ,
170
175
loaded : true ,
171
176
loading : false ,
172
177
total,
173
178
} ) ;
179
+ if ( returnPromise ) {
180
+ return response ;
181
+ }
174
182
} )
175
183
. catch ( errorFromResponse => {
176
184
setState ( {
@@ -180,7 +188,14 @@ const useMutation = (
180
188
loading : false ,
181
189
total : null ,
182
190
} ) ;
191
+ if ( returnPromise ) {
192
+ throw errorFromResponse ;
193
+ }
183
194
} ) ;
195
+
196
+ if ( returnPromise ) {
197
+ return promise ;
198
+ }
184
199
} ,
185
200
[
186
201
// deep equality, see https://github.com/facebook/react/issues/14476#issuecomment-471199055
@@ -204,6 +219,7 @@ export interface Mutation {
204
219
export interface MutationOptions {
205
220
action ?: string ;
206
221
undoable ?: boolean ;
222
+ returnPromise ?: boolean ;
207
223
onSuccess ?: ( response : any ) => any | Object ;
208
224
onFailure ?: ( error ?: any ) => any | Object ;
209
225
withDeclarativeSideEffectsSupport ?: boolean ;
0 commit comments