client/src/reducers/sessionsReducer.js
changeset 129 d48946d164c6
parent 124 c77570164050
child 132 906a6c7c7943
equal deleted inserted replaced
128:34a75bd8d0b9 129:d48946d164c6
     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 import SessionRecord from '../store/sessionRecord';
     3 import SessionRecord from '../store/sessionRecord';
       
     4 import { ActionEnum } from '../constants';
     4 
     5 
     5 export const sessions = (state = Immutable.List([]), action) => {
     6 export const sessions = (state = Immutable.List([]), action) => {
     6 
     7 
     7   switch (action.type) {
     8   switch (action.type) {
     8 
     9 
    13       const sessionToUpdate = state.find(session => session === action.session);
    14       const sessionToUpdate = state.find(session => session === action.session);
    14       const sessionIndex = state.indexOf(action.session);
    15       const sessionIndex = state.indexOf(action.session);
    15       if (sessionIndex === -1) {
    16       if (sessionIndex === -1) {
    16         return state;
    17         return state;
    17       }
    18       }
    18       const updatedSession = sessionToUpdate.merge(action.values, {modified: true});
    19       let newAction;
       
    20       switch (sessionToUpdate.get('action')) {
       
    21         case ActionEnum.CREATED:
       
    22           newAction = ActionEnum.CREATED;
       
    23           break;
       
    24         case ActionEnum.DELETED: // should not happen, but...
       
    25           newAction = ActionEnum.DELETED;
       
    26           break;
       
    27         default:
       
    28           newAction = ActionEnum.UPDATED;
       
    29       }
       
    30 
       
    31       const updatedSession = sessionToUpdate.merge(action.values, {action: newAction});
    19       return state.set(sessionIndex, updatedSession);
    32       return state.set(sessionIndex, updatedSession);
    20     }
    33     }
    21     case types.DO_DELETE_SESSION: {
    34     case types.DO_DELETE_SESSION: {
    22       return state.filter((note) => action.session.get('_id') !== note.session)
    35       return state.filter((session) => action.sessionId !== session._id)
    23     }
    36     }
    24     case types.DELETE_SESSION: {
    37     case types.DELETE_SESSION: {
    25       const sessionIndex = state.indexOf(action.session);
    38       const sessionIndex = state.indexOf(action.session);
    26       if (sessionIndex === -1) {
    39       if (sessionIndex === -1) {
    27         return state;
    40         return state;
    28       }
    41       }
    29       const deletedSession = state.get(sessionIndex);
    42       const deletedSession = state.get(sessionIndex);
    30       return state.set(sessionIndex, deletedSession.merge({deleted:true, modified:true}));
    43       if(deletedSession.get('action') === ActionEnum.CREATED) {
       
    44         // The session was previously created, we can delete it
       
    45         return state.delete(sessionIndex);
       
    46       } else {
       
    47         return state.set(sessionIndex, deletedSession.merge({action: ActionEnum.DELETED}));
       
    48       }
    31     }
    49     }
    32     case types.LOAD_SESSIONS: {
    50     case types.LOAD_SESSIONS: {
    33       return action.sessions;
    51       return action.sessions;
       
    52     }
       
    53     case types.LOAD_SESSION: {
       
    54       const sessionRec = action.session;
       
    55       const sessionId = sessionRec.get('_id');
       
    56       const index = state.findIndex((session) => session.get('_id') === sessionId);
       
    57       if(index >= 0) {
       
    58         return state.set(index, sessionRec);
       
    59       } else {
       
    60         return state.push(sessionRec);
       
    61       }
       
    62     }
       
    63     case types.SYNC_RESET_ALL: {
       
    64       return state.map((session) => {
       
    65         if(session.action !== ActionEnum.NONE) {
       
    66           return session.merge({action: ActionEnum.None});
       
    67         } else {
       
    68           return session;
       
    69         }
       
    70       });
       
    71     }
       
    72     case types.RESET_ACTION_SESSION: {
       
    73       const sessionId = action.session.get('_id');
       
    74       const index = state.findIndex((session) => session.get('_id') === sessionId);
       
    75       const session = state.get(index);
       
    76       return state.set(index, session.merge({action: ActionEnum.NONE}));
    34     }
    77     }
    35     default:
    78     default:
    36       return state;
    79       return state;
    37   }
    80   }
    38 };
    81 };