aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNoah Loomans <noahloomans@gmail.com>2018-07-03 21:27:05 +0200
committerNoah Loomans <noahloomans@gmail.com>2018-07-03 21:27:40 +0200
commit42577eab58aa323c07b8efcc3c8c69b9140b9077 (patch)
treec41b843587b441f243747360453222ef8655ea5f /src
parent784c113c29dbd2e40b72a46ac92b7c569609e117 (diff)
client/reducer: Use withinRange
Diffstat (limited to 'src')
-rw-r--r--src/client/react/store/reducers.js12
-rw-r--r--src/client/react/store/reducers.test.js32
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',
- },
- });
});
});
});