Skip to content

Commit

Permalink
Load latest state from local storage on reset (#7)
Browse files Browse the repository at this point in the history
* Load latest state from local storage on reset

* lint

* v2.0.1
  • Loading branch information
dpikt authored Jun 11, 2019
1 parent 0922cb3 commit c565d22
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-sessions",
"version": "2.0.0",
"version": "2.0.1",
"description": "Redux action creators for storing session state",
"main": "lib/index.js",
"scripts": {
Expand Down
26 changes: 16 additions & 10 deletions src/reducer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { handleActions } from 'redux-actions'
import { set, get } from 'lodash/fp'
import * as actions from './actions'
import { loadSessionState } from './persistenceHelpers'
Expand All @@ -12,17 +11,24 @@ const DEFAULT_USER_TYPE = 'user'
// [userType]: { token, persist },
// [otherUserType]: { token, persist },
// }
const initialState = loadSessionState()
function getInitialState () {
return loadSessionState()
}

// Reducer
const reducer = handleActions({
[actions.setToken]: (state, { payload: { token, userType=DEFAULT_USER_TYPE, persist=true } }) => {
return set(userType, { token, persist }, state)
},
[actions.clearToken]: (state, { payload: { userType=DEFAULT_USER_TYPE }={}}) => {
return set(userType, { token: null, persist: false }, state)
},
}, initialState)
function reducer (state, action) {
if (state === undefined) return getInitialState()
const handlers = {
[actions.setToken]: (state, { payload: { token, userType=DEFAULT_USER_TYPE, persist=true } }) => {
return set(userType, { token, persist }, state)
},
[actions.clearToken]: (state, { payload: { userType=DEFAULT_USER_TYPE }={}}) => {
return set(userType, { token: null, persist: false }, state)
}
}
const handler = handlers[action.type]
return handler ? handler(state, action) : state
}

// Selectors
const selectors = {}
Expand Down
17 changes: 17 additions & 0 deletions test/reducer.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
import { reducer, actions, selectors } from '../src'
import { saveSessionState } from '../src/persistenceHelpers'

describe('reducer', () => {
beforeEach(() => {
localStorage.clear()
sessionStorage.clear()
})
it('loads latest state from local storage when reset', () => {
const initialState = { user: { token: 'first token', persist: true }}
saveSessionState(initialState)
// Passing `undefined` for state resets the reducer
expect(reducer(undefined, {})).toEqual(initialState)
const newInitialState = { user: { token: 'other token', persist: true }}
saveSessionState(newInitialState)
expect(reducer(undefined, {})).toEqual(newInitialState)
})
})

// Tests for actions and selectors

Expand Down

0 comments on commit c565d22

Please sign in to comment.