client/src/reducers/notesReducer.js
author Alexandre Segura <mex.zktk@gmail.com>
Thu, 29 Jun 2017 17:26:35 +0200
changeset 109 ef62de545a8d
parent 96 b58463d7dc8e
child 124 c77570164050
permissions -rw-r--r--
Allow editing margin comment.
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) {
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
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));
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 66
diff changeset
    17
    case types.DELETE_NOTE:
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 66
diff changeset
    18
      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
    19
      return state.delete(noteIndex);
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    20
    case types.UPDATE_NOTE:
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    21
      const index = findNoteIndex(state, action.note.get('_id'));
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    22
      const note = findNote(state, action.note.get('_id'));
109
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 96
diff changeset
    23
      let newNote = note;
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 96
diff changeset
    24
      Object.entries(action.data).forEach(([key, value]) => {
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 96
diff changeset
    25
        newNote = note.set(key, value)
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 96
diff changeset
    26
      });
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    27
      return state.set(index, newNote);
96
b58463d7dc8e Also delete notes when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    28
    case types.DELETE_SESSION:
b58463d7dc8e Also delete notes when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    29
      return state.filter((note) => action.session.get('_id') !== note.session)
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
    default:
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
      return state;
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
  }
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
};