Skip to content

Instantly share code, notes, and snippets.

View Ledoux's full-sized avatar

ledoux Ledoux

View GitHub Profile
{
"id": "F5UQ",
"name": "Projets Rencontre - salle du Clous Brest",
"stocks": [
{ "available": 5, "id": "K3EQ" },
{ "available": 5, "id": "KYSA" },
]
}
import { applyMiddleware, combineReducers, createStore } from "redux";
import thunk from "redux-thunk";
import { createDataReducer } from "redux-thunk-data";
const storeEnhancer = applyMiddleware(thunk.withExtraArgument({ rootUrl: "https://foo.com" }));
const rootReducer = combineReducers({ data: createDataReducer() });
const store = createStore(rootReducer, storeEnhancer);
import { applyMiddleware, combineReducers, createStore } from "redux";
import createSagaMiddleware from "redux-saga";
import { all } from "redux-saga/effects";
import { createDataReducer, watchDataActions } from "redux-saga-data";
const sagaMiddleware = createSagaMiddleware();
const storeEnhancer = applyMiddleware(sagaMiddleware);
function* rootSaga() {
yield all([
watchDataActions({
...
dispatch(
requestData({
apiPath: `/offers/${offerId}`,
normalizer: {
stocks: "stocks"
}
})
);
...
import { mergeData, requestData } from "redux-saga-data";
function mapDispatchToProps(dispatch, ownProps) {
return {
requestGetOffer: () => {
const {
match: {
params: { offerId }
}
} = ownProps;
function mapStateToProps(state, ownProps) {
const {
match: {
params: { offerId }
}
} = ownProps;
return {
offer: selectOfferById(state, offerId),
stocks: selectStocksByOfferId(state, offerId)
};
const selectStocksByOfferId = createSelector(
state => state.data.stocks,
state, offerId => offerId,
(stocks, offerId) => stocks && stocks.filter(stock => stock.offerId === offerId)
)
function mapStateToProps(state, ownProps) {
const { match: { params: { offerId } } } = ownProps
return {
offer: selectOfferById(state, offerId),
...
class Offer extends Component {
componentDidMount () {
const { dispatch, match: { params: { offerId } } } = this.props
dispatch(requestData({
apiPath: `/offers/${offerId}`,
normalizer: {
stocks: 'stocks'
}
import { createSelector } from 'reselect'
const selectOfferById = createSelector(
state => state.data.offers,
(state, offerId) => offerId,
(offers, offerId) => (offers || []).find(offer => offer.id === offerId)
)
export default selectOfferById
const OfferContainer = compose(
withRouter,
connect(
mapStateToProps,
mapDispatchToProps
)
)(Offer);