client/src/actions/notesActions.js
author ymh <ymh.work@gmail.com>
Fri, 23 Jun 2017 17:58:21 +0200
changeset 83 76a4e4b11762
parent 80 b3a02ea6d097
child 86 fa8ef84a1780
permissions -rw-r--r--
add server request for note update and delete

import uuidV1 from 'uuid/v1';

import * as types from '../constants/actionTypes';

export const addNote = (session, data) => {
  const noteId = uuidV1();
  const note = {
    _id: noteId,
    session: session._id,
    raw: data.raw,
    plain: data.plain,
    html: data.html,
    startedAt: data.startedAt,
    finishedAt: data.finishedAt,
    categories: data.categories,
    marginComment: data.marginComment,
  };

  const noteSrvr = {
    ext_id: noteId,
    session: session._id,
    raw: JSON.stringify(data.raw),
    plain: data.plain,
    html: data.html,
    tc_start: data.startedAt,
    tc_end: data.finishedAt,
    categorization: JSON.stringify(data.categories),
  }

  return {
    type: types.ADD_NOTE,
    note,
    meta: {
      offline: {
        effect: {
          url: `/api/notes/sessions/${session.get('_id')}/notes/`,
          method: 'POST',
          data: noteSrvr
        },
        commit: { type: types.NOOP },
        rollback: { type: types.NOOP }
      }
    }
  };
}

export const deleteNote = (note) => {
  return {
    type: types.DELETE_NOTE,
    note,
    meta: {
      offline: {
        effect: {
          url: `/api/notes/sessions/${note.get('session')}/notes/${note.get('_id')}/`,
          method: 'DELETE'
        },
        commit: { type: types.NOOP },
        rollback: { type: types.NOOP }
      }
    }
  };
}

export const updateNote = (note, data) => {
  const noteSrvr = {
    raw: JSON.stringify(data.raw),
    plain: data.plain,
    html: data.html,
    categorization: JSON.stringify(data.categories),
  }

  return {
    type: types.UPDATE_NOTE,
    note,
    data,
    meta: {
      offline: {
        effect: {
          url: `/api/notes/sessions/${note.get('session')}/notes/${note.get('_id')}/`,
          method: 'PUT',
          data: noteSrvr
        },
        commit: { type: types.NOOP },
        rollback: { type: types.NOOP }
      }
    }
  };
}