|
1 import { select } from 'redux-saga/effects' |
|
2 import { getCreatedNotes, getUpdatedNotes, getDeletedNotes } from './selectors'; |
|
3 import NoteRecord from '../store/noteRecord'; |
|
4 import { doDeleteNote, loadNote, resetActionNote } from '../actions/notesActions'; |
|
5 import Immutable from 'immutable'; |
|
6 import SyncMixin from './BaseSyncronizer'; |
|
7 import WebAnnotationSerializer from '../api/WebAnnotationSerializer'; |
|
8 |
|
9 class NoteSyncBase { |
|
10 |
|
11 // local diffs (immutable) |
|
12 * getLocalDiffs() { |
|
13 return Immutable.Map({ |
|
14 created: yield select(getCreatedNotes), |
|
15 updated: yield select(getUpdatedNotes), |
|
16 deleted: yield select(getDeletedNotes) |
|
17 }) |
|
18 } |
|
19 |
|
20 // remote urls |
|
21 getRemoteLoadUrl() { |
|
22 return "/api/notes/notes/"; |
|
23 } |
|
24 |
|
25 getRemoteDeleteUrl(localObjInst) { |
|
26 return `/api/notes/sessions/${localObjInst.get('session')}/notes/${localObjInst.get('_id')}/`; |
|
27 } |
|
28 |
|
29 getRemoteCreateUrl(localObjInst) { |
|
30 return `/api/notes/sessions/${localObjInst.get('session')}/notes/`; |
|
31 } |
|
32 |
|
33 getRemoteUpdateUrl(localObjInst) { |
|
34 return `/api/notes/sessions/${localObjInst.get('session')}/notes/${localObjInst.get('_id')}/`; |
|
35 } |
|
36 |
|
37 // build remote json message |
|
38 getRemoteData(localObjInst) { |
|
39 |
|
40 return { |
|
41 ext_id: localObjInst.get('_id'), |
|
42 session: localObjInst.get('session'), |
|
43 raw: JSON.stringify(localObjInst.get('raw')), |
|
44 plain: localObjInst.get('plain'), |
|
45 html: localObjInst.get('html'), |
|
46 tc_start: localObjInst.get('startedAt'), |
|
47 tc_end: localObjInst.get('finishedAt'), |
|
48 categorization: JSON.stringify(WebAnnotationSerializer.serialize(localObjInst)), |
|
49 margin_note: localObjInst.get('marginComment'), |
|
50 } |
|
51 |
|
52 } |
|
53 |
|
54 getLocalRecord(remoteObj) { |
|
55 return new NoteRecord({ |
|
56 _id: remoteObj.ext_id, |
|
57 session: remoteObj.session, |
|
58 raw: JSON.parse(remoteObj.raw), |
|
59 plain: remoteObj.plain, |
|
60 html: remoteObj.html, |
|
61 startedAt: remoteObj.tc_start, |
|
62 finishedAt: remoteObj.tc_end, |
|
63 categories: remoteObj.categorization, |
|
64 marginComment: remoteObj.margin_note, |
|
65 }); |
|
66 } |
|
67 |
|
68 // actions |
|
69 doDeleteLocalObj(localObjId) { |
|
70 return doDeleteNote(localObjId); |
|
71 } |
|
72 |
|
73 resetLocalObj(localObjInst) { |
|
74 return resetActionNote(localObjInst); |
|
75 } |
|
76 |
|
77 loadObj(objRecord) { |
|
78 return loadNote(objRecord); |
|
79 } |
|
80 |
|
81 } |
|
82 |
|
83 export class NoteSynchronizer extends SyncMixin(NoteSyncBase) {} |
|
84 |
|
85 export default NoteSynchronizer; |