app.jsx 4.9 KB

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