colouring-montreal/app/src/frontend/user/forgotten-password.tsx

86 lines
3.0 KiB
TypeScript
Raw Normal View History

2019-11-07 02:39:26 -05:00
import React, { ChangeEvent, FormEvent } from 'react';
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;
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,
email: undefined,
2019-08-22 09:40:38 -04:00
emailUsed: undefined
};
}
handleChange(event: ChangeEvent<HTMLInputElement>) {
const { name, value } = event.currentTarget;
this.setState({ [name]: value } as any);
2019-08-22 09:40:38 -04:00
}
2019-08-22 09:40:38 -04:00
async handleSubmit(event: FormEvent) {
event.preventDefault();
this.setState({ error: undefined, success: undefined });
const emailSent = this.state.email;
2019-08-22 09:40:38 -04:00
try {
const res = await fetch('/api/users/password', {
method: 'PUT',
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 ?
`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
}
}