client/src/reducers/notesReducer.js
author ymh <ymh.work@gmail.com>
Thu, 20 Jul 2017 11:23:08 +0200
changeset 124 c77570164050
parent 109 ef62de545a8d
child 129 d48946d164c6
permissions -rw-r--r--
implement soft delete and indicator that session and notes have been modiied
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
import Immutable from 'immutable';
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
import * as types from '../constants/actionTypes';
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
     3
import NoteRecord from '../store/noteRecord';
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
     5
const findNoteIndex = (notes, id) => {
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
     6
  return notes.findIndex((note) => note.get('_id') === id);
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
     7
}
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
     8
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
     9
const findNote = (notes, id) => {
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    10
  return notes.get(findNoteIndex(notes, id));
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    11
}
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    12
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
export default (state = Immutable.List([]), action) => {
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
  switch (action.type) {
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    15
    case types.ADD_NOTE: {
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    16
      return state.push(new NoteRecord(action.note));
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    17
    }
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    18
    case types.DELETE_NOTE: {
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    19
      const noteIndex = findNoteIndex(state, action.note.get('_id'));
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    20
      const note = findNote(state, action.note.get('_id'));
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    21
      return state.set(noteIndex, note.merge({deleted:true, modified:true}));
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    22
    }
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    23
    case types.DO_DELETE_NOTE: {
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 66
diff changeset
    24
      const noteIndex = state.findIndex((note) => note.get('_id') === action.note.get('_id'));
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 66
diff changeset
    25
      return state.delete(noteIndex);
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    26
    }
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    27
    case types.UPDATE_NOTE: {
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    28
      const index = findNoteIndex(state, action.note.get('_id'));
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    29
      const note = findNote(state, action.note.get('_id'));
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    30
      let newNote = note.merge(action.data, {modified:true});
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    31
      return state.set(index, newNote);
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    32
    }
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    33
    case types.DELETE_SESSION: {
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    34
      const sessionId = action.session.get('_id');
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    35
      return state.map((note) => {
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    36
        if(sessionId === note.session) {
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    37
          return note.merge({deleted:true, modified:true});
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    38
        } else {
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    39
          return note;
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    40
        }
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    41
      })
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    42
    }
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
    default:
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
      return state;
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
  }
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
};