2016-04-12 15:10:07 +02:00
|
|
|
import { combineReducers } from 'redux';
|
|
|
|
import items from './items';
|
2016-04-16 18:06:27 +02:00
|
|
|
import groups from './groups';
|
2016-06-27 15:46:12 +02:00
|
|
|
import choices from './choices';
|
2019-12-23 19:22:54 +01:00
|
|
|
import loading from './loading';
|
2018-05-28 17:22:22 +02:00
|
|
|
import { cloneObject } from '../lib/utils';
|
2016-03-31 15:51:41 +02:00
|
|
|
|
2019-12-23 19:22:54 +01:00
|
|
|
export const defaultState = {
|
|
|
|
groups: [],
|
|
|
|
items: [],
|
|
|
|
choices: [],
|
|
|
|
loading: false,
|
|
|
|
};
|
|
|
|
|
2016-06-08 15:45:29 +02:00
|
|
|
const appReducer = combineReducers({
|
2016-09-05 23:04:15 +02:00
|
|
|
items,
|
|
|
|
groups,
|
|
|
|
choices,
|
2019-12-23 19:22:54 +01:00
|
|
|
loading,
|
2016-06-08 15:45:29 +02:00
|
|
|
});
|
|
|
|
|
2019-12-23 19:22:54 +01:00
|
|
|
const rootReducer = (passedState, action): object => {
|
2016-09-05 23:04:15 +02:00
|
|
|
let state = passedState;
|
|
|
|
// If we are clearing all items, groups and options we reassign
|
|
|
|
// state and then pass that state to our proper reducer. This isn't
|
|
|
|
// mutating our actual state
|
|
|
|
// See: http://stackoverflow.com/a/35641992
|
|
|
|
if (action.type === 'CLEAR_ALL') {
|
2019-12-23 19:22:54 +01:00
|
|
|
state = defaultState;
|
2018-05-28 17:22:22 +02:00
|
|
|
} else if (action.type === 'RESET_TO') {
|
|
|
|
return cloneObject(action.state);
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
2016-06-08 15:45:29 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
return appReducer(state, action);
|
2016-06-08 15:45:29 +02:00
|
|
|
};
|
2016-04-04 15:43:22 +02:00
|
|
|
|
2016-09-24 12:07:48 +02:00
|
|
|
export default rootReducer;
|