diff options
author | Noah Loomans <noahloomans@gmail.com> | 2018-07-03 21:27:05 +0200 |
---|---|---|
committer | Noah Loomans <noahloomans@gmail.com> | 2018-07-03 21:27:40 +0200 |
commit | 42577eab58aa323c07b8efcc3c8c69b9140b9077 (patch) | |
tree | c41b843587b441f243747360453222ef8655ea5f /src/client/react/store | |
parent | 784c113c29dbd2e40b72a46ac92b7c569609e117 (diff) |
client/reducer: Use withinRange
Diffstat (limited to 'src/client/react/store')
-rw-r--r-- | src/client/react/store/reducers.js | 12 | ||||
-rw-r--r-- | src/client/react/store/reducers.test.js | 32 |
2 files changed, 5 insertions, 39 deletions
diff --git a/src/client/react/store/reducers.js b/src/client/react/store/reducers.js index c5c564c..34c6d99 100644 --- a/src/client/react/store/reducers.js +++ b/src/client/react/store/reducers.js @@ -20,6 +20,7 @@ import getSearchResults from '../lib/getSearchResults'; import users from '../users'; +import withinRange from '../lib/withinRange'; const DEFAULT_STATE = { search: { @@ -91,13 +92,10 @@ function reducer(state = DEFAULT_STATE, action) { const prevSelectedResult = state.search.selected; const prevSelectedResultIndex = state.search.results.indexOf(prevSelectedResult); - let nextSelectedResultIndex = prevSelectedResultIndex + action.relativeChange; - - if (nextSelectedResultIndex < 0) { - nextSelectedResultIndex = state.search.results.length - 1; - } else if (nextSelectedResultIndex > state.search.results.length - 1) { - nextSelectedResultIndex = 0; - } + const nextSelectedResultIndex = withinRange( + prevSelectedResultIndex + action.relativeChange, + state.search.results.length - 1, + ); const nextSelectedResult = state.search.results[nextSelectedResultIndex]; diff --git a/src/client/react/store/reducers.test.js b/src/client/react/store/reducers.test.js index ae4d664..3f6aff1 100644 --- a/src/client/react/store/reducers.test.js +++ b/src/client/react/store/reducers.test.js @@ -173,22 +173,6 @@ describe('reducers', () => { selected: 's/18561', }, }); - - expect(reducer({ - ...DEFAULT_STATE, - search: { - results: ['s/18561', 's/18562', 's/18563'], - text: '1856', - selected: 's/18563', - }, - }, { type: 'SEARCH/CHANGE_SELECTED_RESULT', relativeChange: +1 })).toEqual({ - ...DEFAULT_STATE, - search: { - results: ['s/18561', 's/18562', 's/18563'], - text: '1856', - selected: 's/18561', - }, - }); }); it('Properly wraps around when decrementing', () => { @@ -207,22 +191,6 @@ describe('reducers', () => { selected: 's/18563', }, }); - - expect(reducer({ - ...DEFAULT_STATE, - search: { - results: ['s/18561', 's/18562', 's/18563'], - text: '1856', - selected: null, - }, - }, { type: 'SEARCH/CHANGE_SELECTED_RESULT', relativeChange: -1 })).toEqual({ - ...DEFAULT_STATE, - search: { - results: ['s/18561', 's/18562', 's/18563'], - text: '1856', - selected: 's/18563', - }, - }); }); }); }); |