import React, { FormEvent, useCallback, useEffect, useState } from 'react' import { useSelector } from 'react-redux' import { Redirect } from 'react-router' import { doInternalRegister } from '../../../../api/auth' import { ApplicationState } from '../../../../redux' import { getAndSetUser } from '../../../../utils/apiUtils' import { Row, Col, Card, Form, Button, Alert } from 'react-bootstrap' import { Trans, useTranslation } from 'react-i18next' import { TranslatedExternalLink } from '../../../common/links/translated-external-link' import { ShowIf } from '../../../common/show-if/show-if' export enum RegisterError { NONE = 'none', USERNAME_EXISTING = 'usernameExisting', OTHER = 'other' } export const Register: React.FC = () => { const { t } = useTranslation() const config = useSelector((state: ApplicationState) => state.config) const user = useSelector((state: ApplicationState) => state.user) const [username, setUsername] = useState('') const [password, setPassword] = useState('') const [passwordAgain, setPasswordAgain] = useState('') const [error, setError] = useState(RegisterError.NONE) const [ready, setReady] = useState(false) const doRegisterSubmit = useCallback((event: FormEvent) => { doInternalRegister(username, password) .then(() => getAndSetUser()) .catch((err: Error) => { console.error(err) setError(err.message === RegisterError.USERNAME_EXISTING ? err.message : RegisterError.OTHER) }) event.preventDefault() }, [username, password]) useEffect(() => { setReady(username !== '' && password !== '' && password.length >= 8 && password === passwordAgain) }, [username, password, passwordAgain]) if (!config.allowRegister) { return ( ) } if (user) { return ( ) } return (

setUsername(event.target.value)} placeholder={t('login.auth.username')} className='bg-dark text-white' autoComplete='username' autoFocus={true} required /> = 8} onChange={(event) => setPassword(event.target.value)} placeholder={t('login.auth.password')} className='bg-dark text-white' minLength={8} autoComplete='new-password' required /> setPasswordAgain(event.target.value)} placeholder={t('login.register.passwordAgain')} className='bg-dark text-white' autoComplete='new-password' required />

) }