Skip to content

Instantly share code, notes, and snippets.

@hanipcode
Created February 27, 2017 02:35
Show Gist options
  • Select an option

  • Save hanipcode/96a914bb91b4112a35ce04637b9a43aa to your computer and use it in GitHub Desktop.

Select an option

Save hanipcode/96a914bb91b4112a35ce04637b9a43aa to your computer and use it in GitHub Desktop.
simple reducer with error using immutable data
const initialState = Map({
is_loading: false,
is_finish: null,
error: {
code: '',
message: '',
},
});
const formReducer = (state = initialState, action) => {
switch (action.type) {
case FORM.START_POST:
return state.withMutations(state =>
state.set('is_loading', true)
.set('error', Map())
.set('is_finish', false)
);
case FORM.SUCCESS_POST:
setTimeout(() => state.set('is_finish', false), 2500);
return state.withMutations(state =>
state.set('is_loading', false)
.set('error', Map())
.set('is_finish', true)
);
case FORM.FAILED_POST:
setTimeout(() => state.set('is_error', false), 2500);
setTimeout(() => state.set('is_finish', false), 2500);
return state.withMutations(state =>
state.set('is_loading', false)
.set('error', fromJS({action.message, action.code})
.set('is_finish', true)
);
default:
return state;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment