Skip to content

Commit

Permalink
refactor(reducers): use handleActions for dropdown-reducer
Browse files Browse the repository at this point in the history
Removes a comment that is no longer true.
Also replaces some Symbol keys with strings so that state is serializable.
  • Loading branch information
davidmason committed Sep 29, 2017
1 parent 76fb061 commit 5d3163d
Showing 1 changed file with 14 additions and 32 deletions.
@@ -1,44 +1,26 @@
import updateObject from 'immutability-helper'
import update from 'immutability-helper'
import { handleActions } from 'redux-actions'
import {
OPEN_DROPDOWN, CLOSE_DROPDOWN, TOGGLE_DROPDOWN
} from '../actions/action-types'

const defaultState = {
openDropdownKey: undefined,
docsKey: Symbol('Documents Dropdown'),
localeKey: Symbol('Locales Dropdown'),
uiLocaleKey: Symbol('UI Locales Dropdown')
docsKey: 'Documents Dropdown',
localeKey: 'Locales Dropdown',
uiLocaleKey: 'UI Locales Dropdown'
}

/**
* State is just the key of the currently open dropdown, or undefined if
* no dropdown is open.
*/
const dropdownReducer = (state = defaultState, action) => {
switch (action.type) {
case OPEN_DROPDOWN:
return update({
openDropdownKey: {$set: action.payload}
})
const dropdownReducer = handleActions({
[OPEN_DROPDOWN]: (state, { payload }) =>
update(state, { openDropdownKey: {$set: payload} }),

case CLOSE_DROPDOWN:
return update({
openDropdownKey: {$set: undefined}
})
[CLOSE_DROPDOWN]: state =>
update(state, { openDropdownKey: {$set: undefined} }),

case TOGGLE_DROPDOWN:
const isOpen = action.payload === state.openDropdownKey
return update({
openDropdownKey: {$set: isOpen ? undefined : action.payload}
})

default:
return state
}

function update (commands) {
return updateObject(state, commands)
}
}
[TOGGLE_DROPDOWN]: (state, { payload }) => update(state, { openDropdownKey:
{$set: payload === state.openDropdownKey ? undefined : payload}
})
}, defaultState)

export default dropdownReducer

0 comments on commit 5d3163d

Please sign in to comment.