client/src/reducers/notesReducer.js
changeset 124 c77570164050
parent 109 ef62de545a8d
child 129 d48946d164c6
--- a/client/src/reducers/notesReducer.js	Wed Jul 19 17:03:40 2017 +0200
+++ b/client/src/reducers/notesReducer.js	Thu Jul 20 11:23:08 2017 +0200
@@ -12,21 +12,34 @@
 
 export default (state = Immutable.List([]), action) => {
   switch (action.type) {
-    case types.ADD_NOTE:
+    case types.ADD_NOTE: {
       return state.push(new NoteRecord(action.note));
-    case types.DELETE_NOTE:
+    }
+    case types.DELETE_NOTE: {
+      const noteIndex = findNoteIndex(state, action.note.get('_id'));
+      const note = findNote(state, action.note.get('_id'));
+      return state.set(noteIndex, note.merge({deleted:true, modified:true}));
+    }
+    case types.DO_DELETE_NOTE: {
       const noteIndex = state.findIndex((note) => note.get('_id') === action.note.get('_id'));
       return state.delete(noteIndex);
-    case types.UPDATE_NOTE:
+    }
+    case types.UPDATE_NOTE: {
       const index = findNoteIndex(state, action.note.get('_id'));
       const note = findNote(state, action.note.get('_id'));
-      let newNote = note;
-      Object.entries(action.data).forEach(([key, value]) => {
-        newNote = note.set(key, value)
-      });
+      let newNote = note.merge(action.data, {modified:true});
       return state.set(index, newNote);
-    case types.DELETE_SESSION:
-      return state.filter((note) => action.session.get('_id') !== note.session)
+    }
+    case types.DELETE_SESSION: {
+      const sessionId = action.session.get('_id');
+      return state.map((note) => {
+        if(sessionId === note.session) {
+          return note.merge({deleted:true, modified:true});
+        } else {
+          return note;
+        }
+      })
+    }
     default:
       return state;
   }