client/src/reducers/notesReducer.js
changeset 129 d48946d164c6
parent 124 c77570164050
child 132 906a6c7c7943
equal deleted inserted replaced
128:34a75bd8d0b9 129:d48946d164c6
     1 import Immutable from 'immutable';
     1 import Immutable from 'immutable';
     2 import * as types from '../constants/actionTypes';
     2 import * as types from '../constants/actionTypes';
     3 import NoteRecord from '../store/noteRecord';
     3 import NoteRecord from '../store/noteRecord';
       
     4 import { ActionEnum } from '../constants';
     4 
     5 
     5 const findNoteIndex = (notes, id) => {
     6 const findNoteIndex = (notes, id) => {
     6   return notes.findIndex((note) => note.get('_id') === id);
     7   return notes.findIndex((note) => note.get('_id') === id);
     7 }
     8 }
     8 
     9 
    16       return state.push(new NoteRecord(action.note));
    17       return state.push(new NoteRecord(action.note));
    17     }
    18     }
    18     case types.DELETE_NOTE: {
    19     case types.DELETE_NOTE: {
    19       const noteIndex = findNoteIndex(state, action.note.get('_id'));
    20       const noteIndex = findNoteIndex(state, action.note.get('_id'));
    20       const note = findNote(state, action.note.get('_id'));
    21       const note = findNote(state, action.note.get('_id'));
    21       return state.set(noteIndex, note.merge({deleted:true, modified:true}));
    22       return state.set(noteIndex, note.merge({action: ActionEnum.DELETED}));
    22     }
    23     }
    23     case types.DO_DELETE_NOTE: {
    24     case types.DO_DELETE_NOTE: {
    24       const noteIndex = state.findIndex((note) => note.get('_id') === action.note.get('_id'));
    25       const noteIndex = state.findIndex((note) => note.get('_id') === action.noteId);
    25       return state.delete(noteIndex);
    26       return state.delete(noteIndex);
    26     }
    27     }
    27     case types.UPDATE_NOTE: {
    28     case types.UPDATE_NOTE: {
    28       const index = findNoteIndex(state, action.note.get('_id'));
    29       const index = findNoteIndex(state, action.note.get('_id'));
    29       const note = findNote(state, action.note.get('_id'));
    30       const note = findNote(state, action.note.get('_id'));
    30       let newNote = note.merge(action.data, {modified:true});
    31       let newAction;
       
    32       switch (note.get('action')) {
       
    33         case ActionEnum.CREATED:
       
    34           newAction = ActionEnum.CREATED;
       
    35           break;
       
    36         case ActionEnum.DELETED: // should not happen, but...
       
    37           newAction = ActionEnum.DELETED;
       
    38           break;
       
    39         default:
       
    40           newAction = ActionEnum.UPDATED;
       
    41       }
       
    42 
       
    43       let newNote = note.merge(action.data, {action: newAction});
    31       return state.set(index, newNote);
    44       return state.set(index, newNote);
    32     }
    45     }
    33     case types.DELETE_SESSION: {
    46     case types.DELETE_SESSION: {
    34       const sessionId = action.session.get('_id');
    47       const sessionId = action.session.get('_id');
    35       return state.map((note) => {
    48       return state.map((note) => {
    36         if(sessionId === note.session) {
    49         if(sessionId === note.session) {
    37           return note.merge({deleted:true, modified:true});
    50           return note.merge({action: ActionEnum.DELETED});
    38         } else {
    51         } else {
    39           return note;
    52           return note;
    40         }
    53         }
    41       })
    54       })
    42     }
    55     }
       
    56     case types.DO_DELETE_SESSION: {
       
    57       return state.filter((note) => action.sessionId !== note.session)
       
    58     }
       
    59     case types.RESET_ACTION_NOTE: {
       
    60       const noteId = action.note.get('_id');
       
    61       const index = state.findIndex((note) => note.get('_id') === noteId);
       
    62       const note = state.get(index);
       
    63       return state.set(index, note.merge({action: ActionEnum.NONE}));
       
    64     }
       
    65     case types.SYNC_RESET_ALL: {
       
    66       return state.map((note) => {
       
    67         if(note.action !== ActionEnum.NONE) {
       
    68           return note.merge({action: ActionEnum.None});
       
    69         } else {
       
    70           return note;
       
    71         }
       
    72       });
       
    73     }
       
    74     case types.LOAD_NOTE: {
       
    75       const noteRec = action.note;
       
    76       const noteId = noteRec.get('_id');
       
    77       const index = state.findIndex((note) => note.get('_id') === noteId);
       
    78       if(index >= 0) {
       
    79         return state.set(index, noteRec);
       
    80       } else {
       
    81         return state.push(noteRec);
       
    82       }
       
    83     }
    43     default:
    84     default:
    44       return state;
    85       return state;
    45   }
    86   }
    46 };
    87 };