aboutsummaryrefslogtreecommitdiff
path: root/src/client/react/AppRouter.js
blob: ae637f084410268fac3050c4d7d5f6948a2c8db6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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 (
      <Switch>
        <Route exact path="/" component={Index} />
        <Route path="/:type/:value" component={User} />
        <Redirect to="/" />
      </Switch>
    );
  }
}

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));