client/src/reducers/sessionsReducer.js
author ymh <ymh.work@gmail.com>
Thu, 20 Jul 2017 11:23:08 +0200
changeset 124 c77570164050
parent 93 469da13402e2
child 129 d48946d164c6
permissions -rw-r--r--
implement soft delete and indicator that session and notes have been modiied
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';
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     4
93
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
     5
export const sessions = (state = Immutable.List([]), action) => {
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     6
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     7
  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
     8
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
     9
    case types.CREATE_SESSION: {
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    10
      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
    11
    }
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    12
    case types.UPDATE_SESSION: {
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    13
      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
    14
      const sessionIndex = state.indexOf(action.session);
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    15
      if (sessionIndex === -1) {
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    16
        return state;
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    17
      }
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    18
      const updatedSession = sessionToUpdate.merge(action.values, {modified: true});
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    19
      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
    20
    }
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    21
    case types.DO_DELETE_SESSION: {
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    22
      return state.filter((note) => action.session.get('_id') !== note.session)
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    23
    }
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    24
    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
    25
      const sessionIndex = state.indexOf(action.session);
93
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
    26
      if (sessionIndex === -1) {
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
    27
        return state;
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
    28
      }
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    29
      const deletedSession = state.get(sessionIndex);
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    30
      return state.set(sessionIndex, deletedSession.merge({deleted:true, modified:true}));
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    31
    }
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    32
    case types.LOAD_SESSIONS: {
29
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    33
      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
    34
    }
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    35
    default:
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    36
      return state;
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    37
  }
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    38
};