client/src/reducers/sessionsReducer.js
author ymh <ymh.work@gmail.com>
Tue, 20 Jun 2017 14:13:15 +0200
changeset 62 b2514a9bcd49
parent 59 1eb52770eefa
child 93 469da13402e2
permissions -rw-r--r--
migrate to redux-offline + various optimisation

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

// export const currentSession = (state = null, action) => {
//   switch (action.type) {
//     case types.CREATE_SESSION:
//       return action.session;
//     default:
//       return state;
//   }
// };

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);
      return state.set(sessionIndex, updatedSession);
    case types.LOAD_SESSIONS:
      return action.sessions;
    default:
      return state;
  }
};