client/src/reducers/notesReducer.js
author ymh <ymh.work@gmail.com>
Wed, 19 Jul 2017 15:57:13 +0200
changeset 119 8ff8e2aee0f9
parent 109 ef62de545a8d
child 124 c77570164050
permissions -rw-r--r--
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes

import Immutable from 'immutable';
import * as types from '../constants/actionTypes';
import NoteRecord from '../store/noteRecord';

const findNoteIndex = (notes, id) => {
  return notes.findIndex((note) => note.get('_id') === id);
}

const findNote = (notes, id) => {
  return notes.get(findNoteIndex(notes, id));
}

export default (state = Immutable.List([]), action) => {
  switch (action.type) {
    case types.ADD_NOTE:
      return state.push(new NoteRecord(action.note));
    case types.DELETE_NOTE:
      const noteIndex = state.findIndex((note) => note.get('_id') === action.note.get('_id'));
      return state.delete(noteIndex);
    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)
      });
      return state.set(index, newNote);
    case types.DELETE_SESSION:
      return state.filter((note) => action.session.get('_id') !== note.session)
    default:
      return state;
  }
};