| author | ymh <ymh.work@gmail.com> |
| Tue, 01 Aug 2017 12:20:14 +0200 | |
| changeset 132 | 906a6c7c7943 |
| parent 129 | d48946d164c6 |
| child 168 | ea92f4fe783d |
| permissions | -rw-r--r-- |
|
3
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1 |
import Immutable from 'immutable'; |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2 |
import * as types from '../constants/actionTypes'; |
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
3 |
import NoteRecord from '../store/noteRecord'; |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
4 |
import { ActionEnum } from '../constants'; |
|
3
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
5 |
|
|
80
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
6 |
const findNoteIndex = (notes, id) => { |
|
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
7 |
return notes.findIndex((note) => note.get('_id') === id); |
|
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
8 |
} |
|
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
9 |
|
|
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
10 |
const findNote = (notes, id) => { |
|
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
11 |
return notes.get(findNoteIndex(notes, id)); |
|
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
12 |
} |
|
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
13 |
|
|
3
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
14 |
export default (state = Immutable.List([]), action) => { |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
15 |
switch (action.type) { |
|
124
c77570164050
implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
16 |
case types.ADD_NOTE: { |
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
17 |
return state.push(new NoteRecord(action.note)); |
|
124
c77570164050
implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
18 |
} |
|
c77570164050
implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
19 |
case types.DELETE_NOTE: { |
|
c77570164050
implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
20 |
const noteIndex = findNoteIndex(state, action.note.get('_id')); |
|
c77570164050
implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
21 |
const note = findNote(state, action.note.get('_id')); |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
22 |
return state.set(noteIndex, note.merge({action: ActionEnum.DELETED})); |
|
124
c77570164050
implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
23 |
} |
|
c77570164050
implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
24 |
case types.DO_DELETE_NOTE: { |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
25 |
const noteIndex = state.findIndex((note) => note.get('_id') === action.noteId); |
|
79
772b73e31069
Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
66
diff
changeset
|
26 |
return state.delete(noteIndex); |
|
124
c77570164050
implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
27 |
} |
|
c77570164050
implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
28 |
case types.UPDATE_NOTE: { |
|
80
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
29 |
const index = findNoteIndex(state, action.note.get('_id')); |
|
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
30 |
const note = findNote(state, action.note.get('_id')); |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
31 |
let newAction; |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
32 |
switch (note.get('action')) { |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
33 |
case ActionEnum.CREATED: |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
34 |
newAction = ActionEnum.CREATED; |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
35 |
break; |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
36 |
case ActionEnum.DELETED: // should not happen, but... |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
37 |
newAction = ActionEnum.DELETED; |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
38 |
break; |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
39 |
default: |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
40 |
newAction = ActionEnum.UPDATED; |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
41 |
} |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
42 |
|
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
43 |
let newNote = note.merge(action.data, {action: newAction}); |
|
80
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
44 |
return state.set(index, newNote); |
|
124
c77570164050
implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
45 |
} |
|
c77570164050
implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
46 |
case types.DELETE_SESSION: { |
|
c77570164050
implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
47 |
const sessionId = action.session.get('_id'); |
|
c77570164050
implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
48 |
return state.map((note) => { |
|
c77570164050
implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
49 |
if(sessionId === note.session) { |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
50 |
return note.merge({action: ActionEnum.DELETED}); |
|
124
c77570164050
implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
51 |
} else { |
|
c77570164050
implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
52 |
return note; |
|
c77570164050
implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
53 |
} |
|
c77570164050
implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
54 |
}) |
|
c77570164050
implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
55 |
} |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
56 |
case types.DO_DELETE_SESSION: { |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
57 |
return state.filter((note) => action.sessionId !== note.session) |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
58 |
} |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
59 |
case types.RESET_ACTION_NOTE: { |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
60 |
const noteId = action.note.get('_id'); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
61 |
const index = state.findIndex((note) => note.get('_id') === noteId); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
62 |
const note = state.get(index); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
63 |
return state.set(index, note.merge({action: ActionEnum.NONE})); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
64 |
} |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
65 |
case types.SYNC_RESET_ALL: { |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
66 |
return state.map((note) => { |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
67 |
if(note.action !== ActionEnum.NONE) { |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
68 |
return note.merge({action: ActionEnum.None}); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
69 |
} else { |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
70 |
return note; |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
71 |
} |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
72 |
}); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
73 |
} |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
74 |
case types.LOAD_NOTE: { |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
75 |
const noteRec = action.note; |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
76 |
const noteId = noteRec.get('_id'); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
77 |
const index = state.findIndex((note) => note.get('_id') === noteId); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
78 |
if(index >= 0) { |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
79 |
return state.set(index, noteRec); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
80 |
} else { |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
81 |
return state.push(noteRec); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
82 |
} |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
83 |
} |
|
132
906a6c7c7943
add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
84 |
case types.AUTH_LOGOUT: { |
|
906a6c7c7943
add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
85 |
return Immutable.List(); // empty note list on logout |
|
906a6c7c7943
add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
86 |
} |
|
3
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
87 |
default: |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
88 |
return state; |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
89 |
} |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
90 |
}; |