client/src/reducers/notesReducer.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';
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
     2
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
import * as types from '../constants/actionTypes';
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
     4
import NoteRecord from '../store/noteRecord';
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
     5
import { ActionEnum } from '../constants';
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@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
import { getId, idEq, setAction, getNewAction } from './utils';
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
     8
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
     9
const sessionEq = R.propEq('session');
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    10
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    11
export default (state = [], action) => {
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
  switch (action.type) {
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    13
    case types.ADD_NOTE: {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    14
      return R.append(NoteRecord(action.note), state);
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    15
    }
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    16
    case types.DELETE_NOTE: {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    17
      return R.map(R.when(idEq(getId(action.note)), setAction(ActionEnum.DELETED) ), state);
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    18
    }
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    19
    case types.DO_DELETE_NOTE: {
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.noteId), state)
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    21
    }
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    22
    case types.UPDATE_NOTE: {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    23
      return R.map(R.when(idEq(getId(action.note)), note => {
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    24
        return R.mergeAll([note, action.data, {action: getNewAction(R.prop('action', note))}])
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    25
      }), state);
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    26
    }
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    27
    case types.DELETE_SESSION: {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    28
      const sessionId = getId(action.session);
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    29
      return R.map(R.when(sessionEq(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: 109
diff changeset
    30
    }
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    31
    case types.DO_DELETE_SESSION: {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    32
      return R.reject(sessionEq(action.sessionId), state)
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    33
    }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    34
    case types.RESET_ACTION_NOTE: {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    35
      return R.map(R.when(idEq(getId(action.note)), setAction(ActionEnum.NONE)), state);
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    36
    }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    37
    case types.SYNC_RESET_ALL: {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    38
      return R.map(setAction(ActionEnum.NONE), state);
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    39
    }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    40
    case types.LOAD_NOTE: {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    41
      const noteRec = action.note;
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    42
      const noteId = getId(noteRec);
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    43
      const index = R.findIndex(idEq(noteId), state);
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    44
      if(index >= 0) {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    45
        return  R.update(index, noteRec, state);
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    46
      } else {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    47
        return R.append(noteRec, state);
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    48
      }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    49
    }
132
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents: 129
diff changeset
    50
    case types.AUTH_LOGOUT: {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    51
      return []; // empty note list on logout
132
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents: 129
diff changeset
    52
    }
3
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
    default:
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
      return state;
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
  }
3b5d37d84cfe Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
};