client/src/reducers/sessionsReducer.js
changeset 168 ea92f4fe783d
parent 132 906a6c7c7943
equal deleted inserted replaced
167:1f340f3597a8 168:ea92f4fe783d
     1 import Immutable from 'immutable';
     1 import * as R from 'ramda';
     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 import { ActionEnum } from '../constants';
       
     5 import { getId, idEq, getNewAction, setAction } from './utils';
     5 
     6 
     6 export const sessions = (state = Immutable.List([]), action) => {
     7 export const sessions = (state = [], action) => {
     7 
     8 
     8   switch (action.type) {
     9   switch (action.type) {
     9 
    10 
    10     case types.CREATE_SESSION: {
    11     case types.CREATE_SESSION: {
    11       return state.push(new SessionRecord(action.session));
    12       return R.append(SessionRecord(action.session), state);
    12     }
    13     }
    13     case types.UPDATE_SESSION: {
    14     case types.UPDATE_SESSION: {
    14       const sessionToUpdate = state.find(session => session === action.session);
    15       return R.map(R.when(idEq(action.sessionId), session => {
    15       const sessionIndex = state.indexOf(action.session);
    16         return R.mergeAll([session, action.values, {action: getNewAction(R.prop('action', session))}])
    16       if (sessionIndex === -1) {
    17       }), state);
    17         return state;
       
    18       }
       
    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});
       
    32       return state.set(sessionIndex, updatedSession);
       
    33     }
    18     }
    34     case types.DO_DELETE_SESSION: {
    19     case types.DO_DELETE_SESSION: {
    35       return state.filter((session) => action.sessionId !== session._id)
    20       return R.reject(idEq(action.sessionId) , state);
    36     }
    21     }
       
    22     // TODO: Change the session argument. This should not be the complete session object
    37     case types.DELETE_SESSION: {
    23     case types.DELETE_SESSION: {
    38       const sessionIndex = state.indexOf(action.session);
    24       return R.map(R.when(idEq(action.sessionId), setAction(ActionEnum.DELETED) ), state);
    39       if (sessionIndex === -1) {
       
    40         return state;
       
    41       }
       
    42       const deletedSession = state.get(sessionIndex);
       
    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       }
       
    49     }
    25     }
    50     case types.LOAD_SESSIONS: {
    26     case types.LOAD_SESSIONS: {
    51       return action.sessions;
    27       return action.sessions;
    52     }
    28     }
    53     case types.LOAD_SESSION: {
    29     case types.LOAD_SESSION: {
    54       const sessionRec = action.session;
    30       const sessionRec = action.session;
    55       const sessionId = sessionRec.get('_id');
    31       const sessionId = getId(sessionRec);
    56       const index = state.findIndex((session) => session.get('_id') === sessionId);
    32       const index = R.findIndex(idEq(sessionId), state);
    57       if(index >= 0) {
    33       if(index >= 0) {
    58         return state.set(index, sessionRec);
    34         return  R.update(index, sessionRec, state);
    59       } else {
    35       } else {
    60         return state.push(sessionRec);
    36         return R.append(sessionRec, state);
    61       }
    37       }
    62     }
    38     }
    63     case types.SYNC_RESET_ALL: {
    39     case types.SYNC_RESET_ALL: {
    64       return state.map((session) => {
    40       return R.map(setAction(ActionEnum.NONE), state);
    65         if(session.action !== ActionEnum.NONE) {
       
    66           return session.merge({action: ActionEnum.None});
       
    67         } else {
       
    68           return session;
       
    69         }
       
    70       });
       
    71     }
    41     }
    72     case types.RESET_ACTION_SESSION: {
    42     case types.RESET_ACTION_SESSION: {
    73       const sessionId = action.session.get('_id');
    43       return R.map(R.when(idEq(action.sessionId), setAction(ActionEnum.NONE)), state);
    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}));
       
    77     }
    44     }
    78     case types.AUTH_LOGOUT: {
    45     case types.AUTH_LOGOUT: {
    79       return Immutable.List(); // empty session list on logout
    46       return []; // empty session list on logout
    80     }
    47     }
    81     default:
    48     default:
    82       return state;
    49       return state;
    83   }
    50   }
    84 };
    51 };