2019-06-12 06:02:18 +01:00
|
|
|
import * as React from 'react';
|
|
|
|
import OpenInputField from '@/components/forms/OpenInputField';
|
|
|
|
import { Link } from 'react-router-dom';
|
|
|
|
import requestPasswordResetEmail from '@/api/auth/requestPasswordResetEmail';
|
2019-06-12 07:12:03 +01:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { ReduxState } from '@/redux/types';
|
|
|
|
import { pushFlashMessage, clearAllFlashMessages } from '@/redux/actions/flash';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
2019-06-12 06:02:18 +01:00
|
|
|
|
|
|
|
type Props = Readonly<{
|
2019-06-12 07:12:03 +01:00
|
|
|
pushFlashMessage: typeof pushFlashMessage;
|
|
|
|
clearAllFlashMessages: typeof clearAllFlashMessages;
|
2019-06-12 06:02:18 +01:00
|
|
|
}>;
|
|
|
|
|
|
|
|
type State = Readonly<{
|
|
|
|
email: string;
|
|
|
|
isSubmitting: boolean;
|
|
|
|
}>;
|
|
|
|
|
2019-06-12 07:12:03 +01:00
|
|
|
class ForgotPasswordContainer extends React.PureComponent<Props, State> {
|
2019-06-12 06:02:18 +01:00
|
|
|
state: State = {
|
|
|
|
email: '',
|
|
|
|
isSubmitting: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
handleFieldUpdate = (e: React.ChangeEvent<HTMLInputElement>) => this.setState({
|
|
|
|
email: e.target.value,
|
|
|
|
});
|
|
|
|
|
2019-06-12 07:12:03 +01:00
|
|
|
handleSubmission = (e: React.FormEvent<HTMLFormElement>) => {
|
2019-06-12 06:02:18 +01:00
|
|
|
e.preventDefault();
|
|
|
|
|
2019-06-12 07:12:03 +01:00
|
|
|
this.setState({ isSubmitting: true }, () => {
|
|
|
|
this.props.clearAllFlashMessages();
|
|
|
|
requestPasswordResetEmail(this.state.email)
|
2019-06-12 07:19:43 +01:00
|
|
|
.then(response => this.props.pushFlashMessage({
|
|
|
|
type: 'success', title: 'Success', message: response,
|
|
|
|
}))
|
2019-06-12 07:12:03 +01:00
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
this.props.pushFlashMessage({
|
|
|
|
type: 'error',
|
|
|
|
title: 'Error',
|
|
|
|
message: httpErrorToHuman(error),
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(() => this.setState({ isSubmitting: false }));
|
|
|
|
});
|
|
|
|
};
|
2019-06-12 06:02:18 +01:00
|
|
|
|
|
|
|
render () {
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
|
|
|
<form className={'login-box'} onSubmit={this.handleSubmission}>
|
|
|
|
<div className={'-mx-3'}>
|
|
|
|
<OpenInputField
|
|
|
|
id={'email'}
|
|
|
|
type={'email'}
|
|
|
|
label={'Email'}
|
|
|
|
description={'Enter your account email address to receive instructions on resetting your password.'}
|
|
|
|
autoFocus={true}
|
|
|
|
required={true}
|
|
|
|
onChange={this.handleFieldUpdate}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className={'mt-6'}>
|
|
|
|
<button
|
2019-06-12 07:12:03 +01:00
|
|
|
className={'btn btn-primary btn-jumbo flex justify-center'}
|
2019-06-12 06:02:18 +01:00
|
|
|
disabled={this.state.isSubmitting || this.state.email.length < 5}
|
|
|
|
>
|
|
|
|
{this.state.isSubmitting ?
|
2019-06-12 07:12:03 +01:00
|
|
|
<div className={'spinner-circle spinner-sm spinner-white'}></div>
|
2019-06-12 06:02:18 +01:00
|
|
|
:
|
|
|
|
'Send Email'
|
|
|
|
}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<div className={'mt-6 text-center'}>
|
|
|
|
<Link
|
|
|
|
to={'/login'}
|
|
|
|
className={'text-xs text-neutral-500 tracking-wide uppercase no-underline hover:text-neutral-700'}
|
|
|
|
>
|
|
|
|
Return to Login
|
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-06-12 07:12:03 +01:00
|
|
|
|
|
|
|
const mapStateToProps = (state: ReduxState) => ({
|
|
|
|
flashes: state.flashes,
|
|
|
|
});
|
|
|
|
|
|
|
|
const mapDispatchToProps = {
|
|
|
|
pushFlashMessage,
|
|
|
|
clearAllFlashMessages,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(ForgotPasswordContainer);
|