static-cms/src/reducers/entryDraft.js

63 lines
1.9 KiB
JavaScript
Raw Normal View History

2016-08-24 21:36:44 -03:00
import { Map, List, fromJS } from 'immutable';
import {
DRAFT_CREATE_FROM_ENTRY,
DRAFT_CREATE_EMPTY,
DRAFT_DISCARD,
DRAFT_CHANGE_FIELD,
ENTRY_PERSIST_REQUEST,
ENTRY_PERSIST_SUCCESS,
ENTRY_PERSIST_FAILURE,
} from '../actions/entries';
import {
ADD_MEDIA,
REMOVE_MEDIA,
} from '../actions/media';
const initialState = Map({ entry: Map(), mediaFiles: List(), fieldsMetaData: Map() });
const entryDraftReducer = (state = Map(), action) => {
switch (action.type) {
2016-07-19 17:11:22 -03:00
case DRAFT_CREATE_FROM_ENTRY:
// Existing Entry
return state.withMutations((state) => {
state.set('entry', action.payload);
state.setIn(['entry', 'newRecord'], false);
2016-08-24 21:36:44 -03:00
state.set('mediaFiles', List());
state.set('fieldsMetaData', 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));
state.setIn(['entry', 'newRecord'], true);
state.set('mediaFiles', List());
state.set('fieldsMetaData', Map());
});
case DRAFT_DISCARD:
return initialState;
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));
});
case ENTRY_PERSIST_REQUEST: {
return state.setIn(['entry', 'isPersisting'], true);
}
case ENTRY_PERSIST_SUCCESS:
case ENTRY_PERSIST_FAILURE: {
return state.deleteIn(['entry', 'isPersisting']);
}
2016-06-10 00:16:01 -03:00
case ADD_MEDIA:
return state.update('mediaFiles', list => list.push(action.payload.public_path));
2016-06-10 14:01:14 -03:00
case REMOVE_MEDIA:
return state.update('mediaFiles', list => list.filterNot(path => path === action.payload));
default:
return state;
}
2016-06-10 00:16:01 -03:00
};
export default entryDraftReducer;