1 import Immutable from 'immutable'; |
1 import * as R from 'ramda'; |
|
2 |
2 import * as types from '../constants/actionTypes'; |
3 import * as types from '../constants/actionTypes'; |
3 import NoteRecord from '../store/noteRecord'; |
4 import NoteRecord from '../store/noteRecord'; |
4 import { ActionEnum } from '../constants'; |
5 import { ActionEnum } from '../constants'; |
5 |
6 |
6 const findNoteIndex = (notes, id) => { |
7 import { getId, idEq, setAction, getNewAction } from './utils'; |
7 return notes.findIndex((note) => note.get('_id') === id); |
|
8 } |
|
9 |
8 |
10 const findNote = (notes, id) => { |
9 const sessionEq = R.propEq('session'); |
11 return notes.get(findNoteIndex(notes, id)); |
|
12 } |
|
13 |
10 |
14 export default (state = Immutable.List([]), action) => { |
11 export default (state = [], action) => { |
15 switch (action.type) { |
12 switch (action.type) { |
16 case types.ADD_NOTE: { |
13 case types.ADD_NOTE: { |
17 return state.push(new NoteRecord(action.note)); |
14 return R.append(NoteRecord(action.note), state); |
18 } |
15 } |
19 case types.DELETE_NOTE: { |
16 case types.DELETE_NOTE: { |
20 const noteIndex = findNoteIndex(state, action.note.get('_id')); |
17 return R.map(R.when(idEq(getId(action.note)), setAction(ActionEnum.DELETED) ), state); |
21 const note = findNote(state, action.note.get('_id')); |
|
22 return state.set(noteIndex, note.merge({action: ActionEnum.DELETED})); |
|
23 } |
18 } |
24 case types.DO_DELETE_NOTE: { |
19 case types.DO_DELETE_NOTE: { |
25 const noteIndex = state.findIndex((note) => note.get('_id') === action.noteId); |
20 return R.reject(idEq(action.noteId), state) |
26 return state.delete(noteIndex); |
|
27 } |
21 } |
28 case types.UPDATE_NOTE: { |
22 case types.UPDATE_NOTE: { |
29 const index = findNoteIndex(state, action.note.get('_id')); |
23 return R.map(R.when(idEq(getId(action.note)), note => { |
30 const note = findNote(state, action.note.get('_id')); |
24 return R.mergeAll([note, action.data, {action: getNewAction(R.prop('action', note))}]) |
31 let newAction; |
25 }), state); |
32 switch (note.get('action')) { |
|
33 case ActionEnum.CREATED: |
|
34 newAction = ActionEnum.CREATED; |
|
35 break; |
|
36 case ActionEnum.DELETED: // should not happen, but... |
|
37 newAction = ActionEnum.DELETED; |
|
38 break; |
|
39 default: |
|
40 newAction = ActionEnum.UPDATED; |
|
41 } |
|
42 |
|
43 let newNote = note.merge(action.data, {action: newAction}); |
|
44 return state.set(index, newNote); |
|
45 } |
26 } |
46 case types.DELETE_SESSION: { |
27 case types.DELETE_SESSION: { |
47 const sessionId = action.session.get('_id'); |
28 const sessionId = getId(action.session); |
48 return state.map((note) => { |
29 return R.map(R.when(sessionEq(sessionId), setAction(ActionEnum.DELETED)), state); |
49 if(sessionId === note.session) { |
|
50 return note.merge({action: ActionEnum.DELETED}); |
|
51 } else { |
|
52 return note; |
|
53 } |
|
54 }) |
|
55 } |
30 } |
56 case types.DO_DELETE_SESSION: { |
31 case types.DO_DELETE_SESSION: { |
57 return state.filter((note) => action.sessionId !== note.session) |
32 return R.reject(sessionEq(action.sessionId), state) |
58 } |
33 } |
59 case types.RESET_ACTION_NOTE: { |
34 case types.RESET_ACTION_NOTE: { |
60 const noteId = action.note.get('_id'); |
35 return R.map(R.when(idEq(getId(action.note)), setAction(ActionEnum.NONE)), state); |
61 const index = state.findIndex((note) => note.get('_id') === noteId); |
|
62 const note = state.get(index); |
|
63 return state.set(index, note.merge({action: ActionEnum.NONE})); |
|
64 } |
36 } |
65 case types.SYNC_RESET_ALL: { |
37 case types.SYNC_RESET_ALL: { |
66 return state.map((note) => { |
38 return R.map(setAction(ActionEnum.NONE), state); |
67 if(note.action !== ActionEnum.NONE) { |
|
68 return note.merge({action: ActionEnum.None}); |
|
69 } else { |
|
70 return note; |
|
71 } |
|
72 }); |
|
73 } |
39 } |
74 case types.LOAD_NOTE: { |
40 case types.LOAD_NOTE: { |
75 const noteRec = action.note; |
41 const noteRec = action.note; |
76 const noteId = noteRec.get('_id'); |
42 const noteId = getId(noteRec); |
77 const index = state.findIndex((note) => note.get('_id') === noteId); |
43 const index = R.findIndex(idEq(noteId), state); |
78 if(index >= 0) { |
44 if(index >= 0) { |
79 return state.set(index, noteRec); |
45 return R.update(index, noteRec, state); |
80 } else { |
46 } else { |
81 return state.push(noteRec); |
47 return R.append(noteRec, state); |
82 } |
48 } |
83 } |
49 } |
84 case types.AUTH_LOGOUT: { |
50 case types.AUTH_LOGOUT: { |
85 return Immutable.List(); // empty note list on logout |
51 return []; // empty note list on logout |
86 } |
52 } |
87 default: |
53 default: |
88 return state; |
54 return state; |
89 } |
55 } |
90 }; |
56 }; |