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/AppRouter.js | 55 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/client/react/AppRouter.js (limited to 'src/client/react/AppRouter.js') diff --git a/src/client/react/AppRouter.js b/src/client/react/AppRouter.js new file mode 100644 index 0000000..ae637f0 --- /dev/null +++ b/src/client/react/AppRouter.js @@ -0,0 +1,55 @@ +import React from 'react'; + +import { + withRouter, + Route, + Switch, + Redirect, +} from 'react-router-dom'; +import { connect } from 'react-redux'; +import { PropTypes } from 'prop-types'; + +import Index from './components/page/Index'; +import User from './components/page/User'; +import { setUser } from './store/actions'; +import { userFromLocation } from './lib/url'; + +class AppRouter extends React.Component { + static propTypes = { + user: PropTypes.string, + resetUserState: PropTypes.func.isRequired, + } + + static defaultProps = { + user: null, + } + + componentDidMount() { + const { user, resetUserState } = this.props; + + resetUserState(user); + } + + render() { + return ( + + + + + + ); + } +} + +const mapStateToProps = (state, { location }) => { + return { + key: location.pathname, + user: userFromLocation(location), + }; +}; + +const mapDispatchToProps = dispatch => ({ + resetUserState: user => dispatch(setUser(user)), +}); + +export default withRouter(connect(mapStateToProps, mapDispatchToProps)(AppRouter)); -- cgit v1.1