Settings.jsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import { connect } from "react-redux";
  4. import CustomInput from "./CustomInput.jsx";
  5. import CustomErrors from "./CustomErrors.jsx";
  6. import io from "../../io";
  7. @connect(state => ({
  8. user: {
  9. userId: state.user.get("userId"),
  10. },
  11. }))
  12. export default class Settings extends Component {
  13. static propTypes = {
  14. user: PropTypes.object,
  15. };
  16. static defaultProps = {
  17. user: {
  18. userId: "",
  19. },
  20. };
  21. constructor(props) {
  22. super(props);
  23. this.state = {
  24. email: "",
  25. username: "",
  26. currentPassword: "",
  27. newPassword: "",
  28. newPasswordAgain: "",
  29. passwordLinked: false,
  30. gitHubLinked: false,
  31. inputInvalid: {
  32. email: true,
  33. username: true,
  34. currentPassword: true,
  35. newPassword: true,
  36. newPasswordAgain: true,
  37. },
  38. errors: [],
  39. };
  40. this.customInputs = {};
  41. io.getSocket(socket => {
  42. socket.emit("users.findBySession", res => {
  43. if (res.status === "success") {
  44. this.setState({
  45. email: res.data.email.address,
  46. username: res.data.username,
  47. passwordLinked: res.data.password,
  48. gitHubLinked: res.data.github,
  49. }, () => {
  50. this.customInputs.email.triggerChangeEvent(true);
  51. this.customInputs.username.triggerChangeEvent(true);
  52. });
  53. } else {
  54. this.state.errors = ["You are currently not logged in."];
  55. }
  56. });
  57. socket.on("event:user.username.changed", username => {
  58. this.setState({
  59. username,
  60. }, () => {
  61. this.customInputs.username.triggerChangeEvent(true);
  62. });
  63. });
  64. // TODO Email changed event?
  65. socket.on("event:user.linkPassword", () => {
  66. this.setState({
  67. passwordLinked: true,
  68. });
  69. });
  70. socket.on("event:user.linkGitHub", () => {
  71. this.setState({
  72. gitHubLinked: true,
  73. });
  74. });
  75. socket.on("event:user.unlinkPassword", () => {
  76. this.setState({
  77. passwordLinked: false,
  78. });
  79. });
  80. socket.on("event:user.unlinkGitHub", () => {
  81. this.setState({
  82. gitHubLinked: false,
  83. });
  84. });
  85. });
  86. }
  87. updateField(field, event) {
  88. this.setState({
  89. [field]: event.target.value,
  90. });
  91. }
  92. /* register() {
  93. if (CustomInput.hasInvalidInput(this.state.inputInvalid)) {
  94. alert("Input invalid. Fix before continuing.");
  95. } else {
  96. this.setState({ errors: [] });
  97. io.getSocs.setState({ errors: [res.message] });
  98. }
  99. });
  100. });
  101. }
  102. } */
  103. /* githubRedirect() {
  104. localStorage.setItem("github_redirect", window.location.pathname);
  105. } */
  106. saveChanges = () => {
  107. if (CustomInput.hasInvalidInput(this.state.inputInvalid, ["username", "email"])) {
  108. alert("Input invalid. Fix before continuing.");
  109. } else {
  110. this.setState({ errors: [] });
  111. io.getSocket(socket => {
  112. socket.emit("users.updateEmail", this.props.user.userId, this.state.email, res => {
  113. if (res.status === "success") {
  114. alert("Success!");
  115. } else {
  116. this.setState({
  117. errors: this.state.errors.concat([res.message]),
  118. });
  119. }
  120. });
  121. socket.emit("users.updateUsername", this.props.user.userId, this.state.username, res => {
  122. if (res.status === "success") {
  123. alert("Success!");
  124. } else {
  125. this.setState({
  126. errors: this.state.errors.concat([res.message]),
  127. });
  128. }
  129. });
  130. });
  131. }
  132. };
  133. validationCallback = CustomInput.validationCallback(this);
  134. render() {
  135. return (
  136. <div>
  137. <CustomErrors errors={ this.state.errors } />
  138. <div>
  139. <h2>General</h2>
  140. <CustomInput label="Email" placeholder="Email" inputType="email" type="email" name="email" value={ this.state.email } customInputEvents={ { onChange: event => this.updateField("email", event) } } validationCallback={ this.validationCallback } onRef={ ref => (this.customInputs.email = ref) } />
  141. <CustomInput label="Username" placeholder="Username" inputType="text" type="username" name="username" value={ this.state.username } customInputEvents={ { onChange: event => this.updateField("username", event) } } validationCallback={ this.validationCallback } onRef={ ref => (this.customInputs.username = ref) } />
  142. <button onClick={ this.saveChanges }>Save changes</button>
  143. </div>
  144. <div>
  145. <h2>Security</h2>
  146. <CustomInput label="Current password" placeholder="Current password" inputType="password" type="password" name="currentPassword" value={ this.state.currentPassword } customInputEvents={ { onChange: event => this.updateField("currentPassword", event) } } validationCallback={ this.validationCallback } />
  147. <CustomInput label="New password" placeholder="New password" inputType="password" type="password" name="newPassword" value={ this.state.newPassword } customInputEvents={ { onChange: event => this.updateField("newPassword", event) } } validationCallback={ this.validationCallback } />
  148. <CustomInput label="New password again" placeholder="New password again" inputType="password" type="password" name="newPasswordAgain" value={ this.state.newPasswordAgain } customInputEvents={ { onChange: event => this.updateField("newPasswordAgain", event) } } validationCallback={ this.validationCallback } />
  149. <button>Change password</button>
  150. <button>Link GitHub account</button>
  151. <button>Log out everywhere</button>
  152. </div>
  153. </div>
  154. );
  155. }
  156. }