client/src/selectors/coreSelectors.js
changeset 168 ea92f4fe783d
parent 137 279e1dffa213
equal deleted inserted replaced
167:1f340f3597a8 168:ea92f4fe783d
     1 import Immutable from 'immutable';
     1 import { ActionEnum } from '../constants';
     2 import { ActionEnum } from '../constants'
     2 import * as R from 'ramda';
     3 
     3 
       
     4 const actionFilter = R.propEq('action');
     4 
     5 
     5 const getSessionMapSelector = actionVal => state =>
     6 // export const getSessions = state => state.get('sessions')
     6   state.get('sessions')
     7 export const getSessions = R.prop('sessions')
     7     .filter(s => s.get('action') === actionVal)
     8 
     8     .reduce(
     9 // export const getNotes = state => state.get('notes')
     9       (res, obj) => {
    10 export const getNotes = R.prop('notes');
    10         return res.set(obj.get('_id'), obj);
    11 
    11       },
    12 // const getSessionMapSelector = actionVal => state =>
    12       Immutable.Map()
    13 //   state.sessions
    13     );
    14 //     .filter(s => s.action === actionVal)
       
    15 //     .reduce(
       
    16 //       (res, obj) => {
       
    17 //         res[obj._id] = obj;
       
    18 //         return res;
       
    19 //       },
       
    20 //       {}
       
    21 //     );
       
    22 
       
    23 const groupById = R.reduceBy(R.nthArg(1), {}, R.prop('_id'));
       
    24 
       
    25 const getSessionMapSelector = actionVal => R.compose(
       
    26   groupById,
       
    27   R.filter(actionFilter(actionVal)),
       
    28   getSessions
       
    29   );
       
    30 
       
    31 // const getNoteMapSelector = actionVal => state => {
       
    32 //   const deletedSessions = state.sessions
       
    33 //     .filter(s => s.action === ActionEnum.DELETED)
       
    34 //     .reduce(
       
    35 //       (res, obj) => {
       
    36 //         res[obj._id] = obj;
       
    37 //         return res;
       
    38 //       },
       
    39 //       {}
       
    40 //     );
       
    41 //   return state.notes
       
    42 //     .filter(n => (n.action === actionVal && !(n.session in deletedSessions)))
       
    43 //     .reduce(
       
    44 //       (res, obj) => {
       
    45 //         res[obj._id] = obj;
       
    46 //         return res;
       
    47 //       },
       
    48 //       {}
       
    49 //     );
       
    50 // }
    14 
    51 
    15 const getNoteMapSelector = actionVal => state => {
    52 const getNoteMapSelector = actionVal => state => {
    16   const deletedSessions = state.get('sessions')
    53   const deletedSessions = getSessionMapSelector(ActionEnum.DELETED)(state);
    17     .filter(s => s.get('action') === ActionEnum.DELETED)
    54   return R.compose(
    18     .reduce(
    55    groupById,
    19       (res, obj) => {
    56    R.reject(R.propSatisfies(R.has(R.__, deletedSessions), 'session')),
    20         return res.set(obj.get('_id'), obj);
    57    R.filter(actionFilter(actionVal)),
    21       },
    58    getNotes
    22       Immutable.Map()
    59   )(state)
    23     );
       
    24   return state.get('notes')
       
    25     .filter(n => (n.get('action') === actionVal && !deletedSessions.has(n.get('session'))))
       
    26     .reduce(
       
    27       (res, obj) => {
       
    28         return res.set(obj.get('_id'), obj);
       
    29       },
       
    30       Immutable.Map()
       
    31     );
       
    32 }
    60 }
    33 
    61 
    34 
    62 
    35 export const getUpdatedSessions = getSessionMapSelector(ActionEnum.UPDATED);
    63 export const getUpdatedSessions = getSessionMapSelector(ActionEnum.UPDATED);
    36 
    64 
    42 
    70 
    43 export const getCreatedNotes = getNoteMapSelector(ActionEnum.CREATED);
    71 export const getCreatedNotes = getNoteMapSelector(ActionEnum.CREATED);
    44 
    72 
    45 export const getDeletedNotes = getNoteMapSelector(ActionEnum.DELETED);
    73 export const getDeletedNotes = getNoteMapSelector(ActionEnum.DELETED);
    46 
    74 
    47 export const getActiveSessions = state => state.get('sessions').filter(session => session.get('action') !== ActionEnum.DELETED)
    75 const testActionNotDeleted = R.complement(R.propEq('action', ActionEnum.DELETED));
    48 
    76 
    49 export const getSessions = state => state.get('sessions')
    77 // export const getActiveSessions = state => getSessions(state).filter(session => session.action !== ActionEnum.DELETED)
    50 export const getNotes = state => state.get('notes')
    78 export const getActiveSessions = R.compose(R.filter(testActionNotDeleted), getSessions);
    51 
    79 
    52 export const getSession = (sessionId, state) => {
    80 // export const getSession = (sessionId, state) => {
    53   const sessions = getSessions(state);
    81 //   const sessions = getSessions(state);
    54   return sessions.find(session => session._id === sessionId)
    82 //   return sessions.find(session => session._id === sessionId)
    55 }
    83 // }
       
    84 const findById = R.compose(R.find,R.propEq('_id'));
       
    85 export const getSession = R.uncurryN(2,R.partialRight(R.useWith(R.compose, [findById, R.always(getSessions)]), [null,]))
    56 
    86 
    57 export const getSessionNotes = (sessionId, state) => {
    87 // export const getSessionNotes = (sessionId, state) => {
    58   const notes = getNotes(state);
    88 //   const notes = getNotes(state);
    59   return notes.filter(note => {
    89 //   let filteredNotes = notes.filter(note => {
    60     return (note.get('session') === sessionId && note.get('action') !== ActionEnum.DELETED);
    90 //     return (note.session === sessionId && note.action !== ActionEnum.DELETED);
    61   }).sortBy( n => n.get('startedAt') );
    91 //   });
    62 }
    92 //   filteredNotes.sort((a,b) => a.startedAt - b.startedAt);
       
    93 //   return filteredNotes;
       
    94 // }
       
    95 const filterSessionNote = R.partial(R.useWith(R.both, [R.always(testActionNotDeleted), R.propEq('session')]),[null,]);
    63 
    96 
       
    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)]));
       
    98