RegistrationForm: convert to tsx
This commit is contained in:
parent
45a7dc5bcf
commit
90129818f2
4 changed files with 372 additions and 0 deletions
|
@ -14,8 +14,17 @@ interface IShowablePassword {
|
|||
label?: React.ReactNode,
|
||||
className?: string,
|
||||
hint?: React.ReactNode,
|
||||
placeholder?: string,
|
||||
error?: boolean,
|
||||
onToggleVisibility?: () => void,
|
||||
autoComplete?: string,
|
||||
autoCorrect?: string,
|
||||
autoCapitalize?: string,
|
||||
name?: string,
|
||||
required?: boolean,
|
||||
onChange?: React.ChangeEventHandler<HTMLInputElement>,
|
||||
onBlur?: React.ChangeEventHandler<HTMLInputElement>,
|
||||
value?: string,
|
||||
}
|
||||
|
||||
const ShowablePassword: React.FC<IShowablePassword> = (props) => {
|
||||
|
|
Binary file not shown.
348
app/soapbox/features/auth_login/components/registration_form.tsx
Normal file
348
app/soapbox/features/auth_login/components/registration_form.tsx
Normal file
|
@ -0,0 +1,348 @@
|
|||
import axios from 'axios';
|
||||
import { Map as ImmutableMap } from 'immutable';
|
||||
import { debounce } from 'lodash';
|
||||
import React, { useState, useRef } from 'react';
|
||||
import { useIntl, FormattedMessage, defineMessages } from 'react-intl';
|
||||
import { Link, useHistory } from 'react-router-dom';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
import { accountLookup } from 'soapbox/actions/accounts';
|
||||
import { register, verifyCredentials } from 'soapbox/actions/auth';
|
||||
import { openModal } from 'soapbox/actions/modals';
|
||||
import BirthdayInput from 'soapbox/components/birthday_input';
|
||||
import ShowablePassword from 'soapbox/components/showable_password';
|
||||
import CaptchaField from 'soapbox/features/auth_login/components/captcha';
|
||||
import {
|
||||
SimpleForm,
|
||||
SimpleInput,
|
||||
TextInput,
|
||||
SimpleTextarea,
|
||||
Checkbox,
|
||||
} from 'soapbox/features/forms';
|
||||
import { useAppSelector, useAppDispatch, useSettings, useFeatures } from 'soapbox/hooks';
|
||||
|
||||
const messages = defineMessages({
|
||||
username: { id: 'registration.fields.username_placeholder', defaultMessage: 'Username' },
|
||||
username_hint: { id: 'registration.fields.username_hint', defaultMessage: 'Only letters, numbers, and underscores are allowed.' },
|
||||
email: { id: 'registration.fields.email_placeholder', defaultMessage: 'E-Mail address' },
|
||||
password: { id: 'registration.fields.password_placeholder', defaultMessage: 'Password' },
|
||||
confirm: { id: 'registration.fields.confirm_placeholder', defaultMessage: 'Password (again)' },
|
||||
agreement: { id: 'registration.agreement', defaultMessage: 'I agree to the {tos}.' },
|
||||
tos: { id: 'registration.tos', defaultMessage: 'Terms of Service' },
|
||||
close: { id: 'registration.confirmation_modal.close', defaultMessage: 'Close' },
|
||||
newsletter: { id: 'registration.newsletter', defaultMessage: 'Subscribe to newsletter.' },
|
||||
needsConfirmationHeader: { id: 'confirmations.register.needs_confirmation.header', defaultMessage: 'Confirmation needed' },
|
||||
needsApprovalHeader: { id: 'confirmations.register.needs_approval.header', defaultMessage: 'Approval needed' },
|
||||
});
|
||||
|
||||
interface IRegistrationForm {
|
||||
inviteToken?: string,
|
||||
}
|
||||
|
||||
/** Allows the user to sign up for the website. */
|
||||
const RegistrationForm: React.FC<IRegistrationForm> = ({ inviteToken }) => {
|
||||
const intl = useIntl();
|
||||
const history = useHistory();
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
const settings = useSettings();
|
||||
const features = useFeatures();
|
||||
const instance = useAppSelector(state => state.instance);
|
||||
|
||||
const locale = settings.get('locale');
|
||||
const needsConfirmation = !!instance.pleroma.getIn(['metadata', 'account_activation_required']);
|
||||
const needsApproval = instance.approval_required;
|
||||
const supportsEmailList = features.emailList;
|
||||
const supportsAccountLookup = features.accountLookup;
|
||||
const birthdayRequired = instance.pleroma.getIn(['metadata', 'birthday_required']);
|
||||
|
||||
const [captchaLoading, setCaptchaLoading] = useState(true);
|
||||
const [submissionLoading, setSubmissionLoading] = useState(false);
|
||||
const [params, setParams] = useState(ImmutableMap<string, any>());
|
||||
const [captchaIdempotencyKey, setCaptchaIdempotencyKey] = useState(uuidv4());
|
||||
const [usernameUnavailable, setUsernameUnavailable] = useState(false);
|
||||
const [passwordConfirmation, setPasswordConfirmation] = useState('');
|
||||
const [passwordMismatch, setPasswordMismatch] = useState(false);
|
||||
const [birthday, setBirthday] = useState<Date | undefined>(undefined);
|
||||
|
||||
const source = useRef(axios.CancelToken.source());
|
||||
|
||||
const refreshCancelToken = () => {
|
||||
source.current.cancel();
|
||||
source.current = axios.CancelToken.source();
|
||||
return source.current;
|
||||
};
|
||||
|
||||
const updateParams = (map: any) => {
|
||||
setParams(params.merge(ImmutableMap(map)));
|
||||
};
|
||||
|
||||
const onInputChange: React.ChangeEventHandler<HTMLInputElement | HTMLTextAreaElement> = e => {
|
||||
updateParams({ [e.target.name]: e.target.value });
|
||||
};
|
||||
|
||||
const onUsernameChange: React.ChangeEventHandler<HTMLInputElement> = e => {
|
||||
updateParams({ username: e.target.value });
|
||||
setUsernameUnavailable(false);
|
||||
source.current.cancel();
|
||||
|
||||
usernameAvailable(e.target.value);
|
||||
};
|
||||
|
||||
const onCheckboxChange: React.ChangeEventHandler<HTMLInputElement> = e => {
|
||||
updateParams({ [e.target.name]: e.target.checked });
|
||||
};
|
||||
|
||||
const onPasswordChange: React.ChangeEventHandler<HTMLInputElement> = e => {
|
||||
const password = e.target.value;
|
||||
onInputChange(e);
|
||||
|
||||
if (password === passwordConfirmation) {
|
||||
setPasswordMismatch(false);
|
||||
}
|
||||
};
|
||||
|
||||
const onPasswordConfirmChange: React.ChangeEventHandler<HTMLInputElement> = e => {
|
||||
const password = params.get('password', '');
|
||||
const passwordConfirmation = e.target.value;
|
||||
setPasswordConfirmation(passwordConfirmation);
|
||||
|
||||
if (password === passwordConfirmation) {
|
||||
setPasswordMismatch(false);
|
||||
}
|
||||
};
|
||||
|
||||
const onPasswordConfirmBlur: React.ChangeEventHandler<HTMLInputElement> = () => {
|
||||
setPasswordMismatch(!passwordsMatch());
|
||||
};
|
||||
|
||||
const onBirthdayChange = (newBirthday: Date) => {
|
||||
setBirthday(newBirthday);
|
||||
};
|
||||
|
||||
const launchModal = () => {
|
||||
const message = (<>
|
||||
{needsConfirmation && <p>
|
||||
<FormattedMessage
|
||||
id='confirmations.register.needs_confirmation'
|
||||
defaultMessage='Please check your inbox at {email} for confirmation instructions. You will need to verify your email address to continue.'
|
||||
values={{ email: <strong>{params.get('email')}</strong> }}
|
||||
/></p>}
|
||||
{needsApproval && <p>
|
||||
<FormattedMessage
|
||||
id='confirmations.register.needs_approval'
|
||||
defaultMessage='Your account will be manually approved by an admin. Please be patient while we review your details.'
|
||||
/></p>}
|
||||
</>);
|
||||
|
||||
dispatch(openModal('CONFIRM', {
|
||||
icon: require('@tabler/icons/icons/check.svg'),
|
||||
heading: needsConfirmation
|
||||
? intl.formatMessage(messages.needsConfirmationHeader)
|
||||
: needsApproval
|
||||
? intl.formatMessage(messages.needsApprovalHeader)
|
||||
: undefined,
|
||||
message,
|
||||
confirm: intl.formatMessage(messages.close),
|
||||
}));
|
||||
};
|
||||
|
||||
const postRegisterAction = ({ access_token }: any) => {
|
||||
if (needsConfirmation || needsApproval) {
|
||||
return launchModal();
|
||||
} else {
|
||||
return dispatch(verifyCredentials(access_token)).then(() => {
|
||||
history.push('/');
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const passwordsMatch = () => {
|
||||
return params.get('password', '') === passwordConfirmation;
|
||||
};
|
||||
|
||||
const usernameAvailable = debounce(username => {
|
||||
if (!supportsAccountLookup) return;
|
||||
|
||||
const source = refreshCancelToken();
|
||||
|
||||
dispatch(accountLookup(username, source.token))
|
||||
.then(account => {
|
||||
setUsernameUnavailable(!!account);
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error.response?.status === 404) {
|
||||
setUsernameUnavailable(false);
|
||||
}
|
||||
});
|
||||
|
||||
}, 1000, { trailing: true });
|
||||
|
||||
const onSubmit: React.FormEventHandler = () => {
|
||||
if (!passwordsMatch()) {
|
||||
setPasswordMismatch(true);
|
||||
return;
|
||||
}
|
||||
|
||||
const normalParams = params.withMutations(params => {
|
||||
// Locale for confirmation email
|
||||
params.set('locale', locale);
|
||||
|
||||
// Pleroma invites
|
||||
if (inviteToken) {
|
||||
params.set('token', inviteToken);
|
||||
}
|
||||
|
||||
if (birthday) {
|
||||
params.set('birthday', new Date(birthday.getTime() - (birthday.getTimezoneOffset() * 60000)).toISOString().slice(0, 10));
|
||||
}
|
||||
});
|
||||
|
||||
setSubmissionLoading(true);
|
||||
|
||||
dispatch(register(normalParams.toJS()))
|
||||
.then(postRegisterAction)
|
||||
.catch(() => {
|
||||
setSubmissionLoading(false);
|
||||
refreshCaptcha();
|
||||
});
|
||||
};
|
||||
|
||||
const onCaptchaClick: React.MouseEventHandler = () => {
|
||||
refreshCaptcha();
|
||||
};
|
||||
|
||||
const onFetchCaptcha = (captcha: ImmutableMap<string, any>) => {
|
||||
setCaptchaLoading(false);
|
||||
updateParams({
|
||||
captcha_token: captcha.get('token'),
|
||||
captcha_answer_data: captcha.get('answer_data'),
|
||||
});
|
||||
};
|
||||
|
||||
const onFetchCaptchaFail = () => {
|
||||
setCaptchaLoading(false);
|
||||
};
|
||||
|
||||
const refreshCaptcha = () => {
|
||||
setCaptchaIdempotencyKey(uuidv4());
|
||||
updateParams({ captcha_solution: '' });
|
||||
};
|
||||
|
||||
const isLoading = captchaLoading || submissionLoading;
|
||||
|
||||
return (
|
||||
<SimpleForm onSubmit={onSubmit} data-testid='registrations-open'>
|
||||
<fieldset disabled={isLoading}>
|
||||
<div className='simple_form__overlay-area'>
|
||||
<div className='fields-group'>
|
||||
{usernameUnavailable && (
|
||||
<div className='error'>
|
||||
<FormattedMessage id='registration.username_unavailable' defaultMessage='Username is already taken.' />
|
||||
</div>
|
||||
)}
|
||||
<TextInput
|
||||
placeholder={intl.formatMessage(messages.username)}
|
||||
name='username'
|
||||
hint={intl.formatMessage(messages.username_hint)}
|
||||
autoComplete='off'
|
||||
autoCorrect='off'
|
||||
autoCapitalize='off'
|
||||
pattern='^[a-zA-Z\d_-]+'
|
||||
onChange={onUsernameChange}
|
||||
value={params.get('username', '')}
|
||||
error={usernameUnavailable}
|
||||
required
|
||||
/>
|
||||
<SimpleInput
|
||||
placeholder={intl.formatMessage(messages.email)}
|
||||
name='email'
|
||||
type='email'
|
||||
autoComplete='off'
|
||||
autoCorrect='off'
|
||||
autoCapitalize='off'
|
||||
onChange={onInputChange}
|
||||
value={params.get('email', '')}
|
||||
required
|
||||
/>
|
||||
{passwordMismatch && (
|
||||
<div className='error'>
|
||||
<FormattedMessage id='registration.password_mismatch' defaultMessage="Passwords don't match." />
|
||||
</div>
|
||||
)}
|
||||
<ShowablePassword
|
||||
placeholder={intl.formatMessage(messages.password)}
|
||||
name='password'
|
||||
autoComplete='off'
|
||||
autoCorrect='off'
|
||||
autoCapitalize='off'
|
||||
onChange={onPasswordChange}
|
||||
value={params.get('password', '')}
|
||||
error={passwordMismatch === true}
|
||||
required
|
||||
/>
|
||||
<ShowablePassword
|
||||
placeholder={intl.formatMessage(messages.confirm)}
|
||||
name='password_confirmation'
|
||||
autoComplete='off'
|
||||
autoCorrect='off'
|
||||
autoCapitalize='off'
|
||||
onChange={onPasswordConfirmChange}
|
||||
onBlur={onPasswordConfirmBlur}
|
||||
value={passwordConfirmation}
|
||||
error={passwordMismatch === true}
|
||||
required
|
||||
/>
|
||||
{birthdayRequired &&
|
||||
<BirthdayInput
|
||||
value={birthday}
|
||||
onChange={onBirthdayChange}
|
||||
required
|
||||
/>}
|
||||
{instance.get('approval_required') &&
|
||||
<SimpleTextarea
|
||||
label={<FormattedMessage id='registration.reason' defaultMessage='Why do you want to join?' />}
|
||||
hint={<FormattedMessage id='registration.reason_hint' defaultMessage='This will help us review your application' />}
|
||||
name='reason'
|
||||
maxLength={500}
|
||||
onChange={onInputChange}
|
||||
value={params.get('reason', '')}
|
||||
required
|
||||
/>}
|
||||
</div>
|
||||
<CaptchaField
|
||||
onFetch={onFetchCaptcha}
|
||||
onFetchFail={onFetchCaptchaFail}
|
||||
onChange={onInputChange}
|
||||
onClick={onCaptchaClick}
|
||||
idempotencyKey={captchaIdempotencyKey}
|
||||
name='captcha_solution'
|
||||
value={params.get('captcha_solution', '')}
|
||||
/>
|
||||
<div className='fields-group'>
|
||||
<Checkbox
|
||||
label={intl.formatMessage(messages.agreement, { tos: <Link to='/about/tos' target='_blank' key={0}>{intl.formatMessage(messages.tos)}</Link> })}
|
||||
name='agreement'
|
||||
onChange={onCheckboxChange}
|
||||
checked={params.get('agreement', false)}
|
||||
required
|
||||
/>
|
||||
{supportsEmailList && <Checkbox
|
||||
label={intl.formatMessage(messages.newsletter)}
|
||||
name='accepts_email_list'
|
||||
onChange={onCheckboxChange}
|
||||
checked={params.get('accepts_email_list', false)}
|
||||
/>}
|
||||
</div>
|
||||
<div className='actions'>
|
||||
<button name='button' type='submit' className='btn button button-primary'>
|
||||
<FormattedMessage id='registration.sign_up' defaultMessage='Sign up' />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</fieldset>
|
||||
</SimpleForm>
|
||||
);
|
||||
};
|
||||
|
||||
export default RegistrationForm;
|
|
@ -85,6 +85,10 @@ interface ISimpleInput {
|
|||
name?: string,
|
||||
placeholder?: string,
|
||||
value?: string | number,
|
||||
autoComplete?: string,
|
||||
autoCorrect?: string,
|
||||
autoCapitalize?: string,
|
||||
required?: boolean,
|
||||
}
|
||||
|
||||
export const SimpleInput: React.FC<ISimpleInput> = (props) => {
|
||||
|
@ -104,6 +108,9 @@ interface ISimpleTextarea {
|
|||
value?: string,
|
||||
onChange?: React.ChangeEventHandler<HTMLTextAreaElement>,
|
||||
rows?: number,
|
||||
name?: string,
|
||||
maxLength?: number,
|
||||
required?: boolean,
|
||||
}
|
||||
|
||||
export const SimpleTextarea: React.FC<ISimpleTextarea> = (props) => {
|
||||
|
@ -161,6 +168,7 @@ interface ICheckbox {
|
|||
name?: string,
|
||||
checked?: boolean,
|
||||
onChange?: React.ChangeEventHandler<HTMLInputElement>,
|
||||
required?: boolean,
|
||||
}
|
||||
|
||||
export const Checkbox: React.FC<ICheckbox> = (props) => (
|
||||
|
@ -240,8 +248,15 @@ interface ITextInput {
|
|||
name?: string,
|
||||
onChange?: React.ChangeEventHandler,
|
||||
label?: React.ReactNode,
|
||||
hint?: React.ReactNode,
|
||||
placeholder?: string,
|
||||
value?: string,
|
||||
autoComplete?: string,
|
||||
autoCorrect?: string,
|
||||
autoCapitalize?: string,
|
||||
pattern?: string,
|
||||
error?: boolean,
|
||||
required?: boolean,
|
||||
}
|
||||
|
||||
export const TextInput: React.FC<ITextInput> = props => (
|
||||
|
|
Loading…
Reference in a new issue