From d78b94f4e83210edd2592afbd4a186bfaffb9cdf Mon Sep 17 00:00:00 2001 From: bimlas Date: Fri, 25 May 2018 12:19:50 +0200 Subject: [PATCH] Refactoring update of tag changes --- browser/main/store.js | 68 +++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 42 deletions(-) diff --git a/browser/main/store.js b/browser/main/store.js index 27796d30..965d848c 100644 --- a/browser/main/store.js +++ b/browser/main/store.js @@ -142,27 +142,7 @@ function data (state = defaultDataMap(), action) { } if (oldNote != null) { - const discardedTags = _.difference(oldNote.tags, note.tags) - const addedTags = _.difference(note.tags, oldNote.tags) - if (discardedTags.length + addedTags.length > 0) { - state.tagNoteMap = new Map(state.tagNoteMap) - - discardedTags.forEach((tag) => { - let tagNoteList = state.tagNoteMap.get(tag) - if (tagNoteList != null) { - tagNoteList = new Set(tagNoteList) - tagNoteList.delete(uniqueKey) - state.tagNoteMap.set(tag, tagNoteList) - } - }) - addedTags.forEach((tag) => { - let tagNoteList = state.tagNoteMap.get(tag) - tagNoteList = new Set(tagNoteList) - tagNoteList.add(uniqueKey) - - state.tagNoteMap.set(tag, tagNoteList) - }) - } + updateTagChanges(oldNote, note, state, uniqueKey) } else { state.tagNoteMap = new Map(state.tagNoteMap) note.tags.forEach((tag) => { @@ -278,27 +258,7 @@ function data (state = defaultDataMap(), action) { // Remove from old folder map if (oldNote != null) { - const discardedTags = _.difference(oldNote.tags, note.tags) - const addedTags = _.difference(note.tags, oldNote.tags) - if (discardedTags.length + addedTags.length > 0) { - state.tagNoteMap = new Map(state.tagNoteMap) - - discardedTags.forEach((tag) => { - let tagNoteList = state.tagNoteMap.get(tag) - if (tagNoteList != null) { - tagNoteList = new Set(tagNoteList) - tagNoteList.delete(uniqueKey) - state.tagNoteMap.set(tag, tagNoteList) - } - }) - addedTags.forEach((tag) => { - let tagNoteList = state.tagNoteMap.get(tag) - tagNoteList = new Set(tagNoteList) - tagNoteList.add(uniqueKey) - - state.tagNoteMap.set(tag, tagNoteList) - }) - } + updateTagChanges(oldNote, note, state, uniqueKey) } else { state.tagNoteMap = new Map(state.tagNoteMap) note.tags.forEach((tag) => { @@ -559,6 +519,30 @@ function status (state = defaultStatus, action) { return state } +function updateTagChanges (oldNote, note, state, uniqueKey) { + const discardedTags = _.difference(oldNote.tags, note.tags) + const addedTags = _.difference(note.tags, oldNote.tags) + if (discardedTags.length + addedTags.length > 0) { + state.tagNoteMap = new Map(state.tagNoteMap) + + discardedTags.forEach((tag) => { + let tagNoteList = state.tagNoteMap.get(tag) + if (tagNoteList != null) { + tagNoteList = new Set(tagNoteList) + tagNoteList.delete(uniqueKey) + state.tagNoteMap.set(tag, tagNoteList) + } + }) + addedTags.forEach((tag) => { + let tagNoteList = state.tagNoteMap.get(tag) + tagNoteList = new Set(tagNoteList) + tagNoteList.add(uniqueKey) + + state.tagNoteMap.set(tag, tagNoteList) + }) + } +} + const reducer = combineReducers({ data, config,