client/src/reducers/sessionsReducer.js
author ymh <ymh.work@gmail.com>
Fri, 30 Nov 2018 10:53:15 +0100
changeset 183 f8f3af9e5c83
parent 168 ea92f4fe783d
permissions -rw-r--r--
Change the settings to avoid using Session authentication for rest framework as it raise exceptions in case client and backend are on the same domain On the filter, adapt to take into account new version of django_filters
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
     1
import * as R from 'ramda';
12
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';
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
     5
import { getId, idEq, getNewAction, setAction } from './utils';
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     6
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
     7
export const sessions = (state = [], action) => {
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     8
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     9
  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
    10
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    11
    case types.CREATE_SESSION: {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    12
      return R.append(SessionRecord(action.session), state);
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    13
    }
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    14
    case types.UPDATE_SESSION: {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    15
      return R.map(R.when(idEq(action.sessionId), session => {
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    16
        return R.mergeAll([session, action.values, {action: getNewAction(R.prop('action', session))}])
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    17
      }), state);
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    18
    }
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    19
    case types.DO_DELETE_SESSION: {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    20
      return R.reject(idEq(action.sessionId) , state);
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    21
    }
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    22
    // TODO: Change the session argument. This should not be the complete session object
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    23
    case types.DELETE_SESSION: {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    24
      return R.map(R.when(idEq(action.sessionId), setAction(ActionEnum.DELETED) ), state);
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    25
    }
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 93
diff changeset
    26
    case types.LOAD_SESSIONS: {
29
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    27
      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
    28
    }
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    29
    case types.LOAD_SESSION: {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    30
      const sessionRec = action.session;
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    31
      const sessionId = getId(sessionRec);
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    32
      const index = R.findIndex(idEq(sessionId), state);
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    33
      if(index >= 0) {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    34
        return  R.update(index, sessionRec, state);
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    35
      } else {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    36
        return R.append(sessionRec, state);
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    37
      }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    38
    }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    39
    case types.SYNC_RESET_ALL: {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    40
      return R.map(setAction(ActionEnum.NONE), state);
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    41
    }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    42
    case types.RESET_ACTION_SESSION: {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    43
      return R.map(R.when(idEq(action.sessionId), setAction(ActionEnum.NONE)), state);
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    44
    }
132
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents: 129
diff changeset
    45
    case types.AUTH_LOGOUT: {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    46
      return []; // empty session list on logout
132
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents: 129
diff changeset
    47
    }
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    48
    default:
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    49
      return state;
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    50
  }
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    51
};