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-08 04:42:24 -03:00
|
|
|
|
2016-06-16 22:49:48 -03:00
|
|
|
const initialState = Map({ entry: Map(), mediaFiles: List() });
|
2016-06-08 04:42:24 -03:00
|
|
|
|
2016-06-10 00:16:01 -03:00
|
|
|
const entryDraft = (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());
|
|
|
|
});
|
|
|
|
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());
|
|
|
|
});
|
|
|
|
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));
|
2016-06-08 04:42:24 -03:00
|
|
|
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2016-06-10 00:16:01 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
export default entryDraft;
|