Last active
August 31, 2018 12:19
-
-
Save datvtwkm/c8d26729655501069d0e79ecd585c003 to your computer and use it in GitHub Desktop.
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 { handleActions } from 'redux-actions'; | |
import { combineReducers } from 'redux'; | |
| | |
import actionGenerators from './actions-generators'; | |
const initialCRUD = fromJS({ | |
data: null, operation: 'reset', error: false | |
}); | |
const reducers = {}; | |
const generateReducerFromAction = action => handleActions({ | |
[action.start]: (state, { payload }) => { | |
...state, operation: 'start', error: false | |
}), | |
[action.cancel]: state => ({ | |
...state, operation: 'cancel' | |
}), | |
[action.succeed]: (state, { payload }) => ({ | |
...state, operation: 'succeed', data: payload, meta | |
}), | |
[action.fail]: (state) => ({ | |
...state, operation: 'fail', error: true | |
}), | |
[action.reset]: state => initialCRUD | |
}, initialCRUD); | |
Object.keys(actionGenerators).forEach(group => { | |
Object.keys(actionGenerators[group]).forEach(action => { | |
reducers[action] = generateReducerFromAction(actionGenerators[group][action]); | |
}); | |
}); | |
export default combineReducers(reducers); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment