2022-04-07 10:39:22 -07:00
|
|
|
import { AxiosError } from 'axios';
|
2022-03-21 11:09:01 -07:00
|
|
|
import * as React from 'react';
|
2022-05-24 03:24:26 -07:00
|
|
|
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
|
2022-04-07 10:39:22 -07:00
|
|
|
import { useDispatch } from 'react-redux';
|
2022-03-21 11:09:01 -07:00
|
|
|
import { Redirect } from 'react-router-dom';
|
|
|
|
|
|
|
|
import { logIn, verifyCredentials } from 'soapbox/actions/auth';
|
|
|
|
import { fetchInstance } from 'soapbox/actions/instance';
|
2022-05-02 13:55:52 -07:00
|
|
|
import { startOnboarding } from 'soapbox/actions/onboarding';
|
2022-03-21 11:09:01 -07:00
|
|
|
import snackbar from 'soapbox/actions/snackbar';
|
2022-06-07 13:21:18 -07:00
|
|
|
import { createAccount, removeStoredVerification } from 'soapbox/actions/verification';
|
2022-05-24 03:24:26 -07:00
|
|
|
import { Button, Form, FormGroup, Input } from 'soapbox/components/ui';
|
2022-04-07 10:39:22 -07:00
|
|
|
import { useAppSelector } from 'soapbox/hooks';
|
2022-05-16 14:33:45 -07:00
|
|
|
import { getRedirectUrl } from 'soapbox/utils/redirect';
|
2022-03-21 11:09:01 -07:00
|
|
|
|
2022-05-24 03:24:26 -07:00
|
|
|
const messages = defineMessages({
|
|
|
|
success: {
|
|
|
|
id: 'registrations.success',
|
|
|
|
defaultMessage: 'Welcome to {siteTitle}!',
|
|
|
|
},
|
|
|
|
usernameTaken: {
|
|
|
|
id: 'registrations.unprocessable_entity',
|
|
|
|
defaultMessage: 'This username has already been taken.',
|
|
|
|
},
|
|
|
|
error: {
|
|
|
|
id: 'registrations.error',
|
|
|
|
defaultMessage: 'Failed to register your account.',
|
|
|
|
},
|
|
|
|
});
|
2022-03-21 11:09:01 -07:00
|
|
|
|
|
|
|
const initialState = {
|
|
|
|
username: '',
|
|
|
|
password: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
const Registration = () => {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const intl = useIntl();
|
|
|
|
|
2022-04-07 10:39:22 -07:00
|
|
|
const isLoading = useAppSelector((state) => state.verification.get('isLoading') as boolean);
|
|
|
|
const siteTitle = useAppSelector((state) => state.instance.title);
|
2022-03-21 11:09:01 -07:00
|
|
|
|
|
|
|
const [state, setState] = React.useState(initialState);
|
2022-04-07 10:39:22 -07:00
|
|
|
const [shouldRedirect, setShouldRedirect] = React.useState<boolean>(false);
|
2022-03-21 11:09:01 -07:00
|
|
|
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)))
|
2022-04-07 10:39:22 -07:00
|
|
|
.then(({ access_token }: any) => dispatch(verifyCredentials(access_token)))
|
2022-03-21 11:09:01 -07:00
|
|
|
.then(() => dispatch(fetchInstance()))
|
|
|
|
.then(() => {
|
|
|
|
setShouldRedirect(true);
|
|
|
|
removeStoredVerification();
|
2022-05-02 13:55:52 -07:00
|
|
|
dispatch(startOnboarding());
|
2022-03-21 11:09:01 -07:00
|
|
|
dispatch(
|
|
|
|
snackbar.success(
|
2022-05-24 03:24:26 -07:00
|
|
|
intl.formatMessage(messages.success, { siteTitle }),
|
2022-03-21 11:09:01 -07:00
|
|
|
),
|
|
|
|
);
|
|
|
|
})
|
2022-04-07 10:39:22 -07:00
|
|
|
.catch((error: AxiosError) => {
|
2022-03-21 11:09:01 -07:00
|
|
|
if (error?.response?.status === 422) {
|
|
|
|
dispatch(
|
|
|
|
snackbar.error(
|
2022-05-24 03:24:26 -07:00
|
|
|
intl.formatMessage(messages.usernameTaken),
|
2022-03-21 11:09:01 -07:00
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
dispatch(
|
|
|
|
snackbar.error(
|
2022-05-24 03:24:26 -07:00
|
|
|
intl.formatMessage(messages.error),
|
2022-03-21 11:09:01 -07:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, [username, password]);
|
|
|
|
|
|
|
|
const handleInputChange = React.useCallback((event) => {
|
|
|
|
event.persist();
|
|
|
|
|
|
|
|
setState((prevState) => ({ ...prevState, [event.target.name]: event.target.value }));
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (shouldRedirect) {
|
2022-05-16 14:33:45 -07:00
|
|
|
const redirectUri = getRedirectUrl();
|
|
|
|
return <Redirect to={redirectUri} />;
|
2022-03-21 11:09:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div className='pb-4 sm:pb-10 mb-4 border-b border-gray-200 border-solid -mx-4 sm:-mx-10'>
|
|
|
|
<h1 className='text-center font-bold text-2xl'>
|
2022-05-24 03:24:26 -07:00
|
|
|
<FormattedMessage id='registration.header' defaultMessage='Register your account' />
|
2022-03-21 11:09:01 -07:00
|
|
|
</h1>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className='sm:pt-10 sm:w-2/3 md:w-1/2 mx-auto space-y-4'>
|
2022-04-04 08:54:00 -07:00
|
|
|
<Form onSubmit={handleSubmit}>
|
2022-03-21 11:09:01 -07:00
|
|
|
<FormGroup labelText='Your username'>
|
|
|
|
<Input
|
|
|
|
name='username'
|
|
|
|
type='text'
|
|
|
|
value={username}
|
|
|
|
onChange={handleInputChange}
|
|
|
|
required
|
|
|
|
icon={require('@tabler/icons/icons/at.svg')}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
|
|
|
|
<FormGroup labelText='Password'>
|
|
|
|
<Input
|
|
|
|
name='password'
|
|
|
|
type='password'
|
|
|
|
value={password}
|
|
|
|
onChange={handleInputChange}
|
|
|
|
required
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
|
|
|
|
<div className='text-center'>
|
|
|
|
<Button block theme='primary' type='submit' disabled={isLoading}>Register</Button>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Registration;
|