client/src/reducers/sessionsReducer.js
changeset 29 4cfeabef7d5e
parent 12 48ddaa42b810
child 58 f16a080e0bc4
equal deleted inserted replaced
28:abf9f3ff2635 29:4cfeabef7d5e
     1 import Immutable from 'immutable';
     1 import Immutable from 'immutable';
     2 import * as types from '../constants/actionTypes';
     2 import * as types from '../constants/actionTypes';
     3 
     3 
     4 export const currentSession = (state = null, action) => {
     4 export const currentSession = (state = null, action) => {
     5   switch (action.type) {
     5   switch (action.type) {
     6     case types.NEW_SESSION:
     6     case types.CREATE_SESSION:
     7       return action.session;
     7       return action.session;
     8     default:
     8     default:
     9       return state;
     9       return state;
    10   }
    10   }
    11 };
    11 };
    12 
    12 
    13 export const sessions = (state = Immutable.List([]), action) => {
    13 export const sessions = (state = Immutable.List([]), action) => {
    14   switch (action.type) {
    14   switch (action.type) {
    15     case types.NEW_SESSION:
    15     case types.CREATE_SESSION:
    16       return state.push(action.session);
    16       return state.push(action.session);
    17     case types.UPDATE_SESSION:
    17     case types.UPDATE_SESSION:
    18       const sessionToUpdate = state.find(session => session === action.session);
    18       const sessionToUpdate = state.find(session => session === action.session);
    19       const sessionIndex = state.indexOf(action.session);
    19       const sessionIndex = state.indexOf(action.session);
    20       if (sessionIndex === -1) {
    20       if (sessionIndex === -1) {
    21         return state;
    21         return state;
    22       }
    22       }
    23       const updatedSession = Object.assign({}, sessionToUpdate, action.values);
    23       const updatedSession = Object.assign({}, sessionToUpdate, action.values);
    24       return state.set(sessionIndex, updatedSession);
    24       return state.set(sessionIndex, updatedSession);
       
    25     case types.LOAD_SESSIONS:
       
    26       return action.sessions;
    25     default:
    27     default:
    26       return state;
    28       return state;
    27   }
    29   }
    28 };
    30 };