import React from 'react' import PropTypes from 'prop-types' import { Text, Column, BoundInput, BoundButton } from 'ui' import { MessageModal, WaitModal } from '../Modal' import { api } from '../helpers' import { FormBinder } from 'react-form-binder' export class ResetPassword extends React.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.getField('newPassword').value) }, submit: { nonValue: true, isDisabled: (r) => (!r.anyModified && !r.allValid) } } constructor(props) { super(props) this.state = { binder: new FormBinder({}, ResetPassword.bindings), messageModal: null, waitModal: null } this.handleSubmit = this.handleSubmit.bind(this) this.handleMessageModalDismiss = this.handleMessageModalDismiss.bind(this) } handleSubmit() { const obj = this.state.binder.getValues() 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 }) this.props.history.replace('/login') }).catch((error) => { this.setState({ binder: new FormBinder({}, ResetPassword.bindings), // Reset to avoid accidental rapid retries waitModal: null, messageModal: { title: 'We had a problem changing your password', message: error.message } }) }) } handleMessageModalDismiss() { this.setState({ messageModal: null }) } render() { return (