import React from 'react' import { t } from 'ttag' import ModalCard from './ModalCard' import SocialLogin from './SocialLogin' import { post } from '../io' type LoginModalProps = { onLogin: (email: string | null) => any onResetPassword: () => any } type LoginModalState = { emailText: string | null passwordText: string | null loading: boolean error: string | null } class LoginModal extends React.Component { modal?: ModalCard | null emailInput?: HTMLInputElement | null constructor(props: LoginModalProps) { super(props) this.state = { emailText: null, passwordText: null, 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, passwordText: null, loading: false, error: null }) } submit() { const { emailText, passwordText } = this.state const { onLogin } = this.props if (emailText && passwordText) { this.setState({ loading: true, error: null }) post('/accounts/login/', { email: emailText, password: passwordText }) .then(response => { const { status } = response if (status >= 200 && status < 300) { onLogin(emailText) this.hide() } else { throw new Error('Login failed') } }) .catch(() => { this.setState({ loading: false, error: t`Login failed` }) }) } } render() { const { emailText, passwordText, loading, error } = this.state const { onResetPassword } = this.props let errorNode = null if (error !== null) { errorNode = (
{error}
) } return ( { this.modal = input }} title={t`Sign In`} > {errorNode}
{ e.preventDefault() this.submit() }} >
{ this.emailInput = input }} className="input" type="text" placeholder={t`Email address`} name="username" value={emailText || ''} onChange={e => { this.setState({ emailText: e.target.value }) }} disabled={loading} />
{ this.setState({ passwordText: e.target.value }) }} disabled={loading} />
onResetPassword()}> {t`Forgot your password?`}
) } } export default LoginModal