index.jsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import React, { Component } from "react";
  2. import async from "async";
  3. import PropTypes from "prop-types";
  4. import { connect } from "react-redux";
  5. import { NavLink } from "react-router-dom";
  6. import { translate } from "react-i18next";
  7. import config from "config";
  8. import CustomInput from "components/CustomInput.jsx";
  9. import CustomMessages from "components/CustomMessages.jsx";
  10. import io from "io";
  11. @connect(state => ({
  12. user: {
  13. userId: state.session.get("userId"),
  14. },
  15. }))
  16. @translate(["settings"], { wait: true })
  17. export default class Settings extends Component {
  18. static propTypes = {
  19. user: PropTypes.object,
  20. t: PropTypes.func,
  21. };
  22. static defaultProps = {
  23. user: {
  24. userId: "",
  25. },
  26. t: () => {},
  27. };
  28. constructor(props) {
  29. super(props);
  30. CustomInput.initialize(this);
  31. this.state = {
  32. passwordLinked: false,
  33. gitHubLinked: false,
  34. };
  35. io.getSocket(socket => {
  36. socket.emit("users.findBySession", res => {
  37. if (res.status === "success") {
  38. this.input.email.setValue(res.data.email.address, true);
  39. this.input.username.setValue(res.data.username, true);
  40. this.setState({
  41. passwordLinked: res.data.password,
  42. gitHubLinked: res.data.github,
  43. });
  44. } else {
  45. this.messages.addError(this.props.t("general:notLoggedInError"));
  46. }
  47. });
  48. socket.on("event:user.username.changed", username => {
  49. this.input.username.setValue(username, true);
  50. });
  51. // TODO Email changed event?
  52. socket.on("event:user.linkPassword", () => {
  53. this.setState({
  54. passwordLinked: true,
  55. });
  56. });
  57. socket.on("event:user.linkGitHub", () => {
  58. this.setState({
  59. gitHubLinked: true,
  60. });
  61. });
  62. socket.on("event:user.unlinkPassword", () => {
  63. this.setState({
  64. passwordLinked: false,
  65. });
  66. });
  67. socket.on("event:user.unlinkGitHub", () => {
  68. this.setState({
  69. gitHubLinked: false,
  70. });
  71. });
  72. });
  73. }
  74. /* githubRedirect() {
  75. localStorage.setItem("github_redirect", window.location.pathname);
  76. } */
  77. saveChanges = () => {
  78. this.messages.clearErrorSuccess();
  79. async.waterfall([
  80. (next) => {
  81. if (this.input.username.isPristine()) this.input.username.validate(next);
  82. else next();
  83. },
  84. (next) => {
  85. if (this.input.email.isPristine()) this.input.email.validate(next);
  86. else next();
  87. },
  88. ], () => {
  89. if (CustomInput.hasInvalidInput(this.input, ["username", "email"])) {
  90. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  91. } else if (this.input.username.isOriginal() && this.input.email.isOriginal()) {
  92. this.messages.clearAddError(this.props.t("settings:usernameOrEmailHasntChanged"));
  93. } else {
  94. const email = this.input.email.getValue();
  95. const username = this.input.username.getValue();
  96. io.getSocket(socket => {
  97. if (!this.input.email.isOriginal()) {
  98. socket.emit("users.updateEmail", this.props.user.userId, email, res => {
  99. if (res.status === "success") {
  100. this.messages.clearAddSuccess(this.props.t("settings:successfullyUpdatedEmail"));
  101. } else {
  102. this.messages.addError(res.message);
  103. }
  104. });
  105. }
  106. if (!this.input.username.isOriginal()) {
  107. socket.emit("users.updateUsername", this.props.user.userId, username, res => {
  108. if (res.status === "success") {
  109. this.messages.clearAddSuccess(this.props.t("settings:successfullyUpdatedUsername"));
  110. } else {
  111. this.messages.addError(res.message);
  112. }
  113. });
  114. }
  115. });
  116. }
  117. });
  118. };
  119. changePassword = () => {
  120. this.messages.clearErrorSuccess();
  121. if (CustomInput.hasInvalidInput(this.input, ["newPassword"])) {
  122. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  123. } else if (!this.state.passwordLinked) {
  124. this.messages.clearAddError("You don't have a password set.");
  125. } else {
  126. io.getSocket(socket => {
  127. socket.emit("users.updatePassword", this.input.newPassword.getValue(), res => {
  128. if (res.status === "success") {
  129. this.messages.clearAddSuccess("Successfully changed password.");
  130. } else {
  131. this.messages.addError(res.message);
  132. }
  133. });
  134. });
  135. }
  136. };
  137. logOutEverywhere = () => {
  138. this.messages.clearErrorSuccess();
  139. io.getSocket(socket => {
  140. socket.emit("users.removeSessions", this.props.user.userId, res => {
  141. if (res.status === "success") {
  142. this.messages.clearAddSuccess(this.props.t("settings:successfullyLoggedOutEverywhere"));
  143. } else {
  144. this.messages.addError(res.message);
  145. }
  146. });
  147. });
  148. };
  149. unlinkGitHub = () => {
  150. this.messages.clearErrorSuccess();
  151. io.getSocket(socket => {
  152. socket.emit("users.unlinkGitHub", res => {
  153. if (res.status === "success") {
  154. this.messages.clearAddSuccess(this.props.t("settings:successfullyUnlinkedGitHub"));
  155. } else {
  156. this.messages.addError(res.message);
  157. }
  158. });
  159. });
  160. };
  161. unlinkPassword = () => {
  162. this.messages.clearErrorSuccess();
  163. io.getSocket(socket => {
  164. socket.emit("users.unlinkPassword", res => {
  165. if (res.status === "success") {
  166. this.messages.clearAddSuccess(this.props.t("settings:successfullyUnlinkedPassword"));
  167. } else {
  168. this.messages.addError(res.message);
  169. }
  170. });
  171. });
  172. };
  173. linkButtons = () => {
  174. const newPassword = <CustomInput key="newPassword" type="password" name="newPassword" label={ this.props.t("general:newPasswordInput") } placeholder={ this.props.t("general:newPasswordInput") } onRef={ ref => (this.input.newPassword = ref) } />;
  175. const changePasswordButton = <button key="changePassword" onClick={ this.changePassword }>{ this.props.t("settings:changePassword") }</button>;
  176. const linkPassword = <NavLink key="linkPassword" className="button" to="/settings/setpassword" >{ this.props.t("settings:addAPasswordToAccount") }</NavLink>;
  177. const linkGitHub = <a key="linkGitHub" className="gray-button" href={ config.serverDomain + "/auth/github/link" }>{ this.props.t("settings:linkGitHubToAccount") }</a>;
  178. const unlinkGitHub = (
  179. <button key="unlinkGitHub" className="red-button" onClick={ this.unlinkGitHub }>
  180. { this.props.t("settings:removeLoggingInWithGitHub") }
  181. </button>
  182. );
  183. const unlinkPassword = (
  184. <button key="unlinkPassword" className="red-button" onClick={ this.unlinkPassword }>
  185. { this.props.t("settings:removeLoggingInWithPassword") }
  186. </button>
  187. );
  188. const toReturn = [];
  189. if (this.state.passwordLinked) {
  190. toReturn.push(newPassword);
  191. toReturn.push(changePasswordButton);
  192. }
  193. if (this.state.passwordLinked && this.state.gitHubLinked) {
  194. toReturn.push(unlinkGitHub);
  195. toReturn.push(unlinkPassword);
  196. } else if (!this.state.passwordLinked) {
  197. toReturn.push(linkPassword);
  198. } else toReturn.push(linkGitHub);
  199. return toReturn;
  200. };
  201. render() {
  202. const { t } = this.props;
  203. return (
  204. <main id="settings">
  205. <h1>{ t("settings:title") }</h1>
  206. <CustomMessages onRef={ ref => (this.messages = ref) } />
  207. <div className="sections">
  208. <div className="section general-section">
  209. <h2>{ this.props.t("settings:general") }</h2>
  210. <CustomInput type="email" name="email" label={ this.props.t("general:emailInput") } placeholder={ this.props.t("general:emailInput") } onRef={ ref => (this.input.email = ref) } />
  211. <CustomInput type="username" name="username" label={ this.props.t("general:usernameInput") } placeholder={ this.props.t("general:usernameInput") } onRef={ ref => (this.input.username = ref) } />
  212. <button onClick={ this.saveChanges }>{ this.props.t("settings:saveChanges") }</button>
  213. </div>
  214. <div className="section security-section">
  215. <h2>{ this.props.t("settings:security") }</h2>
  216. { this.linkButtons() }
  217. <button className="red-button" onClick={ this.logOutEverywhere }>{ this.props.t("settings:logOutEverywhere") }</button>
  218. </div>
  219. </div>
  220. </main>
  221. );
  222. }
  223. }