From 41620ceb096a4c3d94bb83cf9a56077939d89a2c Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Thu, 28 Jun 2018 16:03:46 +0200 Subject: Refactor search --- src/client/react/store/actions.js | 2 +- src/client/react/store/reducers.js | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) (limited to 'src/client/react/store') diff --git a/src/client/react/store/actions.js b/src/client/react/store/actions.js index 46a38ab..e6aa8f6 100644 --- a/src/client/react/store/actions.js +++ b/src/client/react/store/actions.js @@ -13,7 +13,7 @@ export function setUser(newUser) { // Therefor, we need to dispatch the SET_USER command manually. dispatch({ type: 'SEARCH/SET_USER', user }); } else { - updatePathname(newUser); + updatePathname(newUser || ''); } }; } diff --git a/src/client/react/store/reducers.js b/src/client/react/store/reducers.js index c2ee7e9..3425de0 100644 --- a/src/client/react/store/reducers.js +++ b/src/client/react/store/reducers.js @@ -74,30 +74,31 @@ function reducer(state = DEFAULT_STATE, action) { }; } - case 'SEARCH/INPUT_CHANGE': + case 'SEARCH/INPUT_CHANGE': { + const results = getSearchResults(action.searchText); + return { ...state, search: { - results: getSearchResults(action.searchText), + results, text: action.searchText, - selected: null, + selected: results.length > 0 ? results[0] : null, }, }; + } case 'SEARCH/CHANGE_SELECTED_RESULT': { const prevSelectedResult = state.search.selected; const prevSelectedResultIndex = state.search.results.indexOf(prevSelectedResult); let nextSelectedResultIndex = prevSelectedResultIndex + action.relativeChange; - if (nextSelectedResultIndex < -1) { + if (nextSelectedResultIndex < 0) { nextSelectedResultIndex = state.search.results.length - 1; } else if (nextSelectedResultIndex > state.search.results.length - 1) { - nextSelectedResultIndex = -1; + nextSelectedResultIndex = 0; } - const nextSelectedResult = nextSelectedResultIndex === -1 - ? null - : state.search.results[nextSelectedResultIndex]; + const nextSelectedResult = state.search.results[nextSelectedResultIndex]; return { ...state, -- cgit v1.1