2
0

index.jsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. socket.on("keep.event:user.session.removed", () => {
  49. location.reload();
  50. // TODO Give user prompt they've been logged out and let them continue.
  51. });
  52. });
  53. if (localStorage.getItem("github_redirect")) {
  54. this.props.history.push(localStorage.getItem("github_redirect"));
  55. localStorage.removeItem("github_redirect");
  56. }
  57. }
  58. render() {
  59. return (
  60. <div>
  61. <Menu />
  62. <div>
  63. <Switch>
  64. <AuthRoute
  65. exact
  66. path="/login"
  67. component={ asyncComponent(() =>
  68. System.import("views/Auth/Login").then(module => module.default)
  69. ) }
  70. authRequired={ false }
  71. />
  72. <AuthRoute
  73. exact
  74. path="/logout"
  75. component={ asyncComponent(() =>
  76. System.import("views/Auth/Logout").then(module => module.default)
  77. ) }
  78. authRequired={ true }
  79. />
  80. <AuthRoute
  81. exact
  82. path="/register"
  83. component={ asyncComponent(() =>
  84. System.import("views/Auth/Register").then(module => module.default)
  85. ) }
  86. authRequired={ false }
  87. />
  88. <AuthRoute
  89. exact
  90. path="/settings"
  91. component={ asyncComponent(() =>
  92. System.import("views/Auth/Settings").then(module => module.default)
  93. ) }
  94. authRequired={ true }
  95. />
  96. <AuthRoute
  97. exact
  98. path="/settings/setpassword"
  99. component={ asyncComponent(() =>
  100. System.import("views/Auth/SetPassword").then(module => module.default)
  101. ) }
  102. authRequired={ true }
  103. />
  104. <AuthRoute
  105. exact
  106. path="/reset_password"
  107. component={ asyncComponent(() =>
  108. System.import("views/Auth/ForgotPassword").then(module => module.default)
  109. ) }
  110. authRequired={ false }
  111. />
  112. <Route
  113. exact
  114. path="/template"
  115. component={ asyncComponent(() =>
  116. System.import("views/Template").then(module => module.default)
  117. ) }
  118. />
  119. <Route
  120. exact
  121. path="/"
  122. component={ asyncComponent(() =>
  123. System.import("views/Home").then(module => module.default)
  124. ) }
  125. />
  126. <Route
  127. path="*"
  128. component={ asyncComponent(() =>
  129. System.import("views/NotFound").then(module => module.default)
  130. ) }
  131. />
  132. </Switch>
  133. </div>
  134. </div>
  135. );
  136. }
  137. }
  138. export default withRouter(App);