client/src/reducers/notesReducer.js
changeset 124 c77570164050
parent 109 ef62de545a8d
child 129 d48946d164c6
equal deleted inserted replaced
123:6542f93cdc0d 124:c77570164050
    10   return notes.get(findNoteIndex(notes, id));
    10   return notes.get(findNoteIndex(notes, id));
    11 }
    11 }
    12 
    12 
    13 export default (state = Immutable.List([]), action) => {
    13 export default (state = Immutable.List([]), action) => {
    14   switch (action.type) {
    14   switch (action.type) {
    15     case types.ADD_NOTE:
    15     case types.ADD_NOTE: {
    16       return state.push(new NoteRecord(action.note));
    16       return state.push(new NoteRecord(action.note));
    17     case types.DELETE_NOTE:
    17     }
       
    18     case types.DELETE_NOTE: {
       
    19       const noteIndex = findNoteIndex(state, action.note.get('_id'));
       
    20       const note = findNote(state, action.note.get('_id'));
       
    21       return state.set(noteIndex, note.merge({deleted:true, modified:true}));
       
    22     }
       
    23     case types.DO_DELETE_NOTE: {
    18       const noteIndex = state.findIndex((note) => note.get('_id') === action.note.get('_id'));
    24       const noteIndex = state.findIndex((note) => note.get('_id') === action.note.get('_id'));
    19       return state.delete(noteIndex);
    25       return state.delete(noteIndex);
    20     case types.UPDATE_NOTE:
    26     }
       
    27     case types.UPDATE_NOTE: {
    21       const index = findNoteIndex(state, action.note.get('_id'));
    28       const index = findNoteIndex(state, action.note.get('_id'));
    22       const note = findNote(state, action.note.get('_id'));
    29       const note = findNote(state, action.note.get('_id'));
    23       let newNote = note;
    30       let newNote = note.merge(action.data, {modified:true});
    24       Object.entries(action.data).forEach(([key, value]) => {
       
    25         newNote = note.set(key, value)
       
    26       });
       
    27       return state.set(index, newNote);
    31       return state.set(index, newNote);
    28     case types.DELETE_SESSION:
    32     }
    29       return state.filter((note) => action.session.get('_id') !== note.session)
    33     case types.DELETE_SESSION: {
       
    34       const sessionId = action.session.get('_id');
       
    35       return state.map((note) => {
       
    36         if(sessionId === note.session) {
       
    37           return note.merge({deleted:true, modified:true});
       
    38         } else {
       
    39           return note;
       
    40         }
       
    41       })
       
    42     }
    30     default:
    43     default:
    31       return state;
    44       return state;
    32   }
    45   }
    33 };
    46 };