Last active
December 24, 2015 15:45
-
-
Save minedeljkovic/9d4495c7ac5203def688 to your computer and use it in GitHub Desktop.
Slightly different variation of https://gist.github.com/minedeljkovic/7347c0b528110889aa50
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 {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 | |
// } | |
// } | |
*/ |
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
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; | |
} | |
} |
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 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