Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(reducers): use handleActions for dropdown-reducer
Removes a comment that is no longer true. Also replaces some Symbol keys with strings so that state is serializable.
- Loading branch information
1 parent
76fb061
commit 5d3163d
Showing
1 changed file
with
14 additions
and
32 deletions.
There are no files selected for viewing
46 changes: 14 additions & 32 deletions
46
server/zanata-frontend/src/frontend/app/editor/reducers/dropdown-reducer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |