static-cms/src/reducers/entryDraft.js

39 lines
1.2 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 } from '../actions/entries';
2016-06-10 14:01:14 -03:00
import { ADD_MEDIA, REMOVE_MEDIA } from '../actions/media';
2016-06-16 22:49:48 -03:00
const initialState = Map({ entry: Map(), mediaFiles: List() });
2016-06-10 00:16:01 -03:00
const entryDraft = (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());
});
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());
});
case DRAFT_DISCARD:
return initialState;
case DRAFT_CHANGE:
return state.set('entry', action.payload);
2016-06-10 00:16:01 -03:00
case ADD_MEDIA:
2016-07-19 17:11:22 -03:00
return state.update('mediaFiles', (list) => list.push(action.payload.path));
2016-06-10 14:01:14 -03:00
case REMOVE_MEDIA:
2016-07-19 17:11:22 -03:00
return state.update('mediaFiles', (list) => list.filterNot((path) => path === action.payload));
default:
return state;
}
2016-06-10 00:16:01 -03:00
};
export default entryDraft;