2016-08-24 21:36:44 -03:00
|
|
|
import { Map, List, fromJS } from 'immutable';
|
2016-10-13 14:30:11 +02:00
|
|
|
import {
|
|
|
|
DRAFT_CREATE_FROM_ENTRY,
|
|
|
|
DRAFT_CREATE_EMPTY,
|
|
|
|
DRAFT_DISCARD,
|
2016-12-29 17:18:24 -02:00
|
|
|
DRAFT_CHANGE_FIELD,
|
2017-01-13 19:30:40 -02:00
|
|
|
DRAFT_VALIDATION_ERRORS,
|
2016-10-13 14:30:11 +02:00
|
|
|
ENTRY_PERSIST_REQUEST,
|
|
|
|
ENTRY_PERSIST_SUCCESS,
|
|
|
|
ENTRY_PERSIST_FAILURE,
|
|
|
|
} from '../actions/entries';
|
2017-01-13 17:52:27 -02:00
|
|
|
import {
|
|
|
|
UNPUBLISHED_ENTRY_PERSIST_REQUEST,
|
|
|
|
UNPUBLISHED_ENTRY_PERSIST_SUCCESS,
|
|
|
|
UNPUBLISHED_ENTRY_PERSIST_FAILURE,
|
|
|
|
} from '../actions/editorialWorkflow';
|
2016-10-13 14:30:11 +02:00
|
|
|
import {
|
2017-01-10 22:23:22 -02:00
|
|
|
ADD_ASSET,
|
|
|
|
REMOVE_ASSET,
|
2016-10-13 14:30:11 +02:00
|
|
|
} from '../actions/media';
|
2016-06-08 04:42:24 -03:00
|
|
|
|
2017-03-15 10:47:03 -07:00
|
|
|
const initialState = Map({
|
|
|
|
entry: Map(),
|
|
|
|
mediaFiles: List(),
|
|
|
|
fieldsMetaData: Map(),
|
|
|
|
fieldsErrors: Map(),
|
|
|
|
hasChanged: false,
|
|
|
|
});
|
2016-06-08 04:42:24 -03:00
|
|
|
|
2016-10-13 14:30:11 +02:00
|
|
|
const entryDraftReducer = (state = Map(), action) => {
|
2016-06-08 04:42:24 -03:00
|
|
|
switch (action.type) {
|
2016-07-19 17:11:22 -03:00
|
|
|
case DRAFT_CREATE_FROM_ENTRY:
|
2016-06-08 04:42:24 -03:00
|
|
|
// Existing Entry
|
|
|
|
return state.withMutations((state) => {
|
|
|
|
state.set('entry', action.payload);
|
2016-08-25 16:11:00 -03:00
|
|
|
state.setIn(['entry', 'newRecord'], false);
|
2016-08-24 21:36:44 -03:00
|
|
|
state.set('mediaFiles', List());
|
2016-12-29 17:18:24 -02:00
|
|
|
state.set('fieldsMetaData', Map());
|
2017-01-13 19:30:40 -02:00
|
|
|
state.set('fieldsErrors', Map());
|
2016-08-24 21:36:44 -03:00
|
|
|
});
|
|
|
|
case DRAFT_CREATE_EMPTY:
|
|
|
|
// New Entry
|
|
|
|
return state.withMutations((state) => {
|
|
|
|
state.set('entry', fromJS(action.payload));
|
2016-08-25 16:11:00 -03:00
|
|
|
state.setIn(['entry', 'newRecord'], true);
|
2016-06-08 04:42:24 -03:00
|
|
|
state.set('mediaFiles', List());
|
2016-12-29 17:18:24 -02:00
|
|
|
state.set('fieldsMetaData', Map());
|
2017-01-13 19:30:40 -02:00
|
|
|
state.set('fieldsErrors', Map());
|
2016-06-08 04:42:24 -03:00
|
|
|
});
|
|
|
|
case DRAFT_DISCARD:
|
|
|
|
return initialState;
|
2016-12-29 17:18:24 -02:00
|
|
|
case DRAFT_CHANGE_FIELD:
|
|
|
|
return state.withMutations((state) => {
|
|
|
|
state.setIn(['entry', 'data', action.payload.field], action.payload.value);
|
|
|
|
state.mergeIn(['fieldsMetaData'], fromJS(action.payload.metadata));
|
2017-03-15 10:47:03 -07:00
|
|
|
state.set('hasChanged', true);
|
2016-12-29 17:18:24 -02:00
|
|
|
});
|
2017-01-13 19:30:40 -02:00
|
|
|
|
|
|
|
case DRAFT_VALIDATION_ERRORS:
|
|
|
|
if (action.payload.errors.length === 0) {
|
|
|
|
return state.deleteIn(['fieldsErrors', action.payload.field]);
|
|
|
|
} else {
|
|
|
|
return state.setIn(['fieldsErrors', action.payload.field], action.payload.errors);
|
|
|
|
}
|
|
|
|
|
2017-01-13 17:52:27 -02:00
|
|
|
case ENTRY_PERSIST_REQUEST:
|
|
|
|
case UNPUBLISHED_ENTRY_PERSIST_REQUEST: {
|
2016-10-13 14:30:11 +02:00
|
|
|
return state.setIn(['entry', 'isPersisting'], true);
|
|
|
|
}
|
|
|
|
|
|
|
|
case ENTRY_PERSIST_SUCCESS:
|
2017-01-13 17:52:27 -02:00
|
|
|
case ENTRY_PERSIST_FAILURE:
|
|
|
|
case UNPUBLISHED_ENTRY_PERSIST_FAILURE: {
|
2016-10-13 14:30:11 +02:00
|
|
|
return state.deleteIn(['entry', 'isPersisting']);
|
|
|
|
}
|
|
|
|
|
2017-03-15 23:20:54 -07:00
|
|
|
case UNPUBLISHED_ENTRY_PERSIST_SUCCESS:
|
|
|
|
return state.withMutations((state) => {
|
|
|
|
state.deleteIn(['entry', 'isPersisting']);
|
|
|
|
state.setIn(['entry', 'hasChanged'], false);
|
|
|
|
});
|
|
|
|
|
2017-01-10 22:23:22 -02:00
|
|
|
case ADD_ASSET:
|
2016-10-13 14:30:11 +02:00
|
|
|
return state.update('mediaFiles', list => list.push(action.payload.public_path));
|
2017-01-10 22:23:22 -02:00
|
|
|
case REMOVE_ASSET:
|
2016-10-13 14:30:11 +02:00
|
|
|
return state.update('mediaFiles', list => list.filterNot(path => path === action.payload));
|
2016-06-08 04:42:24 -03:00
|
|
|
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2016-06-10 00:16:01 -03:00
|
|
|
};
|
|
|
|
|
2016-10-13 14:30:11 +02:00
|
|
|
export default entryDraftReducer;
|