Commit 4e09c599 authored by Kyle Keating's avatar Kyle Keating

Namespace the drafts module

parent 1e62dac0
...@@ -9,9 +9,7 @@ const util = require('../util'); ...@@ -9,9 +9,7 @@ const util = require('../util');
*/ */
function init() { function init() {
return { return [];
drafts: [],
};
} }
/** /**
...@@ -58,27 +56,21 @@ class Draft { ...@@ -58,27 +56,21 @@ class Draft {
const update = { const update = {
DISCARD_ALL_DRAFTS: function() { DISCARD_ALL_DRAFTS: function() {
return { return [];
drafts: [],
};
}, },
REMOVE_DRAFT: function(state, action) { REMOVE_DRAFT: function(state, action) {
const drafts = state.drafts.filter(draft => { const drafts = state.filter(draft => {
return !draft.match(action.annotation); return !draft.match(action.annotation);
}); });
return { return drafts;
drafts,
};
}, },
UPDATE_DRAFT: function(state, action) { UPDATE_DRAFT: function(state, action) {
// removes a matching existing draft, then adds // removes a matching existing draft, then adds
const drafts = state.drafts.filter(draft => { const drafts = state.filter(draft => {
return !draft.match(action.draft.annotation); return !draft.match(action.draft.annotation);
}); });
drafts.push(action.draft); // push ok since its a copy drafts.push(action.draft); // push ok since its a copy
return { return drafts;
drafts,
};
}, },
}; };
...@@ -106,10 +98,10 @@ function createDraft(annotation, changes) { ...@@ -106,10 +98,10 @@ function createDraft(annotation, changes) {
function deleteNewAndEmptyDrafts() { function deleteNewAndEmptyDrafts() {
const annotations = require('./annotations'); const annotations = require('./annotations');
return (dispatch, getState) => { return (dispatch, getState) => {
const newDrafts = getState().base.drafts.filter(draft => { const newDrafts = getState().drafts.filter(draft => {
return ( return (
metadata.isNew(draft.annotation) && metadata.isNew(draft.annotation) &&
!getDraftIfNotEmpty(getState().base, draft.annotation) !getDraftIfNotEmpty(getState(), draft.annotation)
); );
}); });
const removedAnnotations = newDrafts.map(draft => { const removedAnnotations = newDrafts.map(draft => {
...@@ -157,8 +149,9 @@ function countDrafts(state) { ...@@ -157,8 +149,9 @@ function countDrafts(state) {
* @return {Draft|null} * @return {Draft|null}
*/ */
function getDraft(state, annotation) { function getDraft(state, annotation) {
for (let i = 0; i < state.drafts.length; i++) { const drafts = state.drafts;
const draft = state.drafts[i]; for (let i = 0; i < drafts.length; i++) {
const draft = drafts[i];
if (draft.match(annotation)) { if (draft.match(annotation)) {
return draft; return draft;
} }
...@@ -194,6 +187,7 @@ function unsavedAnnotations(state) { ...@@ -194,6 +187,7 @@ function unsavedAnnotations(state) {
module.exports = { module.exports = {
init, init,
namespace: 'drafts',
update, update,
actions: { actions: {
createDraft, createDraft,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment