Created
February 27, 2017 02:35
-
-
Save hanipcode/96a914bb91b4112a35ce04637b9a43aa to your computer and use it in GitHub Desktop.
simple reducer with error using immutable data
This file contains hidden or 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
| 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