2019-11-08 16:29:51 +01:00
|
|
|
import { fromJS } from 'immutable';
|
2019-10-30 13:56:20 +01:00
|
|
|
|
|
|
|
|
const initialState = fromJS({
|
2019-11-07 14:06:50 +01:00
|
|
|
componentsDataStructure: {},
|
|
|
|
|
contentTypeDataStructure: {},
|
2019-10-30 18:46:19 +01:00
|
|
|
formErrors: {},
|
2019-10-30 13:56:20 +01:00
|
|
|
isLoading: true,
|
|
|
|
|
initialData: {},
|
|
|
|
|
modifiedData: {},
|
2019-10-30 18:46:19 +01:00
|
|
|
shouldShowLoadingState: false,
|
2019-11-08 16:17:20 +01:00
|
|
|
shouldCheckErrors: false,
|
2019-12-11 16:52:35 +01:00
|
|
|
modifiedDZName: null,
|
2019-10-30 13:56:20 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const reducer = (state, action) => {
|
|
|
|
|
switch (action.type) {
|
2019-11-04 14:23:46 +01:00
|
|
|
case 'ADD_NON_REPEATABLE_COMPONENT_TO_FIELD':
|
|
|
|
|
return state.updateIn(['modifiedData', ...action.keys], () => {
|
2019-11-07 14:06:50 +01:00
|
|
|
const defaultDataStructure = state.getIn([
|
|
|
|
|
'componentsDataStructure',
|
|
|
|
|
action.componentUid,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return fromJS(defaultDataStructure);
|
2019-11-04 14:23:46 +01:00
|
|
|
});
|
2019-11-05 11:51:04 +01:00
|
|
|
case 'ADD_REPEATABLE_COMPONENT_TO_FIELD': {
|
2019-11-08 16:17:20 +01:00
|
|
|
return state
|
|
|
|
|
.updateIn(['modifiedData', ...action.keys], list => {
|
|
|
|
|
const defaultDataStructure = state.getIn([
|
|
|
|
|
'componentsDataStructure',
|
|
|
|
|
action.componentUid,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (list) {
|
|
|
|
|
return list.push(defaultDataStructure);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fromJS([defaultDataStructure]);
|
|
|
|
|
})
|
|
|
|
|
.update('shouldCheckErrors', v => {
|
|
|
|
|
if (action.shouldCheckErrors === true) {
|
|
|
|
|
return !v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return v;
|
|
|
|
|
});
|
2019-11-05 11:51:04 +01:00
|
|
|
}
|
2019-11-04 14:23:46 +01:00
|
|
|
case 'ADD_COMPONENT_TO_DYNAMIC_ZONE':
|
2019-11-08 16:17:20 +01:00
|
|
|
return state
|
|
|
|
|
.updateIn(['modifiedData', ...action.keys], list => {
|
|
|
|
|
const defaultDataStructure = state
|
|
|
|
|
.getIn(['componentsDataStructure', action.componentUid])
|
|
|
|
|
.set('__component', action.componentUid);
|
|
|
|
|
|
|
|
|
|
if (list) {
|
|
|
|
|
return list.push(defaultDataStructure);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fromJS([defaultDataStructure]);
|
|
|
|
|
})
|
2019-12-11 16:52:35 +01:00
|
|
|
.update('modifiedDZName', () => action.keys[0])
|
2019-11-08 16:17:20 +01:00
|
|
|
.update('shouldCheckErrors', v => {
|
|
|
|
|
if (action.shouldCheckErrors === true) {
|
|
|
|
|
return !v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return v;
|
|
|
|
|
});
|
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-11-05 17:50:22 +01:00
|
|
|
case 'MOVE_COMPONENT_FIELD':
|
|
|
|
|
return state.updateIn(
|
|
|
|
|
['modifiedData', ...action.pathToComponent],
|
|
|
|
|
list => {
|
|
|
|
|
return list
|
|
|
|
|
.delete(action.dragIndex)
|
|
|
|
|
.insert(
|
|
|
|
|
action.hoverIndex,
|
|
|
|
|
state.getIn([
|
|
|
|
|
'modifiedData',
|
|
|
|
|
...action.pathToComponent,
|
|
|
|
|
action.dragIndex,
|
|
|
|
|
])
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
);
|
2019-11-06 16:29:19 +01:00
|
|
|
case 'MOVE_COMPONENT_UP':
|
2019-12-11 17:13:23 +01:00
|
|
|
return state
|
|
|
|
|
.update('shouldCheckErrors', v => {
|
|
|
|
|
if (action.shouldCheckErrors) {
|
|
|
|
|
return !v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return v;
|
|
|
|
|
})
|
|
|
|
|
.updateIn(['modifiedData', action.dynamicZoneName], list => {
|
|
|
|
|
return list
|
|
|
|
|
.delete(action.currentIndex)
|
|
|
|
|
.insert(
|
|
|
|
|
action.currentIndex - 1,
|
|
|
|
|
state.getIn([
|
|
|
|
|
'modifiedData',
|
|
|
|
|
action.dynamicZoneName,
|
|
|
|
|
action.currentIndex,
|
|
|
|
|
])
|
|
|
|
|
);
|
|
|
|
|
});
|
2019-11-06 16:29:19 +01:00
|
|
|
case 'MOVE_COMPONENT_DOWN':
|
2019-12-11 17:13:23 +01:00
|
|
|
return state
|
|
|
|
|
.update('shouldCheckErrors', v => {
|
|
|
|
|
if (action.shouldCheckErrors) {
|
|
|
|
|
return !v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return v;
|
|
|
|
|
})
|
|
|
|
|
.updateIn(['modifiedData', action.dynamicZoneName], list => {
|
|
|
|
|
return list
|
|
|
|
|
.delete(action.currentIndex)
|
|
|
|
|
.insert(
|
|
|
|
|
action.currentIndex + 1,
|
|
|
|
|
state.getIn([
|
|
|
|
|
'modifiedData',
|
|
|
|
|
action.dynamicZoneName,
|
|
|
|
|
action.currentIndex,
|
|
|
|
|
])
|
|
|
|
|
);
|
|
|
|
|
});
|
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;
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-11-06 16:29:19 +01:00
|
|
|
case 'REMOVE_COMPONENT_FROM_DYNAMIC_ZONE':
|
2019-12-11 16:52:35 +01:00
|
|
|
return state
|
|
|
|
|
.update('shouldCheckErrors', v => {
|
|
|
|
|
if (action.shouldCheckErrors) {
|
|
|
|
|
return !v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return v;
|
|
|
|
|
})
|
|
|
|
|
.deleteIn(['modifiedData', action.dynamicZoneName, action.index]);
|
2019-11-04 14:23:46 +01:00
|
|
|
case 'REMOVE_COMPONENT_FROM_FIELD': {
|
|
|
|
|
const componentPathToRemove = ['modifiedData', ...action.keys];
|
|
|
|
|
|
|
|
|
|
return state.updateIn(componentPathToRemove, () => null);
|
|
|
|
|
}
|
2019-11-05 15:36:22 +01:00
|
|
|
case 'REMOVE_REPEATABLE_FIELD': {
|
|
|
|
|
const componentPathToRemove = ['modifiedData', ...action.keys];
|
|
|
|
|
|
2020-01-20 15:15:35 +01:00
|
|
|
return state
|
|
|
|
|
.update('shouldCheckErrors', v => {
|
|
|
|
|
const hasErrors = state.get('formErrors').keySeq().size > 0;
|
|
|
|
|
|
|
|
|
|
if (hasErrors) {
|
|
|
|
|
return !v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return v;
|
|
|
|
|
})
|
|
|
|
|
.deleteIn(componentPathToRemove);
|
2019-11-05 15:36:22 +01:00
|
|
|
}
|
2019-11-04 14:23:46 +01:00
|
|
|
|
2019-10-30 15:06:38 +01:00
|
|
|
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;
|
2019-11-07 14:06:50 +01:00
|
|
|
case 'SET_DEFAULT_DATA_STRUCTURES':
|
|
|
|
|
return state
|
|
|
|
|
.update('componentsDataStructure', () =>
|
|
|
|
|
fromJS(action.componentsDataStructure)
|
|
|
|
|
)
|
|
|
|
|
.update('contentTypeDataStructure', () =>
|
|
|
|
|
fromJS(action.contentTypeDataStructure)
|
|
|
|
|
);
|
|
|
|
|
case 'SET_DEFAULT_MODIFIED_DATA_STRUCTURE':
|
2019-11-08 16:17:20 +01:00
|
|
|
return state
|
|
|
|
|
.update('isLoading', () => false)
|
2019-12-16 15:06:51 +01:00
|
|
|
.update('initialData', () => fromJS(action.contentTypeDataStructure))
|
2019-11-08 16:17:20 +01:00
|
|
|
.update('modifiedData', () => fromJS(action.contentTypeDataStructure));
|
|
|
|
|
case 'SET_ERRORS':
|
2019-12-11 16:52:35 +01:00
|
|
|
return state
|
|
|
|
|
.update('modifiedDZName', () => null)
|
|
|
|
|
.update('formErrors', () => fromJS(action.errors));
|
2019-10-30 18:46:19 +01:00
|
|
|
case 'SUBMIT_ERRORS':
|
|
|
|
|
return state
|
|
|
|
|
.update('formErrors', () => fromJS(action.errors))
|
|
|
|
|
.update('shouldShowLoadingState', () => false);
|
2019-12-16 15:06:51 +01:00
|
|
|
case 'SUBMIT_SUCCESS':
|
|
|
|
|
case 'DELETE_SUCCEEDED':
|
|
|
|
|
return state.update('initialData', () => state.get('modifiedData'));
|
2020-01-20 15:15:35 +01:00
|
|
|
case 'TRIGGER_FORM_VALIDATION':
|
|
|
|
|
return state.update('shouldCheckErrors', v => {
|
|
|
|
|
const hasErrors = state.get('formErrors').keySeq().size > 0;
|
|
|
|
|
|
|
|
|
|
if (hasErrors) {
|
|
|
|
|
return !v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return v;
|
|
|
|
|
});
|
2019-10-30 13:56:20 +01:00
|
|
|
default:
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default reducer;
|
|
|
|
|
export { initialState };
|