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

import * as R from 'ramda';

import * as types from '../constants/actionTypes';
import NoteRecord from '../store/noteRecord';
import { ActionEnum } from '../constants';

import { getId, idEq, setAction, getNewAction } from './utils';

const sessionEq = R.propEq('session');

export default (state = [], action) => {
  switch (action.type) {
    case types.ADD_NOTE: {
      return R.append(NoteRecord(action.note), state);
    }
    case types.DELETE_NOTE: {
      return R.map(R.when(idEq(getId(action.note)), setAction(ActionEnum.DELETED) ), state);
    }
    case types.DO_DELETE_NOTE: {
      return R.reject(idEq(action.noteId), state)
    }
    case types.UPDATE_NOTE: {
      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 = getId(action.session);
      return R.map(R.when(sessionEq(sessionId), setAction(ActionEnum.DELETED)), state);
    }
    case types.DO_DELETE_SESSION: {
      return R.reject(sessionEq(action.sessionId), state)
    }
    case types.RESET_ACTION_NOTE: {
      return R.map(R.when(idEq(getId(action.note)), setAction(ActionEnum.NONE)), state);
    }
    case types.SYNC_RESET_ALL: {
      return R.map(setAction(ActionEnum.NONE), state);
    }
    case types.LOAD_NOTE: {
      const noteRec = action.note;
      const noteId = getId(noteRec);
      const index = R.findIndex(idEq(noteId), state);
      if(index >= 0) {
        return  R.update(index, noteRec, state);
      } else {
        return R.append(noteRec, state);
      }
    }
    case types.AUTH_LOGOUT: {
      return []; // empty note list on logout
    }
    default:
      return state;
  }
};