aboutsummaryrefslogtreecommitdiff
path: root/src/client/react/store/reducers.js
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/client/react/store/reducers.js
parent784c113c29dbd2e40b72a46ac92b7c569609e117 (diff)
client/reducer: Use withinRange
Diffstat (limited to 'src/client/react/store/reducers.js')
-rw-r--r--src/client/react/store/reducers.js12
1 files changed, 5 insertions, 7 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];