2019-11-07 02:39:26 -05:00
|
|
|
import React, { ChangeEvent, FormEvent } from 'react';
|
|
|
|
|
2019-08-23 09:16:40 -04:00
|
|
|
import ErrorBox from '../components/error-box';
|
2019-11-07 02:39:26 -05:00
|
|
|
import InfoBox from '../components/info-box';
|
2019-08-22 09:40:38 -04:00
|
|
|
|
|
|
|
interface ForgottenPasswordState {
|
|
|
|
success: boolean;
|
|
|
|
error: string;
|
2019-08-22 09:48:51 -04:00
|
|
|
email: string;
|
2019-08-22 09:40:38 -04:00
|
|
|
emailUsed: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class ForgottenPassword extends React.Component<{}, ForgottenPasswordState> {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
error: undefined,
|
|
|
|
success: undefined,
|
2019-08-22 09:48:51 -04:00
|
|
|
email: undefined,
|
2019-08-22 09:40:38 -04:00
|
|
|
emailUsed: undefined
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handleChange(event: ChangeEvent<HTMLInputElement>) {
|
2019-08-22 09:48:51 -04:00
|
|
|
const { name, value } = event.currentTarget;
|
|
|
|
this.setState({ [name]: value } as any);
|
2019-08-22 09:40:38 -04:00
|
|
|
}
|
2019-08-23 09:16:40 -04:00
|
|
|
|
2019-08-22 09:40:38 -04:00
|
|
|
async handleSubmit(event: FormEvent) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.setState({ error: undefined, success: undefined });
|
|
|
|
|
2019-08-22 09:48:51 -04:00
|
|
|
const emailSent = this.state.email;
|
2019-08-22 09:40:38 -04:00
|
|
|
try {
|
|
|
|
const res = await fetch('/api/users/password', {
|
|
|
|
method: 'PUT',
|
2019-08-22 09:48:51 -04:00
|
|
|
body: JSON.stringify({ email: emailSent }),
|
2019-08-22 09:40:38 -04:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const data = await res.json();
|
|
|
|
|
|
|
|
if (data.error != undefined) {
|
|
|
|
this.setState({ error: data.error });
|
|
|
|
} else if (data.success === true) {
|
|
|
|
this.setState({ success: true, emailUsed: emailSent});
|
|
|
|
} else {
|
|
|
|
this.setState({ error: 'Unexpected result.' });
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
this.setState({ error: 'Something went wrong.' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<article>
|
|
|
|
<section className="main-col">
|
|
|
|
<h1 className="h2">Forgotten password</h1>
|
|
|
|
<p>Please provide the e-mail address associated with your account. A password reset link will be sent to your mailbox.</p>
|
|
|
|
<ErrorBox msg={this.state.error} />
|
|
|
|
<InfoBox msg="">
|
|
|
|
{this.state.success ?
|
2019-11-14 11:17:05 -05:00
|
|
|
`If the email address is registered on Colouring London, a password reset link will be sent to ${this.state.emailUsed}. Please check your inbox.` :
|
2019-08-22 09:40:38 -04:00
|
|
|
null
|
|
|
|
}
|
|
|
|
</InfoBox>
|
|
|
|
<form onSubmit={e => this.handleSubmit(e)}>
|
|
|
|
<label htmlFor="email">E-mail</label>
|
|
|
|
<input name="email" id="email"
|
|
|
|
className="form-control" type="email"
|
|
|
|
placeholder="Your e-mail address" required
|
|
|
|
onChange={e => this.handleChange(e)}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<div className="buttons-container">
|
|
|
|
<input type="submit" value="Request password reset" className="btn btn-primary" />
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</section>
|
|
|
|
</article>
|
2019-11-07 03:13:30 -05:00
|
|
|
);
|
2019-08-22 09:40:38 -04:00
|
|
|
}
|
2019-08-23 09:16:40 -04:00
|
|
|
}
|