client/src/reducers/sessionsReducer.js
author ymh <ymh.work@gmail.com>
Thu, 20 Jul 2017 11:23:08 +0200
changeset 124 c77570164050
parent 93 469da13402e2
child 129 d48946d164c6
permissions -rw-r--r--
implement soft delete and indicator that session and notes have been modiied

import Immutable from 'immutable';
import * as types from '../constants/actionTypes';
import SessionRecord from '../store/sessionRecord';

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

  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);
      const sessionIndex = state.indexOf(action.session);
      if (sessionIndex === -1) {
        return state;
      }
      const updatedSession = sessionToUpdate.merge(action.values, {modified: true});
      return state.set(sessionIndex, updatedSession);
    }
    case types.DO_DELETE_SESSION: {
      return state.filter((note) => action.session.get('_id') !== note.session)
    }
    case types.DELETE_SESSION: {
      const sessionIndex = state.indexOf(action.session);
      if (sessionIndex === -1) {
        return state;
      }
      const deletedSession = state.get(sessionIndex);
      return state.set(sessionIndex, deletedSession.merge({deleted:true, modified:true}));
    }
    case types.LOAD_SESSIONS: {
      return action.sessions;
    }
    default:
      return state;
  }
};