123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- import React, { Component } from "react";
- import { Route, Switch, withRouter } from "react-router-dom";
- import PropTypes from "prop-types";
- import { connect } from "react-redux";
- import { ban, authenticate } from "actions/app";
- import Menu from "components/Global/Menu";
- import io from "../../io";
- import config from "../../../../config/default";
- import AuthRoute from "../Auth/AuthRoute";
- const asyncComponent = getComponent => {
- return class AsyncComponent extends React.Component {
- static Component = null;
- state = { Component: AsyncComponent.Component };
- componentWillMount() {
- if (!this.state.Component) {
- getComponent().then(Component => { // eslint-disable-line no-shadow
- AsyncComponent.Component = Component;
- this.setState({ Component });
- });
- }
- }
- render() {
- const { Component } = this.state; // eslint-disable-line no-shadow
- if (Component) return <Component { ...this.props } />;
- return null;
- }
- };
- };
- @connect()
- class App extends Component { // eslint-disable-line react/no-multi-comp
- static propTypes = {
- dispatch: PropTypes.func,
- history: PropTypes.shape({
- push: PropTypes.func.isRequired,
- }).isRequired,
- }
- static defaultProps = {
- dispatch: () => {},
- }
- componentDidMount() {
- const { dispatch } = this.props;
- io.init(config.serverDomain);
- io.getSocket(socket => {
- socket.on("ready", (loggedIn, role, username, userId) => {
- dispatch(authenticate({ loggedIn, role, username, userId }));
- });
- socket.on("keep.event:banned", reason => dispatch(ban(reason)));
- });
- if (localStorage.getItem("github_redirect")) {
- this.props.history.push(localStorage.getItem("github_redirect"));
- localStorage.removeItem("github_redirect");
- }
- }
- render() {
- return (
- <div>
- <Menu />
- <div>
- <Switch>
- <AuthRoute
- exact
- path="/login"
- component={ asyncComponent(() =>
- System.import("views/Auth/Login").then(module => module.default)
- ) }
- authRequired={ false }
- />
- <AuthRoute
- exact
- path="/logout"
- component={ asyncComponent(() =>
- System.import("views/Auth/Logout").then(module => module.default)
- ) }
- authRequired={ true }
- />
- <AuthRoute
- exact
- path="/register"
- component={ asyncComponent(() =>
- System.import("views/Auth/Register").then(module => module.default)
- ) }
- authRequired={ false }
- />
- <AuthRoute
- exact
- path="/settings"
- component={ asyncComponent(() =>
- System.import("views/Auth/Settings").then(module => module.default)
- ) }
- authRequired={ true }
- />
- <Route
- exact
- path="/template"
- component={ asyncComponent(() =>
- System.import("views/Template").then(module => module.default)
- ) }
- />
- <Route
- exact
- path="/"
- component={ asyncComponent(() =>
- System.import("views/Home").then(module => module.default)
- ) }
- />
- <Route
- path="*"
- component={ asyncComponent(() =>
- System.import("views/NotFound").then(module => module.default)
- ) }
- />
- </Switch>
- </div>
- </div>
- );
- }
- }
- export default withRouter(App);
|