import React from 'react'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; 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, removeStoredVerification } from 'soapbox/actions/verification'; import { Button, Form, FormGroup, Input, Text } from 'soapbox/components/ui'; import { useAppDispatch, useAppSelector, useInstance, useSoapboxConfig } from 'soapbox/hooks'; import { getRedirectUrl } from 'soapbox/utils/redirect'; import PasswordIndicator from './components/password-indicator'; import type { AxiosError } from 'axios'; const messages = defineMessages({ success: { id: 'registrations.success', defaultMessage: 'Welcome to {siteTitle}!' }, usernameLabel: { id: 'registrations.username.label', defaultMessage: 'Your username' }, usernameHint: { id: 'registrations.username.hint', defaultMessage: 'May only contain A-Z, 0-9, and underscores' }, usernameTaken: { id: 'registrations.unprocessable_entity', defaultMessage: 'This username has already been taken.' }, passwordLabel: { id: 'registrations.password.label', defaultMessage: 'Password' }, error: { id: 'registrations.error', defaultMessage: 'Failed to register your account.' }, }); const initialState = { username: '', password: '', }; const Registration = () => { const dispatch = useAppDispatch(); const intl = useIntl(); const instance = useInstance(); const soapboxConfig = useSoapboxConfig(); const { links } = soapboxConfig; const isLoading = useAppSelector((state) => state.verification.isLoading as boolean); const [state, setState] = React.useState(initialState); const [shouldRedirect, setShouldRedirect] = React.useState(false); const [hasValidPassword, setHasValidPassword] = React.useState(false); const { username, password } = state; const handleSubmit = React.useCallback((event) => { event.preventDefault(); dispatch(createAccount(username, password)) .then(() => dispatch(logIn(username, password))) .then(({ access_token }: any) => dispatch(verifyCredentials(access_token))) .then(() => dispatch(fetchInstance())) .then(() => { setShouldRedirect(true); removeStoredVerification(); dispatch(startOnboarding()); dispatch( snackbar.success( intl.formatMessage(messages.success, { siteTitle: instance.title }), ), ); }) .catch((error: AxiosError) => { if (error?.response?.status === 422) { dispatch( snackbar.error( intl.formatMessage(messages.usernameTaken), ), ); } else { dispatch( snackbar.error( intl.formatMessage(messages.error), ), ); } }); }, [username, password]); const handleInputChange = React.useCallback((event) => { event.persist(); setState((prevState) => ({ ...prevState, [event.target.name]: event.target.value })); }, []); if (shouldRedirect) { const redirectUri = getRedirectUrl(); return ; } return (

{(links.get('termsOfService') && links.get('privacyPolicy')) ? ( ), privacy: ( ), }} /> ) : null}
); }; export default Registration;