import * as React from 'react'; import { RouteComponentProps } from 'react-router'; import { parse } from 'query-string'; import { Link } from 'react-router-dom'; import NetworkErrorMessage from '@/components/NetworkErrorMessage'; import performPasswordReset from '@/api/auth/performPasswordReset'; import { httpErrorToHuman } from '@/api/http'; import { connect } from 'react-redux'; import { pushFlashMessage, clearAllFlashMessages } from '@/redux/actions/flash'; import LoginFormContainer from '@/components/auth/LoginFormContainer'; type State = Readonly<{ email?: string; password?: string; passwordConfirm?: string; isLoading: boolean; errorMessage?: string; }>; type Props = Readonly & { pushFlashMessage: typeof pushFlashMessage; clearAllFlashMessages: typeof clearAllFlashMessages; }>; class ResetPasswordContainer extends React.PureComponent { state: State = { isLoading: false, }; componentDidMount () { const parsed = parse(this.props.location.search); this.setState({ email: parsed.email as string || undefined }); } canSubmit () { if (!this.state.password || !this.state.email) { return false; } return this.state.password.length >= 8 && this.state.password === this.state.passwordConfirm; } onPasswordChange = (e: React.ChangeEvent) => this.setState({ password: e.target.value, }); onPasswordConfirmChange = (e: React.ChangeEvent) => this.setState({ passwordConfirm: e.target.value, }); onSubmit = (e: React.FormEvent) => { e.preventDefault(); const { password, passwordConfirm, email } = this.state; if (!password || !email || !passwordConfirm) { return; } this.props.clearAllFlashMessages(); this.setState({ isLoading: true }, () => { performPasswordReset(email, { token: this.props.match.params.token, password: password, passwordConfirmation: passwordConfirm, }) .then(response => { if (response.redirectTo) { // @ts-ignore window.location = response.redirectTo; return; } this.props.pushFlashMessage({ type: 'success', message: 'Your password has been reset, please login to continue.', }); this.props.history.push('/login'); }) .catch(error => { console.error(error); this.setState({ errorMessage: httpErrorToHuman(error) }); }) .then(() => this.setState({ isLoading: false })); }); }; render () { return (

Reset Password

Passwords must be at least 8 characters in length.

Return to Login
); } } const mapDispatchToProps = { pushFlashMessage, clearAllFlashMessages, }; export default connect(null, mapDispatchToProps)(ResetPasswordContainer);