81 lines
2.1 KiB
JavaScript
Raw Normal View History

import {
fromJS,
// List,
} from 'immutable';
const initialState = fromJS({
2019-10-30 18:46:19 +01:00
formErrors: {},
isLoading: true,
initialData: {},
modifiedData: {},
2019-10-30 18:46:19 +01:00
shouldShowLoadingState: false,
});
const reducer = (state, action) => {
switch (action.type) {
2019-10-30 15:06:38 +01:00
case 'ADD_RELATION':
return state.updateIn(['modifiedData', ...action.keys], list => {
if (!action.value) {
return list;
}
const el = action.value[0].value;
if (list) {
return list.push(fromJS(el));
}
return fromJS([el]);
});
2019-10-30 18:46:19 +01:00
case 'GET_DATA_SUCCEEDED':
return state
.update('initialData', () => fromJS(action.data))
.update('modifiedData', () => fromJS(action.data))
.update('isLoading', () => false);
2019-10-30 19:06:40 +01:00
case 'IS_SUBMITTING':
return state.update('shouldShowLoadingState', () => action.value);
2019-10-30 15:06:38 +01:00
case 'MOVE_FIELD':
return state.updateIn(['modifiedData', ...action.keys], list => {
return list
.delete(action.dragIndex)
.insert(action.overIndex, list.get(action.dragIndex));
});
case 'ON_CHANGE': {
let newState = state;
const [nonRepeatableComponentKey] = action.keys;
if (
action.keys.length === 2 &&
state.getIn(['modifiedData', nonRepeatableComponentKey]) === null
) {
newState = state.updateIn(
['modifiedData', nonRepeatableComponentKey],
() => fromJS({})
);
}
return newState.updateIn(['modifiedData', ...action.keys], () => {
return action.value;
});
}
case 'REMOVE_RELATION':
return state.removeIn(['modifiedData', ...action.keys.split('.')]);
2019-10-30 19:06:40 +01:00
case 'RESET_DATA':
return state
.update('modifiedData', () => state.get('initialData'))
.update('formErrors', () => fromJS({}));
2019-10-30 18:46:19 +01:00
case 'RESET_PROPS':
return initialState;
case 'SUBMIT_ERRORS':
return state
.update('formErrors', () => fromJS(action.errors))
.update('shouldShowLoadingState', () => false);
default:
return state;
}
};
export default reducer;
export { initialState };