2
0

index.jsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import React, { Component } from "react";
  2. import { Route, Switch, withRouter } from "react-router-dom";
  3. import PropTypes from "prop-types";
  4. import { connect } from "react-redux";
  5. import { ban, authenticate } from "actions/app";
  6. import Menu from "components/Global/Menu";
  7. import io from "../../io";
  8. import config from "../../../../config/default";
  9. import AuthRoute from "../Auth/AuthRoute";
  10. const asyncComponent = getComponent => {
  11. return class AsyncComponent extends React.Component {
  12. static Component = null;
  13. state = { Component: AsyncComponent.Component };
  14. componentWillMount() {
  15. if (!this.state.Component) {
  16. getComponent().then(Component => { // eslint-disable-line no-shadow
  17. AsyncComponent.Component = Component;
  18. this.setState({ Component });
  19. });
  20. }
  21. }
  22. render() {
  23. const { Component } = this.state; // eslint-disable-line no-shadow
  24. if (Component) return <Component { ...this.props } />;
  25. return null;
  26. }
  27. };
  28. };
  29. @connect()
  30. class App extends Component { // eslint-disable-line react/no-multi-comp
  31. static propTypes = {
  32. dispatch: PropTypes.func,
  33. history: PropTypes.shape({
  34. push: PropTypes.func.isRequired,
  35. }).isRequired,
  36. }
  37. static defaultProps = {
  38. dispatch: () => {},
  39. }
  40. componentDidMount() {
  41. const { dispatch } = this.props;
  42. io.init(config.serverDomain);
  43. io.getSocket(socket => {
  44. socket.on("ready", (loggedIn, role, username, userId) => {
  45. dispatch(authenticate({ loggedIn, role, username, userId }));
  46. });
  47. socket.on("keep.event:banned", reason => dispatch(ban(reason)));
  48. });
  49. if (localStorage.getItem("github_redirect")) {
  50. this.props.history.push(localStorage.getItem("github_redirect"));
  51. localStorage.removeItem("github_redirect");
  52. }
  53. }
  54. render() {
  55. return (
  56. <div>
  57. <Menu />
  58. <div>
  59. <Switch>
  60. <AuthRoute
  61. exact
  62. path="/login"
  63. component={ asyncComponent(() =>
  64. System.import("views/Auth/Login").then(module => module.default)
  65. ) }
  66. authRequired={ false }
  67. />
  68. <AuthRoute
  69. exact
  70. path="/logout"
  71. component={ asyncComponent(() =>
  72. System.import("views/Auth/Logout").then(module => module.default)
  73. ) }
  74. authRequired={ true }
  75. />
  76. <AuthRoute
  77. exact
  78. path="/register"
  79. component={ asyncComponent(() =>
  80. System.import("views/Auth/Register").then(module => module.default)
  81. ) }
  82. authRequired={ false }
  83. />
  84. <AuthRoute
  85. exact
  86. path="/settings"
  87. component={ asyncComponent(() =>
  88. System.import("views/Auth/Settings").then(module => module.default)
  89. ) }
  90. authRequired={ true }
  91. />
  92. <Route
  93. exact
  94. path="/template"
  95. component={ asyncComponent(() =>
  96. System.import("views/Template").then(module => module.default)
  97. ) }
  98. />
  99. <Route
  100. exact
  101. path="/"
  102. component={ asyncComponent(() =>
  103. System.import("views/Home").then(module => module.default)
  104. ) }
  105. />
  106. <Route
  107. path="*"
  108. component={ asyncComponent(() =>
  109. System.import("views/NotFound").then(module => module.default)
  110. ) }
  111. />
  112. </Switch>
  113. </div>
  114. </div>
  115. );
  116. }
  117. }
  118. export default withRouter(App);