2
0

index.jsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import React, { Component } from "react";
  2. import { Route, Switch } 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. const asyncComponent = getComponent => {
  10. return class AsyncComponent extends React.Component {
  11. static Component = null;
  12. state = { Component: AsyncComponent.Component };
  13. componentWillMount() {
  14. if (!this.state.Component) {
  15. getComponent().then(Component => { // eslint-disable-line no-shadow
  16. AsyncComponent.Component = Component;
  17. this.setState({ Component });
  18. });
  19. }
  20. }
  21. render() {
  22. const { Component } = this.state; // eslint-disable-line no-shadow
  23. if (Component) return <Component { ...this.props } />;
  24. return null;
  25. }
  26. };
  27. };
  28. @connect()
  29. export default class App extends Component { // eslint-disable-line react/no-multi-comp
  30. static propTypes = {
  31. dispatch: PropTypes.func,
  32. }
  33. static defaultProps = {
  34. dispatch: () => {},
  35. }
  36. componentDidMount() {
  37. const { dispatch } = this.props;
  38. io.init(config.serverDomain);
  39. io.getSocket(socket => {
  40. socket.on("ready", (status, role, username, userId) => {
  41. dispatch(authenticate({ status, role, username, userId }));
  42. });
  43. socket.on("keep.event:banned", reason => dispatch(ban(reason)));
  44. });
  45. }
  46. render() {
  47. return (
  48. <div>
  49. <Menu />
  50. <div>
  51. <Switch>
  52. <Route
  53. exact
  54. path="/login"
  55. component={ asyncComponent(() =>
  56. System.import("views/Auth/Login").then(module => module.default)
  57. ) }
  58. />
  59. <Route
  60. exact
  61. path="/logout"
  62. component={ asyncComponent(() =>
  63. System.import("views/Auth/Logout").then(module => module.default)
  64. ) }
  65. />
  66. <Route
  67. exact
  68. path="/register"
  69. component={ asyncComponent(() =>
  70. System.import("views/Auth/Register").then(module => module.default)
  71. ) }
  72. />
  73. <Route
  74. exact
  75. path="/template"
  76. component={ asyncComponent(() =>
  77. System.import("views/Template").then(module => module.default)
  78. ) }
  79. />
  80. <Route
  81. exact
  82. path="/"
  83. component={ asyncComponent(() =>
  84. System.import("views/Home").then(module => module.default)
  85. ) }
  86. />
  87. <Route
  88. path="*"
  89. component={ asyncComponent(() =>
  90. System.import("views/NotFound").then(module => module.default)
  91. ) }
  92. />
  93. </Switch>
  94. </div>
  95. </div>
  96. );
  97. }
  98. }