Skip to content

Instantly share code, notes, and snippets.

@datvtwkm
Last active August 31, 2018 12:19
Show Gist options
  • Save datvtwkm/c8d26729655501069d0e79ecd585c003 to your computer and use it in GitHub Desktop.
Save datvtwkm/c8d26729655501069d0e79ecd585c003 to your computer and use it in GitHub Desktop.
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