Skip to content

Instantly share code, notes, and snippets.

@minedeljkovic
Last active December 24, 2015 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minedeljkovic/7347c0b528110889aa50 to your computer and use it in GitHub Desktop.
Save minedeljkovic/7347c0b528110889aa50 to your computer and use it in GitHub Desktop.
Implementing simple scenario using redux-side-effects, as a example for discussion https://github.com/rackt/redux/issues/291#issuecomment-167128155
import {createStore, applyMiddleware} from 'redux';
import {createEffectCapableStore} from 'redux-side-effects';
import createLogger from 'redux-logger';
import userRegistration, {START_COUNTRY_SELECTION} from './userRegistration';
import {CHANGE_SELECTION, CONFIRM_SELECTION} from './countrySelection';
const storeFactory = createEffectCapableStore(applyMiddleware(createLogger())(createStore));
const store = storeFactory(userRegistration);
// scenario that should select a country for a user
store.dispatch({ type: START_COUNTRY_SELECTION });
store.dispatch({ type: CHANGE_SELECTION, payload: 1 });
store.dispatch({ type: CONFIRM_SELECTION });
// expected state:
// {
// "user": {
// "countryId": 1
// },
// "countrySelection": null
// }
/*
// scenario that should raise a warning based on state in countrySelection slice
store.dispatch({ type: START_COUNTRY_SELECTION });
store.dispatch({ type: CONFIRM_SELECTION });
// expected state:
// {
// "user": {
// },
// "countrySelection": {
// "selectedCountryId": null,
// "showNoSelectionWarning": true
// }
// }
*/
import {COUNTRY_SELECTION_SUCCESS} from './globalActions.js';
export const CHANGE_SELECTION = 'country-selection/CHANGE_SELECTION';
export const CONFIRM_SELECTION = 'country-selection/CONFIRM_SELECTION';
export default function *countrySelection(state = { selectedCountryId: null, showNoSelectionWarning: false }, action = {}) {
switch (action.type) {
case CHANGE_SELECTION:
return {
...state,
selectedCountryId: action.payload
};
case CONFIRM_SELECTION:
if (state.selectedCountryId == null) return {...state, showNoSelectionWarning: true};
yield dispatch => dispatch({type: COUNTRY_SELECTION_SUCCESS, payload: state.selectedCountryId});
return state;
default:
return state;
}
}
export const COUNTRY_SELECTION_SUCCESS = 'COUNTRY_SELECTION_SUCCESS';
import {COUNTRY_SELECTION_SUCCESS} from './globalActions.js';
import countrySelection from './countrySelection';
export const START_COUNTRY_SELECTION = 'user-registration/START_COUNTRY_SELECTION';
export default function *userRegistration(state = { user: {}, countrySelection: null }, action = {}) {
switch (action.type) {
case START_COUNTRY_SELECTION:
return {
...state,
countrySelection: yield* countrySelection(undefined, action)
};
case COUNTRY_SELECTION_SUCCESS:
return {
...state,
countrySelection: null,
user: {
...state.user,
countryId: action.payload
}
};
default:
return {
...state,
countrySelection: state.countrySelection && (yield* countrySelection(state.countrySelection, action))
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment