Skip to content

Commit c565d22

Browse files
authored
Load latest state from local storage on reset (#7)
* Load latest state from local storage on reset * lint * v2.0.1
1 parent 0922cb3 commit c565d22

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-sessions",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "Redux action creators for storing session state",
55
"main": "lib/index.js",
66
"scripts": {

src/reducer.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { handleActions } from 'redux-actions'
21
import { set, get } from 'lodash/fp'
32
import * as actions from './actions'
43
import { loadSessionState } from './persistenceHelpers'
@@ -12,17 +11,24 @@ const DEFAULT_USER_TYPE = 'user'
1211
// [userType]: { token, persist },
1312
// [otherUserType]: { token, persist },
1413
// }
15-
const initialState = loadSessionState()
14+
function getInitialState () {
15+
return loadSessionState()
16+
}
1617

1718
// 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+
}
2632

2733
// Selectors
2834
const selectors = {}

test/reducer.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
import { reducer, actions, selectors } from '../src'
2+
import { saveSessionState } from '../src/persistenceHelpers'
3+
4+
describe('reducer', () => {
5+
beforeEach(() => {
6+
localStorage.clear()
7+
sessionStorage.clear()
8+
})
9+
it('loads latest state from local storage when reset', () => {
10+
const initialState = { user: { token: 'first token', persist: true }}
11+
saveSessionState(initialState)
12+
// Passing `undefined` for state resets the reducer
13+
expect(reducer(undefined, {})).toEqual(initialState)
14+
const newInitialState = { user: { token: 'other token', persist: true }}
15+
saveSessionState(newInitialState)
16+
expect(reducer(undefined, {})).toEqual(newInitialState)
17+
})
18+
})
219

320
// Tests for actions and selectors
421

0 commit comments

Comments
 (0)