import React from 'react'
import { c, t } from 'ttag'
import ModalCard from './ModalCard'
import { post } from '../io'
type ResetPasswordModalState = {
emailText: string | null
submitted: boolean
loading: boolean
error: string | null
}
class ResetPasswordModal extends React.Component<{}, ResetPasswordModalState> {
modal?: ModalCard | null
emailInput?: HTMLInputElement | null
constructor(props: any) {
super(props)
this.state = { emailText: null, submitted: false, loading: false, error: null }
}
show() {
this.reset()
this.modal?.show()
setTimeout(() => this.emailInput?.focus(), 1)
}
hide() {
this.modal?.hide()
this.reset()
}
reset() {
this.setState({ emailText: null, submitted: false, loading: false, error: null })
}
submit() {
const { emailText } = this.state
if ((emailText || '').trim()) {
post('/accounts/lost-password/', { email: emailText })
.then(response => {
const { status } = response
if (status === 400) {
return response.json()
}
if (status < 200 || status >= 300) {
throw new Error(t`Sorry, there was an error resetting your password`)
}
this.setState({ submitted: true, loading: false })
return null
})
.then(json => {
if (json) {
const { email } = json
if (email) {
throw new Error(email)
}
}
})
.catch(err => {
this.setState({ loading: false, error: err.message })
})
}
}
render() {
const { emailText, submitted, loading, error } = this.state
let content
if (submitted) {
content = (