import React, { Component } from "react"; import PropTypes from "prop-types"; import { connect } from "react-redux"; import CustomInput from "./CustomInput.jsx"; import CustomErrors from "./CustomErrors.jsx"; import io from "../../io"; @connect(state => ({ user: { userId: state.user.get("userId"), }, })) export default class Settings extends Component { static propTypes = { user: PropTypes.object, }; static defaultProps = { user: { userId: "", }, }; constructor(props) { super(props); CustomInput.initialize(this); this.state = { step: 1, resetCode: "", }; } getActions = () => { const emailInput = (this.input.email = ref) } />; const requestResetCodeButton = (); const iAlreadyHaveAResetCodeButton = (); const resetCodeInput = (this.input.email = ref) } />; const verifyResetCode = (); const newPasswordInput = (this.input.newPassword = ref) } />; const newPasswordAgainInput = (this.input.newPasswordAgain = ref) } />; const changePassword = (); if (this.state.step === 1) { return [emailInput, requestResetCodeButton, iAlreadyHaveAResetCodeButton]; } else if (this.state.step === 2) { return [resetCodeInput, verifyResetCode]; } return [newPasswordInput, newPasswordAgainInput, changePassword]; }; requestResetCode = () => { if (CustomInput.hasInvalidInput(this.input, ["email"])) { this.errors.clearAddError("Some fields are incorrect. Please fix them before continuing."); } else { this.errors.clearErrors(); io.getSocket(socket => { socket.emit("users.requestPasswordReset", this.input.email.getValue(), res => { if (res.status === "success") { alert("Success!"); this.setState({ step: 2, }); } else { this.errors.addError(res.message); } }); }); } }; verifyResetCode = () => { if (CustomInput.hasInvalidInput(this.input, ["resetCode"])) { this.errors.clearAddError("Some fields are incorrect. Please fix them before continuing."); } else { this.errors.clearErrors(); io.getSocket(socket => { socket.emit("users.verifyPasswordResetCode", this.input.resetCode.getValue(), res => { if (res.status === "success") { alert("Success!"); this.setState({ step: 3, resetCode: this.input.resetCode.getValue(), }); } else { this.errors.addError(res.message); } }); }); } }; changePassword = () => { if (CustomInput.hasInvalidInput(this.input, ["newPassword", "newPasswordAgain"])) { this.errors.clearAddError("Some fields are incorrect. Please fix them before continuing."); } else if (CustomInput.isTheSame(this.input, ["newPassword", "newPasswordAgain"])) { this.errors.clearAddError("New password and new password again need to be the same."); } else { this.errors.clearErrors(); io.getSocket(socket => { socket.emit("users.changePasswordWithResetCode", this.state.resetCode, this.input.newPassword.getValue(), res => { if (res.status === "success") { alert("Success!"); location.href = "/login"; } else { this.errors.addError(res.message); } }); }); } }; skipRequestResetCode = () => { this.setState({ step: 2, }); }; render() { return (

Reset password

1 2 3
(this.errors = ref) } /> { this.getActions() }
); } }