client/src/reducers/sessionsReducer.js
author salimr <riwad.salim@yahoo.fr>
Mon, 08 Oct 2018 03:24:47 +0200
changeset 159 a4705c2b4544
parent 132 906a6c7c7943
child 168 ea92f4fe783d
permissions -rw-r--r--
Add send note options to the editor
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     1
import Immutable from 'immutable';
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     2
import * as types from '../constants/actionTypes';
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
     3
import SessionRecord from '../store/sessionRecord';
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
     4
import { ActionEnum } from '../constants';
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     5
93
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
     6
export const sessions = (state = Immutable.List([]), action) => {
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     7
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     8
  switch (action.type) {
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
     9
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    10
    case types.CREATE_SESSION: {
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    11
      return state.push(new SessionRecord(action.session));
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    12
    }
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    13
    case types.UPDATE_SESSION: {
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    14
      const sessionToUpdate = state.find(session => session === action.session);
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    15
      const sessionIndex = state.indexOf(action.session);
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    16
      if (sessionIndex === -1) {
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    17
        return state;
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    18
      }
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    19
      let newAction;
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    20
      switch (sessionToUpdate.get('action')) {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    21
        case ActionEnum.CREATED:
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    22
          newAction = ActionEnum.CREATED;
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    23
          break;
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    24
        case ActionEnum.DELETED: // should not happen, but...
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    25
          newAction = ActionEnum.DELETED;
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    26
          break;
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    27
        default:
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    28
          newAction = ActionEnum.UPDATED;
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    29
      }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    30
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    31
      const updatedSession = sessionToUpdate.merge(action.values, {action: newAction});
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    32
      return state.set(sessionIndex, updatedSession);
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    33
    }
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    34
    case types.DO_DELETE_SESSION: {
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    35
      return state.filter((session) => action.sessionId !== session._id)
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    36
    }
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    37
    case types.DELETE_SESSION: {
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    38
      const sessionIndex = state.indexOf(action.session);
93
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
    39
      if (sessionIndex === -1) {
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
    40
        return state;
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
    41
      }
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    42
      const deletedSession = state.get(sessionIndex);
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    43
      if(deletedSession.get('action') === ActionEnum.CREATED) {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    44
        // The session was previously created, we can delete it
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    45
        return state.delete(sessionIndex);
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    46
      } else {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    47
        return state.set(sessionIndex, deletedSession.merge({action: ActionEnum.DELETED}));
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    48
      }
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    49
    }
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    50
    case types.LOAD_SESSIONS: {
29
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    51
      return action.sessions;
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    52
    }
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    53
    case types.LOAD_SESSION: {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    54
      const sessionRec = action.session;
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    55
      const sessionId = sessionRec.get('_id');
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    56
      const index = state.findIndex((session) => session.get('_id') === sessionId);
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    57
      if(index >= 0) {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    58
        return state.set(index, sessionRec);
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    59
      } else {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    60
        return state.push(sessionRec);
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    61
      }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    62
    }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    63
    case types.SYNC_RESET_ALL: {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    64
      return state.map((session) => {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    65
        if(session.action !== ActionEnum.NONE) {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    66
          return session.merge({action: ActionEnum.None});
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    67
        } else {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    68
          return session;
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    69
        }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    70
      });
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    71
    }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    72
    case types.RESET_ACTION_SESSION: {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    73
      const sessionId = action.session.get('_id');
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    74
      const index = state.findIndex((session) => session.get('_id') === sessionId);
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    75
      const session = state.get(index);
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    76
      return state.set(index, session.merge({action: ActionEnum.NONE}));
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    77
    }
132
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents: 129
diff changeset
    78
    case types.AUTH_LOGOUT: {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents: 129
diff changeset
    79
      return Immutable.List(); // empty session list on logout
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents: 129
diff changeset
    80
    }
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    81
    default:
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    82
      return state;
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    83
  }
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    84
};