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 SessionRecord from '../store/sessionRecord'; |
3 import SessionRecord from '../store/sessionRecord'; |
4 |
4 |
5 export const sessions = (state = Immutable.List([]), action) => { |
5 export const sessions = (state = Immutable.List([]), action) => { |
6 let sessionIndex; |
|
7 |
6 |
8 switch (action.type) { |
7 switch (action.type) { |
9 case types.CREATE_SESSION: |
8 |
|
9 case types.CREATE_SESSION: { |
10 return state.push(new SessionRecord(action.session)); |
10 return state.push(new SessionRecord(action.session)); |
11 case types.UPDATE_SESSION: |
11 } |
|
12 case types.UPDATE_SESSION: { |
12 const sessionToUpdate = state.find(session => session === action.session); |
13 const sessionToUpdate = state.find(session => session === action.session); |
13 sessionIndex = state.indexOf(action.session); |
14 const sessionIndex = state.indexOf(action.session); |
14 if (sessionIndex === -1) { |
15 if (sessionIndex === -1) { |
15 return state; |
16 return state; |
16 } |
17 } |
17 const updatedSession = sessionToUpdate.merge(action.values); |
18 const updatedSession = sessionToUpdate.merge(action.values, {modified: true}); |
18 return state.set(sessionIndex, updatedSession); |
19 return state.set(sessionIndex, updatedSession); |
19 case types.DELETE_SESSION: |
20 } |
20 sessionIndex = state.indexOf(action.session); |
21 case types.DO_DELETE_SESSION: { |
|
22 return state.filter((note) => action.session.get('_id') !== note.session) |
|
23 } |
|
24 case types.DELETE_SESSION: { |
|
25 const sessionIndex = state.indexOf(action.session); |
21 if (sessionIndex === -1) { |
26 if (sessionIndex === -1) { |
22 return state; |
27 return state; |
23 } |
28 } |
24 return state.delete(sessionIndex); |
29 const deletedSession = state.get(sessionIndex); |
25 case types.LOAD_SESSIONS: |
30 return state.set(sessionIndex, deletedSession.merge({deleted:true, modified:true})); |
|
31 } |
|
32 case types.LOAD_SESSIONS: { |
26 return action.sessions; |
33 return action.sessions; |
|
34 } |
27 default: |
35 default: |
28 return state; |
36 return state; |
29 } |
37 } |
30 }; |
38 }; |