1
- import { handleActions } from 'redux-actions'
2
1
import { set , get } from 'lodash/fp'
3
2
import * as actions from './actions'
4
3
import { loadSessionState } from './persistenceHelpers'
@@ -12,17 +11,24 @@ const DEFAULT_USER_TYPE = 'user'
12
11
// [userType]: { token, persist },
13
12
// [otherUserType]: { token, persist },
14
13
// }
15
- const initialState = loadSessionState ( )
14
+ function getInitialState ( ) {
15
+ return loadSessionState ( )
16
+ }
16
17
17
18
// Reducer
18
- const reducer = handleActions ( {
19
- [ actions . setToken ] : ( state , { payload : { token, userType= DEFAULT_USER_TYPE , persist= true } } ) => {
20
- return set ( userType , { token, persist } , state )
21
- } ,
22
- [ actions . clearToken ] : ( state , { payload : { userType= DEFAULT_USER_TYPE } = { } } ) => {
23
- return set ( userType , { token : null , persist : false } , state )
24
- } ,
25
- } , initialState )
19
+ function reducer ( state , action ) {
20
+ if ( state === undefined ) return getInitialState ( )
21
+ const handlers = {
22
+ [ actions . setToken ] : ( state , { payload : { token, userType= DEFAULT_USER_TYPE , persist= true } } ) => {
23
+ return set ( userType , { token, persist } , state )
24
+ } ,
25
+ [ actions . clearToken ] : ( state , { payload : { userType= DEFAULT_USER_TYPE } = { } } ) => {
26
+ return set ( userType , { token : null , persist : false } , state )
27
+ }
28
+ }
29
+ const handler = handlers [ action . type ]
30
+ return handler ? handler ( state , action ) : state
31
+ }
26
32
27
33
// Selectors
28
34
const selectors = { }
0 commit comments