Moved draft state for an entry (when in edit mode) to Redux

This commit is contained in:
Cássio Zen
2016-06-08 04:42:24 -03:00
parent 2d48743f37
commit 9275aaec90
11 changed files with 206 additions and 112 deletions

View File

@ -0,0 +1,34 @@
import { Map, List } from 'immutable';
import { DRAFT_CREATE, DRAFT_DISCARD, DRAFT_CHANGE, DRAFT_ADD_MEDIA, DRAFT_REMOVE_MEDIA } from '../actions/entries';
const initialState = Map({entry: Map(), mediaFiles: List()});
export function entryDraft(state = Map(), action) {
switch (action.type) {
case DRAFT_CREATE:
if (!action.payload) {
// New entry
return initialState;
}
// Existing Entry
return state.withMutations((state) => {
state.set('entry', action.payload);
state.set('mediaFiles', List());
});
case DRAFT_DISCARD:
return initialState;
case DRAFT_CHANGE:
return state.set('entry', action.payload);
case DRAFT_ADD_MEDIA:
return state.update('mediaFiles', (list) => list.push(action.payload));
case DRAFT_REMOVE_MEDIA:
const mediaIndex = state.get('mediaFiles').indexOf(action.payload);
if (mediaIndex === -1) return state;
return state.update('mediaFiles', (list) => list.splice(mediaIndex, 1));
default:
return state;
}
}