|
| 1 | +import * as React from 'react'; |
| 2 | +import OpenInputField from '@/components/forms/OpenInputField'; |
| 3 | +import { RouteComponentProps } from 'react-router'; |
| 4 | +import { parse } from 'query-string'; |
| 5 | +import { Link } from 'react-router-dom'; |
| 6 | +import NetworkErrorMessage from '@/components/NetworkErrorMessage'; |
| 7 | +import performPasswordReset from '@/api/auth/performPasswordReset'; |
| 8 | +import { httpErrorToHuman } from '@/api/http'; |
| 9 | +import { connect } from 'react-redux'; |
| 10 | +import { pushFlashMessage, clearAllFlashMessages } from '@/redux/actions/flash'; |
| 11 | + |
| 12 | +type State = Readonly<{ |
| 13 | + email?: string; |
| 14 | + password?: string; |
| 15 | + passwordConfirm?: string; |
| 16 | + isLoading: boolean; |
| 17 | + errorMessage?: string; |
| 18 | +}>; |
| 19 | + |
| 20 | +type Props = Readonly<RouteComponentProps<{ token: string }> & { |
| 21 | + pushFlashMessage: typeof pushFlashMessage; |
| 22 | + clearAllFlashMessages: typeof clearAllFlashMessages; |
| 23 | +}>; |
| 24 | + |
| 25 | +class ResetPasswordContainer extends React.PureComponent<Props, State> { |
| 26 | + state: State = { |
| 27 | + isLoading: false, |
| 28 | + }; |
| 29 | + |
| 30 | + componentDidMount () { |
| 31 | + const parsed = parse(this.props.location.search); |
| 32 | + |
| 33 | + this.setState({ email: parsed.email as string || undefined }); |
| 34 | + } |
| 35 | + |
| 36 | + canSubmit () { |
| 37 | + if (!this.state.password || !this.state.email) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + return this.state.password.length >= 8 && this.state.password === this.state.passwordConfirm; |
| 42 | + } |
| 43 | + |
| 44 | + onPasswordChange = (e: React.ChangeEvent<HTMLInputElement>) => this.setState({ |
| 45 | + password: e.target.value, |
| 46 | + }); |
| 47 | + |
| 48 | + onPasswordConfirmChange = (e: React.ChangeEvent<HTMLInputElement>) => this.setState({ |
| 49 | + passwordConfirm: e.target.value, |
| 50 | + }); |
| 51 | + |
| 52 | + onSubmit = (e: React.FormEvent<HTMLFormElement>) => { |
| 53 | + e.preventDefault(); |
| 54 | + |
| 55 | + const { password, passwordConfirm, email } = this.state; |
| 56 | + if (!password || !email || !passwordConfirm) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + this.props.clearAllFlashMessages(); |
| 61 | + this.setState({ isLoading: true }, () => { |
| 62 | + performPasswordReset(email, { |
| 63 | + token: this.props.match.params.token, |
| 64 | + password: password, |
| 65 | + passwordConfirmation: passwordConfirm, |
| 66 | + }) |
| 67 | + .then(response => { |
| 68 | + if (response.redirectTo) { |
| 69 | + // @ts-ignore |
| 70 | + window.location = response.redirectTo; |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + this.props.pushFlashMessage({ |
| 75 | + type: 'success', |
| 76 | + message: 'Your password has been reset, please login to continue.', |
| 77 | + }); |
| 78 | + this.props.history.push('/login'); |
| 79 | + }) |
| 80 | + .catch(error => { |
| 81 | + console.error(error); |
| 82 | + this.setState({ errorMessage: httpErrorToHuman(error) }); |
| 83 | + }) |
| 84 | + .then(() => this.setState({ isLoading: false })); |
| 85 | + }); |
| 86 | + }; |
| 87 | + |
| 88 | + render () { |
| 89 | + return ( |
| 90 | + <div> |
| 91 | + <h2 className={'text-center text-neutral-100 font-medium py-4'}> |
| 92 | + Reset Password |
| 93 | + </h2> |
| 94 | + <NetworkErrorMessage message={this.state.errorMessage}/> |
| 95 | + <form className={'login-box'} onSubmit={this.onSubmit}> |
| 96 | + <div className={'mt-3'}> |
| 97 | + <OpenInputField |
| 98 | + label={'Email'} |
| 99 | + value={this.state.email || ''} |
| 100 | + disabled |
| 101 | + /> |
| 102 | + </div> |
| 103 | + <div className={'mt-6'}> |
| 104 | + <OpenInputField |
| 105 | + autoFocus={true} |
| 106 | + label={'New Password'} |
| 107 | + description={'Passwords must be at least 8 characters in length.'} |
| 108 | + type={'password'} |
| 109 | + required={true} |
| 110 | + id={'password'} |
| 111 | + onChange={this.onPasswordChange} |
| 112 | + /> |
| 113 | + </div> |
| 114 | + <div className={'mt-6'}> |
| 115 | + <OpenInputField |
| 116 | + label={'Confirm New Password'} |
| 117 | + type={'password'} |
| 118 | + required={true} |
| 119 | + id={'password-confirm'} |
| 120 | + onChange={this.onPasswordConfirmChange} |
| 121 | + /> |
| 122 | + </div> |
| 123 | + <div className={'mt-6'}> |
| 124 | + <button |
| 125 | + type={'submit'} |
| 126 | + className={'btn btn-primary btn-jumbo'} |
| 127 | + disabled={this.state.isLoading || !this.canSubmit()} |
| 128 | + > |
| 129 | + {this.state.isLoading ? |
| 130 | + <span className={'spinner white'}> </span> |
| 131 | + : |
| 132 | + 'Reset Password' |
| 133 | + } |
| 134 | + </button> |
| 135 | + </div> |
| 136 | + <div className={'mt-6 text-center'}> |
| 137 | + <Link |
| 138 | + to={'/login'} |
| 139 | + className={'text-xs text-neutral-500 tracking-wide no-underline uppercase hover:text-neutral-600'} |
| 140 | + > |
| 141 | + Return to Login |
| 142 | + </Link> |
| 143 | + </div> |
| 144 | + </form> |
| 145 | + </div> |
| 146 | + ); |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +const mapDispatchToProps = { |
| 151 | + pushFlashMessage, |
| 152 | + clearAllFlashMessages, |
| 153 | +}; |
| 154 | + |
| 155 | +export default connect(null, mapDispatchToProps)(ResetPasswordContainer); |
0 commit comments