client/src/reducers/sessionsReducer.js
author ymh <ymh.work@gmail.com>
Wed, 19 Jul 2017 15:57:13 +0200
changeset 119 8ff8e2aee0f9
parent 93 469da13402e2
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 SessionRecord from '../store/sessionRecord';

export const sessions = (state = Immutable.List([]), action) => {
  let sessionIndex;

  switch (action.type) {
    case types.CREATE_SESSION:
      return state.push(new SessionRecord(action.session));
    case types.UPDATE_SESSION:
      const sessionToUpdate = state.find(session => session === action.session);
      sessionIndex = state.indexOf(action.session);
      if (sessionIndex === -1) {
        return state;
      }
      const updatedSession = sessionToUpdate.merge(action.values);
      return state.set(sessionIndex, updatedSession);
    case types.DELETE_SESSION:
      sessionIndex = state.indexOf(action.session);
      if (sessionIndex === -1) {
        return state;
      }
      return state.delete(sessionIndex);
    case types.LOAD_SESSIONS:
      return action.sessions;
    default:
      return state;
  }
};