import { AxiosError } from 'axios'; import * as React from 'react'; import { useIntl } from 'react-intl'; import { useDispatch } from 'react-redux'; import { Redirect } from 'react-router-dom'; import { logIn, verifyCredentials } from 'soapbox/actions/auth'; import { fetchInstance } from 'soapbox/actions/instance'; import { startOnboarding } from 'soapbox/actions/onboarding'; import snackbar from 'soapbox/actions/snackbar'; import { createAccount } from 'soapbox/actions/verification'; import { removeStoredVerification } from 'soapbox/actions/verification'; import { useAppSelector } from 'soapbox/hooks'; import { Button, Form, FormGroup, Input } from '../../components/ui'; const initialState = { username: '', password: '', }; const Registration = () => { const dispatch = useDispatch(); const intl = useIntl(); const isLoading = useAppSelector((state) => state.verification.get('isLoading') as boolean); const siteTitle = useAppSelector((state) => state.instance.title); const [state, setState] = React.useState(initialState); const [shouldRedirect, setShouldRedirect] = React.useState(false); const { username, password } = state; const handleSubmit = React.useCallback((event) => { event.preventDefault(); // TODO: handle validation errors from Pepe dispatch(createAccount(username, password)) .then(() => dispatch(logIn(intl, username, password))) .then(({ access_token }: any) => dispatch(verifyCredentials(access_token))) .then(() => dispatch(fetchInstance())) .then(() => { setShouldRedirect(true); removeStoredVerification(); dispatch(startOnboarding()); dispatch( snackbar.success( intl.formatMessage({ id: 'registrations.success', defaultMessage: 'Welcome to {siteTitle}!', }, { siteTitle }), ), ); }) .catch((error: AxiosError) => { if (error?.response?.status === 422) { dispatch( snackbar.error( intl.formatMessage({ id: 'registrations.unprocessable_entity', defaultMessage: 'This username has already been taken.', }), ), ); } else { dispatch( snackbar.error( intl.formatMessage({ id: 'registrations.error', defaultMessage: 'Failed to register your account.', }), ), ); } }); }, [username, password]); const handleInputChange = React.useCallback((event) => { event.persist(); setState((prevState) => ({ ...prevState, [event.target.name]: event.target.value })); }, []); if (shouldRedirect) { return ; } return (

{intl.formatMessage({ id: 'registration.header', defaultMessage: 'Register your account' })}

); }; export default Registration;