import React, { Component, Fragment } from "react" import PropTypes from "prop-types" import { Box, Text, Image, Column, Row, BoundInput, BoundButton } from "ui" import { MessageModal, WaitModal } from "../Modal" import { api } from "src/API" import { FormBinder } from "react-form-binder" import { sizeInfo, colorInfo } from "ui/style" import autobind from "autobind-decorator" import headerLogo from "images/logo.png" export class ResetPassword extends Component { static propTypes = { history: PropTypes.oneOfType([PropTypes.array, PropTypes.object]), } static bindings = { newPassword: { alwaysGet: true, isValid: (r, v) => v.length >= 6, }, reenteredNewPassword: { isValid: (r, v) => v !== "" && v === r.getBindingValue("newPassword"), }, submit: { noValue: true, isDisabled: (r) => !r.anyModified || !r.allValid, }, } constructor(props) { super(props) this.state = { binder: new FormBinder({}, ResetPassword.bindings), messageModal: null, waitModal: null, tokenConfirmed: false, } } componentDidMount(props) { const passwordToken = new URLSearchParams( decodeURIComponent(window.location.search) ).get("password-token") this.setState({ waitModal: { message: "Confirming password reset..." } }) if (passwordToken) { api .logout() .then(() => { return api.confirmResetPassword(passwordToken) }) .then((response) => { this.setState({ waitModal: null }) }) .catch((err) => { this.setState({ waitModal: null, messageModal: { icon: "hand", message: `We were unable to confirm you requested a password reset. Please request another reset.`, detail: err.message, }, }) }) } else { this.props.history.replace("/") } } @autobind handleSubmit(e) { e.preventDefault() e.stopPropagation() const obj = this.state.binder.getModifiedBindingValues() const passwordToken = new URLSearchParams( decodeURIComponent(window.location.search) ).get("password-token") this.setState({ waitModal: { message: "Setting Password..." } }) api .resetPassword({ newPassword: obj.newPassword, passwordToken }) .then(() => { this.setState({ waitModal: null, messageModal: { icon: "thumb", message: `Your password has been reset. You can now login to the system with your new password.`, }, }) }) .catch((err) => { this.setState({ binder: new FormBinder({}, ResetPassword.bindings), // Reset to avoid accidental rapid retries waitModal: null, messageModal: { icon: "hand", message: "There was a problem changing your password. Please request another reset email.", detail: err.message, }, }) }) } @autobind handleMessageModalDismiss() { const { redirect } = this.state.messageModal this.setState({ messageModal: null }) this.props.history.replace(redirect || "/login") } render() { const { messageModal, waitModal, binder } = this.state return (
Reset Password
) } }