app.jsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 { translate } from "react-i18next";
  6. import { ban, authenticate } from "actions/auth";
  7. import Navbar from "components/Global/Navbar";
  8. import config from "config";
  9. import AuthRoute from "components/AuthRoute";
  10. import io from "./io";
  11. const asyncComponent = getComponent => {
  12. return class AsyncComponent extends React.Component {
  13. static Component = null;
  14. state = { Component: AsyncComponent.Component };
  15. componentWillMount() {
  16. if (!this.state.Component) {
  17. getComponent().then(Component => { // eslint-disable-line no-shadow
  18. AsyncComponent.Component = Component;
  19. this.setState({ Component });
  20. });
  21. }
  22. }
  23. render() {
  24. const { Component } = this.state; // eslint-disable-line no-shadow
  25. if (Component) return <Component { ...this.props } />;
  26. return null;
  27. }
  28. };
  29. };
  30. @connect()
  31. @translate(["pages"], { wait: false })
  32. class App extends Component { // eslint-disable-line react/no-multi-comp
  33. static propTypes = {
  34. dispatch: PropTypes.func,
  35. t: PropTypes.func,
  36. };
  37. static defaultProps = {
  38. dispatch: () => {},
  39. t: () => {},
  40. };
  41. componentDidMount() {
  42. const { dispatch } = this.props;
  43. io.init(config.serverDomain);
  44. io.getSocket(socket => {
  45. socket.on("ready", (loggedIn, role, username, userId) => {
  46. dispatch(authenticate({ loggedIn, role, username, userId }));
  47. });
  48. socket.on("keep.event:banned", reason => dispatch(ban(reason)));
  49. socket.on("keep.event:user.session.removed", () => {
  50. location.reload();
  51. // TODO Give user prompt they've been logged out and let them continue.
  52. });
  53. });
  54. if (localStorage.getItem("github_redirect")) {
  55. // TODO
  56. localStorage.removeItem("github_redirect");
  57. }
  58. }
  59. render() {
  60. const { t } = this.props;
  61. return (
  62. <div>
  63. <Navbar />
  64. <Switch>
  65. <AuthRoute
  66. exact
  67. path="/login"
  68. component={ asyncComponent(() =>
  69. System.import("views/Auth/Login").then(module => module.default)
  70. ) }
  71. auth="disallowed"
  72. title={ t("pages:login") }
  73. />
  74. <AuthRoute
  75. exact
  76. path="/logout"
  77. component={ asyncComponent(() =>
  78. System.import("views/Auth/Logout").then(module => module.default)
  79. ) }
  80. auth="required"
  81. title="Logout"
  82. />
  83. <AuthRoute
  84. exact
  85. path="/register"
  86. component={ asyncComponent(() =>
  87. System.import("views/Auth/Register").then(module => module.default)
  88. ) }
  89. auth="disallowed"
  90. title={ t("pages:register") }
  91. />
  92. <AuthRoute
  93. exact
  94. path="/settings"
  95. component={ asyncComponent(() =>
  96. System.import("views/Auth/Settings").then(module => module.default)
  97. ) }
  98. auth="required"
  99. title={ t("pages:settings") }
  100. />
  101. <AuthRoute
  102. exact
  103. path="/settings/setpassword"
  104. component={ asyncComponent(() =>
  105. System.import("views/Auth/Settings/SetPassword").then(module => module.default)
  106. ) }
  107. auth="required"
  108. title={ t("pages:setPassword") }
  109. />
  110. <AuthRoute
  111. exact
  112. path="/reset_password"
  113. component={ asyncComponent(() =>
  114. System.import("views/Auth/ForgotPassword").then(module => module.default)
  115. ) }
  116. auth="disallowed"
  117. title={ t("pages:resetPassword") }
  118. />
  119. <AuthRoute
  120. path="/terms"
  121. component={ asyncComponent(() =>
  122. System.import("views/Terms").then(module => module.default)
  123. ) }
  124. auth="ignored"
  125. title={ t("pages:terms") }
  126. />
  127. <AuthRoute
  128. path="/privacy"
  129. component={ asyncComponent(() =>
  130. System.import("views/Privacy").then(module => module.default)
  131. ) }
  132. auth="ignored"
  133. title={ t("pages:privacy") }
  134. />
  135. <AuthRoute
  136. exact
  137. path="/"
  138. component={ asyncComponent(() =>
  139. System.import("views/Home").then(module => module.default)
  140. ) }
  141. auth="ignored"
  142. title={ t("pages:homepage") }
  143. />
  144. <AuthRoute
  145. path="*"
  146. component={ asyncComponent(() =>
  147. System.import("views/Errors/Error404").then(module => module.default)
  148. ) }
  149. auth="ignored"
  150. title={ t("pages:error404") }
  151. />
  152. </Switch>
  153. </div>
  154. );
  155. }
  156. }
  157. export default withRouter(App);