import React, { FormEvent, ChangeEvent } from 'react'; import InfoBox from './info-box'; import ErrorBox from './error-box'; interface ForgottenPasswordState { success: boolean; error: string; inputs: any; emailUsed: string; } export default class ForgottenPassword extends React.Component<{}, ForgottenPasswordState> { constructor(props) { super(props); this.state = { error: undefined, success: undefined, inputs: { email: '' }, emailUsed: undefined }; } handleChange(event: ChangeEvent) { this.setState({ inputs: { [event.target.name]: event.target.value } }); } async handleSubmit(event: FormEvent) { event.preventDefault(); this.setState({ error: undefined, success: undefined }); const emailSent = this.state.inputs.email; try { const res = await fetch('/api/users/password', { method: 'PUT', body: JSON.stringify(this.state.inputs), 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 (

Forgotten password

Please provide the e-mail address associated with your account. A password reset link will be sent to your mailbox.

{this.state.success ? `A password reset link has been sent to ${this.state.emailUsed}. Please check your inbox.` : null }
this.handleSubmit(e)}> this.handleChange(e)} />
) } }