Skip to content

Commit

Permalink
Merge pull request #3385 from marmelab/fix-with-dataprovider-callback
Browse files Browse the repository at this point in the history
[RFR] Ensure dataProvider callback sideeffect is called when using withDataProvider
  • Loading branch information
fzaninotto authored Jul 7, 2019
2 parents a246449 + 55fa38e commit 4d71274
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions packages/ra-core/src/util/withDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ interface DispatchProps {
const mapDispatchToProps = (dispatch): DispatchProps => ({
dataProvider: (type, resource: string, payload: any, meta: any = {}) =>
new Promise((resolve, reject) => {
const onSuccess = get(meta, 'onSuccess', {});
const onFailure = get(meta, 'onFailure', {});

const action = {
type: 'CUSTOM_FETCH',
payload,
Expand All @@ -21,15 +24,28 @@ const mapDispatchToProps = (dispatch): DispatchProps => ({
resource,
fetch: type,
onSuccess: {
...get(meta, 'onSuccess', {}),
callback: ({ payload: response }) => resolve(response),
...onSuccess,
callback: ({ payload: response }) => {
if (onSuccess.callback) {
onSuccess.callback(response);
}

resolve(response);
},
},
onFailure: {
...get(meta, 'onFailure', {}),
callback: ({ error }) =>
reject(
new Error(error.message ? error.message : error)
),
callback: ({ error }) => {
const sanitizedError = new Error(
error.message ? error.message : error
);

if (onFailure.callback) {
onFailure.callback(sanitizedError);
}

reject(sanitizedError);
},
},
},
};
Expand Down

0 comments on commit 4d71274

Please sign in to comment.