Last active
July 17, 2019 17:25
-
-
Save Ledoux/ec5f19208d8f5b15706d866f77d0beb4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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