Skip to content

Instantly share code, notes, and snippets.

@minedeljkovic
Last active December 24, 2015 15:45
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/9d4495c7ac5203def688 to your computer and use it in GitHub Desktop.
Save minedeljkovic/9d4495c7ac5203def688 to your computer and use it in GitHub Desktop.
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
// }
// }
*/
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 = {}, props = {}) {
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: props.selectionSuccessActionType, payload: state.selectedCountryId});
return state;
default:
return state;
}
}
import countrySelection from './countrySelection';
export const COUNTRY_SELECTION_SUCCESS = 'user-registration/COUNTRY_SELECTION_SUCCESS';
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, {selectionSuccessActionType: COUNTRY_SELECTION_SUCCESS})
};
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, {selectionSuccessActionType: COUNTRY_SELECTION_SUCCESS}))
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment