client/src/selectors/coreSelectors.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: 137
diff changeset
     1
import { ActionEnum } from '../constants';
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
     2
import * as R from 'ramda';
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
     3
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
     4
const actionFilter = R.propEq('action');
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
     5
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
     6
// export const getSessions = state => state.get('sessions')
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
     7
export const getSessions = R.prop('sessions')
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
     8
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
     9
// export const getNotes = state => state.get('notes')
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    10
export const getNotes = R.prop('notes');
134
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    12
// const getSessionMapSelector = actionVal => state =>
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    13
//   state.sessions
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    14
//     .filter(s => s.action === actionVal)
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    15
//     .reduce(
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    16
//       (res, obj) => {
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    17
//         res[obj._id] = obj;
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    18
//         return res;
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    19
//       },
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    20
//       {}
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    21
//     );
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    22
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    23
const groupById = R.reduceBy(R.nthArg(1), {}, R.prop('_id'));
134
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    25
const getSessionMapSelector = actionVal => R.compose(
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    26
  groupById,
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    27
  R.filter(actionFilter(actionVal)),
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    28
  getSessions
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    29
  );
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    30
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    31
// const getNoteMapSelector = actionVal => state => {
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    32
//   const deletedSessions = state.sessions
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    33
//     .filter(s => s.action === ActionEnum.DELETED)
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    34
//     .reduce(
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    35
//       (res, obj) => {
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    36
//         res[obj._id] = obj;
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    37
//         return res;
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    38
//       },
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    39
//       {}
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    40
//     );
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    41
//   return state.notes
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    42
//     .filter(n => (n.action === actionVal && !(n.session in deletedSessions)))
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    43
//     .reduce(
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    44
//       (res, obj) => {
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    45
//         res[obj._id] = obj;
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    46
//         return res;
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    47
//       },
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    48
//       {}
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    49
//     );
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    50
// }
134
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
const getNoteMapSelector = actionVal => state => {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    53
  const deletedSessions = getSessionMapSelector(ActionEnum.DELETED)(state);
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    54
  return R.compose(
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    55
   groupById,
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    56
   R.reject(R.propSatisfies(R.has(R.__, deletedSessions), 'session')),
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    57
   R.filter(actionFilter(actionVal)),
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    58
   getNotes
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    59
  )(state)
134
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
}
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
export const getUpdatedSessions = getSessionMapSelector(ActionEnum.UPDATED);
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
export const getCreatedSessions = getSessionMapSelector(ActionEnum.CREATED);
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
export const getDeletedSessions = getSessionMapSelector(ActionEnum.DELETED);
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
export const getUpdatedNotes = getNoteMapSelector(ActionEnum.UPDATED);
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
export const getCreatedNotes = getNoteMapSelector(ActionEnum.CREATED);
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
export const getDeletedNotes = getNoteMapSelector(ActionEnum.DELETED);
137
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    74
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    75
const testActionNotDeleted = R.complement(R.propEq('action', ActionEnum.DELETED));
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    76
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    77
// export const getActiveSessions = state => getSessions(state).filter(session => session.action !== ActionEnum.DELETED)
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    78
export const getActiveSessions = R.compose(R.filter(testActionNotDeleted), getSessions);
137
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    79
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    80
// export const getSession = (sessionId, state) => {
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    81
//   const sessions = getSessions(state);
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    82
//   return sessions.find(session => session._id === sessionId)
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    83
// }
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    84
const findById = R.compose(R.find,R.propEq('_id'));
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    85
export const getSession = R.uncurryN(2,R.partialRight(R.useWith(R.compose, [findById, R.always(getSessions)]), [null,]))
137
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    86
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    87
// export const getSessionNotes = (sessionId, state) => {
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    88
//   const notes = getNotes(state);
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    89
//   let filteredNotes = notes.filter(note => {
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    90
//     return (note.session === sessionId && note.action !== ActionEnum.DELETED);
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    91
//   });
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    92
//   filteredNotes.sort((a,b) => a.startedAt - b.startedAt);
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    93
//   return filteredNotes;
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    94
// }
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    95
const filterSessionNote = R.partial(R.useWith(R.both, [R.always(testActionNotDeleted), R.propEq('session')]),[null,]);
137
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    96
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    97
export const getSessionNotes = R.uncurryN(2,R.converge(R.compose, [R.always(R.sortBy(R.prop('startedAt'))), R.useWith(R.filter, [filterSessionNote,]), R.always(getNotes)]));
137
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    98