--- a/client/src/reducers/notesReducer.js Tue Oct 09 19:07:47 2018 +0200
+++ b/client/src/reducers/notesReducer.js Mon Oct 08 18:35:47 2018 +0200
@@ -1,88 +1,54 @@
-import Immutable from 'immutable';
+import * as R from 'ramda';
+
import * as types from '../constants/actionTypes';
import NoteRecord from '../store/noteRecord';
import { ActionEnum } from '../constants';
-const findNoteIndex = (notes, id) => {
- return notes.findIndex((note) => note.get('_id') === id);
-}
+import { getId, idEq, setAction, getNewAction } from './utils';
-const findNote = (notes, id) => {
- return notes.get(findNoteIndex(notes, id));
-}
+const sessionEq = R.propEq('session');
-export default (state = Immutable.List([]), action) => {
+export default (state = [], action) => {
switch (action.type) {
case types.ADD_NOTE: {
- return state.push(new NoteRecord(action.note));
+ return R.append(NoteRecord(action.note), state);
}
case types.DELETE_NOTE: {
- const noteIndex = findNoteIndex(state, action.note.get('_id'));
- const note = findNote(state, action.note.get('_id'));
- return state.set(noteIndex, note.merge({action: ActionEnum.DELETED}));
+ return R.map(R.when(idEq(getId(action.note)), setAction(ActionEnum.DELETED) ), state);
}
case types.DO_DELETE_NOTE: {
- const noteIndex = state.findIndex((note) => note.get('_id') === action.noteId);
- return state.delete(noteIndex);
+ return R.reject(idEq(action.noteId), state)
}
case types.UPDATE_NOTE: {
- const index = findNoteIndex(state, action.note.get('_id'));
- const note = findNote(state, action.note.get('_id'));
- let newAction;
- switch (note.get('action')) {
- case ActionEnum.CREATED:
- newAction = ActionEnum.CREATED;
- break;
- case ActionEnum.DELETED: // should not happen, but...
- newAction = ActionEnum.DELETED;
- break;
- default:
- newAction = ActionEnum.UPDATED;
- }
-
- let newNote = note.merge(action.data, {action: newAction});
- return state.set(index, newNote);
+ return R.map(R.when(idEq(getId(action.note)), note => {
+ return R.mergeAll([note, action.data, {action: getNewAction(R.prop('action', note))}])
+ }), state);
}
case types.DELETE_SESSION: {
- const sessionId = action.session.get('_id');
- return state.map((note) => {
- if(sessionId === note.session) {
- return note.merge({action: ActionEnum.DELETED});
- } else {
- return note;
- }
- })
+ const sessionId = getId(action.session);
+ return R.map(R.when(sessionEq(sessionId), setAction(ActionEnum.DELETED)), state);
}
case types.DO_DELETE_SESSION: {
- return state.filter((note) => action.sessionId !== note.session)
+ return R.reject(sessionEq(action.sessionId), state)
}
case types.RESET_ACTION_NOTE: {
- const noteId = action.note.get('_id');
- const index = state.findIndex((note) => note.get('_id') === noteId);
- const note = state.get(index);
- return state.set(index, note.merge({action: ActionEnum.NONE}));
+ return R.map(R.when(idEq(getId(action.note)), setAction(ActionEnum.NONE)), state);
}
case types.SYNC_RESET_ALL: {
- return state.map((note) => {
- if(note.action !== ActionEnum.NONE) {
- return note.merge({action: ActionEnum.None});
- } else {
- return note;
- }
- });
+ return R.map(setAction(ActionEnum.NONE), state);
}
case types.LOAD_NOTE: {
const noteRec = action.note;
- const noteId = noteRec.get('_id');
- const index = state.findIndex((note) => note.get('_id') === noteId);
+ const noteId = getId(noteRec);
+ const index = R.findIndex(idEq(noteId), state);
if(index >= 0) {
- return state.set(index, noteRec);
+ return R.update(index, noteRec, state);
} else {
- return state.push(noteRec);
+ return R.append(noteRec, state);
}
}
case types.AUTH_LOGOUT: {
- return Immutable.List(); // empty note list on logout
+ return []; // empty note list on logout
}
default:
return state;