import React from 'react' import { t } from 'ttag' import ModalCard from './ModalCard' import SocialLogin from './SocialLogin' import { post } from '../io' type SignupModalProps = { onSignup: (email: string) => any } type SignupModalState = { emailText: string | null passwordText: string | null confirmText: string | null loading: boolean error: string | null emailError: boolean } class SignupModal extends React.Component { modal?: ModalCard emailInput?: HTMLInputElement constructor(props: SignupModalProps) { super(props) this.state = { emailText: null, passwordText: null, confirmText: null, loading: false, error: null, emailError: false, } } show() { this.reset() this.modal?.show() setTimeout(() => { this.emailInput?.focus() }, 1) } hide() { this.modal?.hide() this.reset() } reset() { this.setState({ emailText: null, passwordText: null, confirmText: null, loading: false, error: null, emailError: false, }) } submit() { const { emailText, passwordText, confirmText } = this.state const { onSignup } = this.props if (passwordText !== confirmText) { this.setState({ error: t`Passwords don't match` }) return } if (emailText && passwordText) { this.setState({ loading: true, error: null, emailError: false }) post('/accounts/create-account/', { email: emailText, password: passwordText }) .then(response => { const { status } = response if (status === 400) { return response.json() } if (status < 200 || status >= 300) { throw new Error(t`There was an unexpected error while creating your account`) } onSignup(emailText) this.hide() return false }) .then(json => { if (json) { if (json.email) { this.setState({ emailError: true }) throw new Error(json.email) } throw new Error(t`There was an unexpected error while creating your account`) } }) .catch(err => { this.setState({ loading: false, error: err.message }) }) } } render() { const { emailText, passwordText, confirmText, loading, error, emailError } = this.state let errorNode = null if (error !== null) { errorNode = (
{error}
) } return ( { this.modal = input }} title={t`Create Account`} > {errorNode}
{ e.preventDefault() this.submit() }} >
{ this.emailInput = input }} className={`${emailError ? 'is-danger' : ''} 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} />
{ this.setState({ confirmText: e.target.value }) }} disabled={loading} />

) } } export default SignupModal