diff options
author | Noah Loomans <noahloomans@gmail.com> | 2018-07-06 16:14:30 +0200 |
---|---|---|
committer | Noah Loomans <noahloomans@gmail.com> | 2018-07-06 16:14:30 +0200 |
commit | ebb14ffc54670c8e2cbeba18eac238965eee4e81 (patch) | |
tree | d411ba6066a0ee4f38e85a6327456b9b4bc4b13b /src/client/react/store | |
parent | 9f935565ebe09444b7b576b0be986c6271baef39 (diff) |
client: Simplify setUser
Diffstat (limited to 'src/client/react/store')
-rw-r--r-- | src/client/react/store/actions.js | 14 | ||||
-rw-r--r-- | src/client/react/store/reducers.js | 26 | ||||
-rw-r--r-- | src/client/react/store/reducers.test.js | 37 |
3 files changed, 28 insertions, 49 deletions
diff --git a/src/client/react/store/actions.js b/src/client/react/store/actions.js index 36dc748..2d2cd30 100644 --- a/src/client/react/store/actions.js +++ b/src/client/react/store/actions.js @@ -4,18 +4,10 @@ import withinRange from '../lib/withinRange'; export function setUser(newUser) { return (dispatch, getState, { getHistory }) => { - const { user, updatePathname } = getHistory(); + const { updatePathname } = getHistory(); - if (newUser === user) { - // EDGE CASE: If the user that is being selected is equal to the user - // that is already selected, then updatePathname will not change - // anything. This results in state.search not properly being resetted. - // Example: If you search for 5H2 while already viewing 6-5H2, pressing - // enter won't do anything unless this check is present. - dispatch({ type: 'SEARCH/SET_USER', user }); - } else { - updatePathname(newUser || ''); - } + dispatch({ type: 'SEARCH/RESET' }); + updatePathname(newUser); }; } diff --git a/src/client/react/store/reducers.js b/src/client/react/store/reducers.js index 34c6d99..4a3576d 100644 --- a/src/client/react/store/reducers.js +++ b/src/client/react/store/reducers.js @@ -19,15 +19,10 @@ */ import getSearchResults from '../lib/getSearchResults'; -import users from '../users'; import withinRange from '../lib/withinRange'; const DEFAULT_STATE = { - search: { - results: [], - text: '', - selected: null, - }, + search: null, isRoomFinderVisible: false, schedules: {}, }; @@ -52,23 +47,10 @@ const schedule = (state = {}, action) => { function reducer(state = DEFAULT_STATE, action) { switch (action.type) { - case 'SEARCH/SET_USER': { - const { user } = action; - - if (user == null) { - return { - ...state, - search: DEFAULT_STATE.search, - }; - } - + case 'SEARCH/RESET': { return { ...state, - search: { - results: [], - text: users.byId[user].value, - selected: user, - }, + search: null, }; } @@ -86,7 +68,7 @@ function reducer(state = DEFAULT_STATE, action) { } case 'SEARCH/CHANGE_SELECTED_RESULT': { - if (state.search.results.length === 0) { + if (!state.search || state.search.results.length === 0) { return state; } diff --git a/src/client/react/store/reducers.test.js b/src/client/react/store/reducers.test.js index 3f6aff1..89cd438 100644 --- a/src/client/react/store/reducers.test.js +++ b/src/client/react/store/reducers.test.js @@ -39,26 +39,15 @@ describe('reducers', () => { deepFreeze(DEFAULT_STATE); }); - describe('SEARCH/SET_USER', () => { - it('Resets the search state if the user is null', () => { + describe('SEARCH/RESET', () => { + it('Resets the search state', () => { const prevState = { search: { foo: 'bar' } }; - const action = { type: 'SEARCH/SET_USER', user: null }; + const action = { type: 'SEARCH/RESET', user: null }; deepFreeze([prevState, action]); expect(reducer(prevState, action)).toEqual({ - search: DEFAULT_STATE.search, - }); - }); - - it('Sets all the values of that user properly', () => { - expect(reducer(undefined, { type: 'SEARCH/SET_USER', user: 's/18561' })).toEqual({ - ...DEFAULT_STATE, - search: { - results: [], - text: '18561', - selected: 's/18561', - }, + search: null, }); }); }); @@ -108,7 +97,7 @@ describe('reducers', () => { }); describe('SEARCH/CHANGE_SELECTED_RESULT', () => { - describe('State has no results', () => { + describe('State is empty', () => { it('Does nothing', () => { const actionPlus = { type: 'SEARCH/CHANGE_SELECTED_RESULT', relativeChange: +1 }; const actionMin = { type: 'SEARCH/CHANGE_SELECTED_RESULT', relativeChange: -1 }; @@ -122,6 +111,22 @@ describe('reducers', () => { }); }); + describe('State has no results', () => { + it('Does nothing', () => { + const state = { ...DEFAULT_STATE, search: { results: [] } }; + + const actionPlus = { type: 'SEARCH/CHANGE_SELECTED_RESULT', relativeChange: +1 }; + const actionMin = { type: 'SEARCH/CHANGE_SELECTED_RESULT', relativeChange: -1 }; + + deepFreeze([actionPlus, actionMin]); + + const nextStatePlus = reducer(state, actionPlus); + const nextStateMin = reducer(state, actionMin); + expect(nextStatePlus).toEqual(state); + expect(nextStateMin).toEqual(state); + }); + }); + describe('State has many results', () => { it('Switches to the correct selectedResult', () => { const prevState = { |