index.jsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import { connect } from "react-redux";
  4. import CustomInput from "components/CustomInput.jsx";
  5. import CustomErrors from "components/CustomMessages.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. CustomInput.initialize(this);
  24. this.state = {
  25. step: 1,
  26. resetCode: "",
  27. };
  28. }
  29. getActions = () => {
  30. const emailInput = <CustomInput key="email" type="email" name="email" label="Email" placeholder="Email" onRef={ ref => (this.input.email = ref) } />;
  31. const requestResetCodeButton = (<button key="requestResetCode" onClick={ this.requestResetCode }>
  32. Request reset code
  33. </button>);
  34. const iAlreadyHaveAResetCodeButton = (<button key="skipRequestResetCode" onClick={ this.skipRequestResetCode }>
  35. I already have a reset code
  36. </button>);
  37. const resetCodeInput = <CustomInput key="resetCode" type="uniqueCode" name="resetCode" label="Reset code" placeholder="Reset code" onRef={ ref => (this.input.resetCode = ref) } />;
  38. const verifyResetCode = (<button key="verifyResetCode" onClick={ this.verifyResetCode }>
  39. Verify reset code
  40. </button>);
  41. const newPasswordInput = <CustomInput key="newPassword" type="password" name="newPassword" label="New password" placeholder="New password" onRef={ ref => (this.input.newPassword = ref) } />;
  42. const newPasswordAgainInput = <CustomInput key="newPasswordAgain" type="password" name="newPasswordAgain" label="New password again" placeholder="New password again" onRef={ ref => (this.input.newPasswordAgain = ref) } />;
  43. const changePassword = (<button key="changePassword" onClick={ this.changePassword }>
  44. Change password
  45. </button>);
  46. if (this.state.step === 1) {
  47. return [emailInput, requestResetCodeButton, iAlreadyHaveAResetCodeButton];
  48. } else if (this.state.step === 2) {
  49. return [resetCodeInput, verifyResetCode];
  50. } return [newPasswordInput, newPasswordAgainInput, changePassword];
  51. };
  52. requestResetCode = () => {
  53. if (CustomInput.hasInvalidInput(this.input, ["email"])) {
  54. this.messages.clearAddError("Some fields are incorrect. Please fix them before continuing.");
  55. } else {
  56. this.messages.clearAll();
  57. io.getSocket(socket => {
  58. socket.emit("users.requestPasswordReset", this.input.email.getValue(), res => {
  59. if (res.status === "success") {
  60. this.messages.clearAddSuccess("Successfully requested reset code.");
  61. this.messages.clearAddInfo("We have sent a unique reset code to your email address.");
  62. this.setState({
  63. step: 2,
  64. });
  65. } else {
  66. this.messages.addError(res.message);
  67. }
  68. });
  69. });
  70. }
  71. };
  72. verifyResetCode = () => {
  73. if (CustomInput.hasInvalidInput(this.input, ["resetCode"])) {
  74. this.messages.clearAddError("Some fields are incorrect. Please fix them before continuing.");
  75. } else {
  76. this.messages.clearErrors();
  77. io.getSocket(socket => {
  78. socket.emit("users.verifyPasswordResetCode", this.input.resetCode.getValue(), res => {
  79. if (res.status === "success") {
  80. this.messages.clearAddSuccess("Successfully verified reset code.");
  81. this.setState({
  82. step: 3,
  83. resetCode: this.input.resetCode.getValue(),
  84. });
  85. } else {
  86. this.messages.addError(res.message);
  87. }
  88. });
  89. });
  90. }
  91. };
  92. changePassword = () => {
  93. this.messages.clearErrorSuccess();
  94. if (CustomInput.hasInvalidInput(this.input, ["newPassword", "newPasswordAgain"])) {
  95. this.messages.clearAddError("Some fields are incorrect. Please fix them before continuing.");
  96. } else if (CustomInput.isTheSame(this.input, ["newPassword", "newPasswordAgain"])) {
  97. this.messages.clearAddError("New password and new password again need to be the same.");
  98. } else {
  99. io.getSocket(socket => {
  100. socket.emit("users.changePasswordWithResetCode", this.state.resetCode, this.input.newPassword.getValue(), res => {
  101. if (res.status === "success") {
  102. this.messages.clearAddSuccess("Successfully changed password. Redirecting you to the login page.");
  103. // TODO Maybe add 5s delay and replace location.href everywhere
  104. location.href = "/login";
  105. } else {
  106. this.messages.addError(res.message);
  107. }
  108. });
  109. });
  110. }
  111. };
  112. skipRequestResetCode = () => {
  113. this.setState({
  114. step: 2,
  115. });
  116. };
  117. render() {
  118. return (
  119. <div>
  120. <h1>Reset password</h1>
  121. <div className="steps">
  122. <span className={ `step-circle-1 ${ this.state.step === 1 ? "step-circle-active" : "" }` }>1</span>
  123. <span className="step-line-1" />
  124. <span className={ `step-circle-2 ${ this.state.step === 2 ? "step-circle-active" : "" }` }>2</span>
  125. <span className="step-line-2" />
  126. <span className={ `step-circle-3 ${ this.state.step === 3 ? "step-circle-active" : "" }` }>3</span>
  127. </div>
  128. <CustomErrors onRef={ ref => (this.messages = ref) } />
  129. { this.getActions() }
  130. </div>
  131. );
  132. }
  133. }