From ebb14ffc54670c8e2cbeba18eac238965eee4e81 Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Fri, 6 Jul 2018 16:14:30 +0200 Subject: client: Simplify setUser --- src/client/react/components/container/HelpBox.js | 2 +- src/client/react/components/container/Results.js | 20 +++++--------------- src/client/react/components/container/Search.js | 23 +++++++++++++++++------ 3 files changed, 23 insertions(+), 22 deletions(-) (limited to 'src/client/react/components/container') diff --git a/src/client/react/components/container/HelpBox.js b/src/client/react/components/container/HelpBox.js index 30b2942..d10c26b 100644 --- a/src/client/react/components/container/HelpBox.js +++ b/src/client/react/components/container/HelpBox.js @@ -22,7 +22,7 @@ import { connect } from 'react-redux'; import HelpBox from '../presentational/HelpBox'; const mapStateToProps = state => ({ - isVisible: state.search.results.length === 0 && state.search.text === '', + isVisible: !state.search || (state.search.results.length === 0 && state.search.text === ''), }); export default connect(mapStateToProps)(HelpBox); diff --git a/src/client/react/components/container/Results.js b/src/client/react/components/container/Results.js index 0d761d1..398adaa 100644 --- a/src/client/react/components/container/Results.js +++ b/src/client/react/components/container/Results.js @@ -19,27 +19,17 @@ */ import { connect } from 'react-redux'; -import { withRouter } from 'react-router-dom'; - -import users from '../../users'; -import { userFromMatch } from '../../lib/url'; import { setUser } from '../../store/actions'; import Results from '../presentational/Results'; -const mapStateToProps = (state, { match }) => { - const user = userFromMatch(match); - const searchText = state.search.text; - - return { - isExactMatch: user != null && searchText === users.byId[user].value, - results: state.search.results, - selectedResult: state.search.selected, - }; -}; +const mapStateToProps = state => ({ + results: state.search ? state.search.results : undefined, + selectedResult: state.search && state.search.selected, +}); const mapDispatchToProps = dispatch => ({ setUser: user => dispatch(setUser(user)), }); -export default withRouter(connect(mapStateToProps, mapDispatchToProps)(Results)); +export default connect(mapStateToProps, mapDispatchToProps)(Results); diff --git a/src/client/react/components/container/Search.js b/src/client/react/components/container/Search.js index b680fc6..73919d3 100644 --- a/src/client/react/components/container/Search.js +++ b/src/client/react/components/container/Search.js @@ -28,21 +28,32 @@ import users from '../../users'; const mapStateToProps = (state, { location }) => { const currentUser = userFromLocation(location); - const searchText = state.search.text; - const isExactMatch = currentUser != null && searchText === users.byId[currentUser].value; + const selectedUser = state.search && state.search.selected; + let searchText; + let isExactMatch; + + if (state.search) { + searchText = state.search.text; + isExactMatch = false; + } else if (currentUser) { + searchText = users.byId[currentUser].value; + isExactMatch = true; + } else { + searchText = ''; + isExactMatch = false; + } return { currentUser, + selectedUser, + searchText, isExactMatch, - selectedUser: state.search.selected, - results: state.search.results, - searchText: state.search.text, }; }; const mapDispatchToProps = dispatch => ({ setUser: user => dispatch(setUserAction(user)), - onInputChange: searchText => dispatch({ + changeInput: searchText => dispatch({ type: 'SEARCH/INPUT_CHANGE', searchText, }), -- cgit v1.1