app.jsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/auth";
  6. import Navbar from "components/Global/Navbar";
  7. import config from "config";
  8. import AuthRoute from "components/AuthRoute";
  9. import io from "./io";
  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. };
  34. static defaultProps = {
  35. dispatch: () => {},
  36. };
  37. componentDidMount() {
  38. const { dispatch } = this.props;
  39. io.init(config.serverDomain);
  40. io.getSocket(socket => {
  41. socket.on("ready", (loggedIn, role, username, userId) => {
  42. dispatch(authenticate({ loggedIn, role, username, userId }));
  43. });
  44. socket.on("keep.event:banned", reason => dispatch(ban(reason)));
  45. socket.on("keep.event:user.session.removed", () => {
  46. location.reload();
  47. // TODO Give user prompt they've been logged out and let them continue.
  48. });
  49. });
  50. if (localStorage.getItem("github_redirect")) {
  51. // TODO
  52. localStorage.removeItem("github_redirect");
  53. }
  54. }
  55. render() {
  56. return (
  57. <div>
  58. <Navbar />
  59. <Switch>
  60. <AuthRoute
  61. exact
  62. path="/login"
  63. component={ asyncComponent(() =>
  64. System.import("views/Auth/Login").then(module => module.default)
  65. ) }
  66. auth="disallowed"
  67. title="Login"
  68. />
  69. <AuthRoute
  70. exact
  71. path="/logout"
  72. component={ asyncComponent(() =>
  73. System.import("views/Auth/Logout").then(module => module.default)
  74. ) }
  75. auth="required"
  76. title="Logout"
  77. />
  78. <AuthRoute
  79. exact
  80. path="/register"
  81. component={ asyncComponent(() =>
  82. System.import("views/Auth/Register").then(module => module.default)
  83. ) }
  84. auth="disallowed"
  85. title="Register"
  86. />
  87. <AuthRoute
  88. exact
  89. path="/settings"
  90. component={ asyncComponent(() =>
  91. System.import("views/Auth/Settings").then(module => module.default)
  92. ) }
  93. auth="required"
  94. title="Settings"
  95. />
  96. <AuthRoute
  97. exact
  98. path="/settings/setpassword"
  99. component={ asyncComponent(() =>
  100. System.import("views/Auth/Settings/SetPassword").then(module => module.default)
  101. ) }
  102. auth="required"
  103. title="Set password"
  104. />
  105. <AuthRoute
  106. exact
  107. path="/reset_password"
  108. component={ asyncComponent(() =>
  109. System.import("views/Auth/ForgotPassword").then(module => module.default)
  110. ) }
  111. auth="disallowed"
  112. title="Reset password"
  113. />
  114. <AuthRoute
  115. exact
  116. path="/"
  117. component={ asyncComponent(() =>
  118. System.import("views/Home").then(module => module.default)
  119. ) }
  120. auth="ignored"
  121. title="Homepage"
  122. />
  123. <AuthRoute
  124. path="*"
  125. component={ asyncComponent(() =>
  126. System.import("views/Errors/Error404").then(module => module.default)
  127. ) }
  128. auth="ignored"
  129. title="404"
  130. />
  131. </Switch>
  132. </div>
  133. );
  134. }
  135. }
  136. export default withRouter(App);