client/src/sagas/NoteSyncronizer.js
author ymh <ymh.work@gmail.com>
Mon, 08 Oct 2018 18:35:47 +0200
changeset 168 ea92f4fe783d
parent 134 be36eed5e6e0
permissions -rw-r--r--
- move SlateEditor and dependencies to its own folder - remove Immutable - remove redux-persist-immutable - remobe redux-immutable - update libraries - added tests on store manipulations (accessor and reducers)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
import { select } from 'redux-saga/effects'
134
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents: 129
diff changeset
     2
import { getCreatedNotes, getUpdatedNotes, getDeletedNotes } from '../selectors/coreSelectors';
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
import NoteRecord from '../store/noteRecord';
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
import { doDeleteNote, loadNote, resetActionNote } from '../actions/notesActions';
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
import SyncMixin from './BaseSyncronizer';
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
import WebAnnotationSerializer from '../api/WebAnnotationSerializer';
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
     8
import * as R from 'ramda';
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
     9
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
class NoteSyncBase {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
  * getLocalDiffs() {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    13
    return {
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
      created: yield select(getCreatedNotes),
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
      updated: yield select(getUpdatedNotes),
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
      deleted: yield select(getDeletedNotes)
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    17
    }
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
  }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
  // remote urls
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
  getRemoteLoadUrl() {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    return "/api/notes/notes/";
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
  }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
  getRemoteDeleteUrl(localObjInst) {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    26
    return `/api/notes/sessions/${R.prop('session',localObjInst)}/notes/${R.prop('_id',localObjInst)}/`;
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
  }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
  getRemoteCreateUrl(localObjInst) {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    30
    return `/api/notes/sessions/${R.prop('session',localObjInst)}/notes/`;
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
  }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
  getRemoteUpdateUrl(localObjInst) {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    34
    return `/api/notes/sessions/${R.prop('session',localObjInst)}/notes/${R.prop('_id',localObjInst)}/`;
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
  }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
  // build remote json message
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
  getRemoteData(localObjInst) {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
    return {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    41
      ext_id: R.prop('_id',localObjInst),
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    42
      session: R.prop('session',localObjInst),
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    43
      raw: JSON.stringify(R.prop('raw',localObjInst)),
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    44
      plain: R.prop('plain',localObjInst),
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    45
      html: R.prop('html',localObjInst),
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    46
      tc_start: R.prop('startedAt',localObjInst),
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    47
      tc_end: R.prop('finishedAt',localObjInst),
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
      categorization: JSON.stringify(WebAnnotationSerializer.serialize(localObjInst)),
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    49
      margin_note: R.prop('marginComment',localObjInst),
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
    }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
  }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
  getLocalRecord(remoteObj) {
168
ea92f4fe783d - move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    55
    return NoteRecord({
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
      _id: remoteObj.ext_id,
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
      session: remoteObj.session,
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
      raw: JSON.parse(remoteObj.raw),
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
      plain: remoteObj.plain,
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
      html: remoteObj.html,
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
      startedAt: remoteObj.tc_start,
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
      finishedAt: remoteObj.tc_end,
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
      categories: remoteObj.categorization,
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
      marginComment: remoteObj.margin_note,
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
    });
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
  }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
  // actions
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
  doDeleteLocalObj(localObjId) {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
    return doDeleteNote(localObjId);
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
  }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
  resetLocalObj(localObjInst) {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
    return resetActionNote(localObjInst);
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
  }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
  loadObj(objRecord) {
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
    return loadNote(objRecord);
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
  }
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
}
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
export class NoteSynchronizer extends SyncMixin(NoteSyncBase) {}
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
export default NoteSynchronizer;