Skip to content

Instantly share code, notes, and snippets.

@Ledoux
Last active July 17, 2019 17:25
Show Gist options
  • Save Ledoux/ec5f19208d8f5b15706d866f77d0beb4 to your computer and use it in GitHub Desktop.
Save Ledoux/ec5f19208d8f5b15706d866f77d0beb4 to your computer and use it in GitHub Desktop.
import { mergeData, requestData } from "redux-saga-data";
function mapDispatchToProps(dispatch, ownProps) {
return {
requestGetOffer: () => {
const {
match: {
params: { offerId }
}
} = ownProps;
dispatch(
requestData({
apiPath: `/offers/${offerId}`,
// option in the case we want to normalize the nested stocks in the payload:
// normalizer: {
// stocks: {
// stateKey: "stocks"
// }
// }
// (see https://github.com/betagouv/fetch-normalize-data/blob/master/src/normalize/tests/getNormalizedMergedState.spec.js
// for all the kind of options)
})
);
},
requestPostStock: available => () => {
const {
match: {
params: { offerId }
}
} = ownProps;
dispatch(
requestData({
apiPath: "/stocks",
body: {
available,
offerId
},
handleSuccess: (state, action) => {
const {
payload: { datum }
} = action;
const offer = selectOfferById(state, offerId);
const stocks = [...offer.stocks].concat(datum);
dispatch(mergeData({ offers: [{ id: offerId, stocks }] }));
},
method: "POST"
})
);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment