1 import Immutable from 'immutable'; |
1 import Immutable from 'immutable'; |
2 import * as types from '../constants/actionTypes'; |
2 import * as types from '../constants/actionTypes'; |
3 import NoteRecord from '../store/noteRecord'; |
3 import NoteRecord from '../store/noteRecord'; |
|
4 |
|
5 const findNoteIndex = (notes, id) => { |
|
6 return notes.findIndex((note) => note.get('_id') === id); |
|
7 } |
|
8 |
|
9 const findNote = (notes, id) => { |
|
10 return notes.get(findNoteIndex(notes, id)); |
|
11 } |
4 |
12 |
5 export default (state = Immutable.List([]), action) => { |
13 export default (state = Immutable.List([]), action) => { |
6 switch (action.type) { |
14 switch (action.type) { |
7 case types.ADD_NOTE: |
15 case types.ADD_NOTE: |
8 return state.push(new NoteRecord(action.note)); |
16 return state.push(new NoteRecord(action.note)); |
9 case types.DELETE_NOTE: |
17 case types.DELETE_NOTE: |
10 const noteIndex = state.findIndex((note) => note.get('_id') === action.note.get('_id')); |
18 const noteIndex = state.findIndex((note) => note.get('_id') === action.note.get('_id')); |
11 return state.delete(noteIndex); |
19 return state.delete(noteIndex); |
|
20 case types.UPDATE_NOTE: |
|
21 const index = findNoteIndex(state, action.note.get('_id')); |
|
22 const note = findNote(state, action.note.get('_id')); |
|
23 const newNote = note |
|
24 .set('plain', action.data.plain) |
|
25 .set('raw', action.data.raw) |
|
26 .set('html', action.data.html) |
|
27 .set('categories', action.data.categories); |
|
28 return state.set(index, newNote); |
12 default: |
29 default: |
13 return state; |
30 return state; |
14 } |
31 } |
15 }; |
32 }; |