Apply new ValidationCheckmark component to Registration
This commit is contained in:
parent
4fc43afe1b
commit
cf128d70b4
|
@ -64,4 +64,21 @@ describe('<Registration />', () => {
|
||||||
expect(screen.getByTestId('toast')).toHaveTextContent(/failed to register your account/i);
|
expect(screen.getByTestId('toast')).toHaveTextContent(/failed to register your account/i);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('validations', () => {
|
||||||
|
it('should undisable button with valid password', async() => {
|
||||||
|
render(<Registration />);
|
||||||
|
|
||||||
|
expect(screen.getByTestId('button')).toBeDisabled();
|
||||||
|
fireEvent.change(screen.getByTestId('password-input'), { target: { value: 'Password' } });
|
||||||
|
expect(screen.getByTestId('button')).not.toBeDisabled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should disable button with invalid password', async() => {
|
||||||
|
render(<Registration />);
|
||||||
|
|
||||||
|
fireEvent.change(screen.getByTestId('password-input'), { target: { value: 'Passwor' } });
|
||||||
|
expect(screen.getByTestId('button')).toBeDisabled();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import { AxiosError } from 'axios';
|
import { AxiosError } from 'axios';
|
||||||
import classNames from 'classnames';
|
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
|
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
|
||||||
import { useDispatch } from 'react-redux';
|
import { useDispatch } from 'react-redux';
|
||||||
|
@ -10,7 +9,8 @@ import { fetchInstance } from 'soapbox/actions/instance';
|
||||||
import { startOnboarding } from 'soapbox/actions/onboarding';
|
import { startOnboarding } from 'soapbox/actions/onboarding';
|
||||||
import snackbar from 'soapbox/actions/snackbar';
|
import snackbar from 'soapbox/actions/snackbar';
|
||||||
import { createAccount, removeStoredVerification } from 'soapbox/actions/verification';
|
import { createAccount, removeStoredVerification } from 'soapbox/actions/verification';
|
||||||
import { Button, Form, FormGroup, HStack, Icon, Input, Stack, Text } from 'soapbox/components/ui';
|
import { Button, Form, FormGroup, Input, Stack } from 'soapbox/components/ui';
|
||||||
|
import ValidationCheckmark from 'soapbox/components/validation-checkmark';
|
||||||
import { useAppSelector } from 'soapbox/hooks';
|
import { useAppSelector } from 'soapbox/hooks';
|
||||||
import { getRedirectUrl } from 'soapbox/utils/redirect';
|
import { getRedirectUrl } from 'soapbox/utils/redirect';
|
||||||
|
|
||||||
|
@ -27,6 +27,18 @@ const messages = defineMessages({
|
||||||
id: 'registrations.error',
|
id: 'registrations.error',
|
||||||
defaultMessage: 'Failed to register your account.',
|
defaultMessage: 'Failed to register your account.',
|
||||||
},
|
},
|
||||||
|
minimumCharacters: {
|
||||||
|
id: 'registration.validation.minimum_characters',
|
||||||
|
defaultMessage: '8 characters',
|
||||||
|
},
|
||||||
|
capitalLetter: {
|
||||||
|
id: 'registration.validation.capital_letter',
|
||||||
|
defaultMessage: '1 capital letter',
|
||||||
|
},
|
||||||
|
lowercaseLetter: {
|
||||||
|
id: 'registration.validation.lowercase_letter',
|
||||||
|
defaultMessage: '1 lowercase letter',
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
|
@ -34,6 +46,19 @@ const initialState = {
|
||||||
password: '',
|
password: '',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const hasUppercaseCharacter = (string: string) => {
|
||||||
|
for (let i = 0; i < string.length; i++) {
|
||||||
|
if (string.charAt(i) === string.charAt(i).toUpperCase() && string.charAt(i).match(/[a-z]/i)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
const hasLowercaseCharacter = (string: string) => {
|
||||||
|
return string.toUpperCase() !== string;
|
||||||
|
};
|
||||||
|
|
||||||
const Registration = () => {
|
const Registration = () => {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
|
@ -46,11 +71,13 @@ const Registration = () => {
|
||||||
const { username, password } = state;
|
const { username, password } = state;
|
||||||
|
|
||||||
const meetsLengthRequirements = React.useMemo(() => password.length >= 8, [password]);
|
const meetsLengthRequirements = React.useMemo(() => password.length >= 8, [password]);
|
||||||
|
const meetsCapitalLetterRequirements = React.useMemo(() => hasUppercaseCharacter(password), [password]);
|
||||||
|
const meetsLowercaseLetterRequirements = React.useMemo(() => hasLowercaseCharacter(password), [password]);
|
||||||
|
const hasValidPassword = meetsLengthRequirements && meetsCapitalLetterRequirements && meetsLowercaseLetterRequirements;
|
||||||
|
|
||||||
const handleSubmit = React.useCallback((event) => {
|
const handleSubmit = React.useCallback((event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
// TODO: handle validation errors from Pepe
|
|
||||||
dispatch(createAccount(username, password))
|
dispatch(createAccount(username, password))
|
||||||
.then(() => dispatch(logIn(intl, username, password)))
|
.then(() => dispatch(logIn(intl, username, password)))
|
||||||
.then(({ access_token }: any) => dispatch(verifyCredentials(access_token)))
|
.then(({ access_token }: any) => dispatch(verifyCredentials(access_token)))
|
||||||
|
@ -121,26 +148,36 @@ const Registration = () => {
|
||||||
value={password}
|
value={password}
|
||||||
onChange={handleInputChange}
|
onChange={handleInputChange}
|
||||||
required
|
required
|
||||||
|
data-testid='password-input'
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Stack className='mt-2'>
|
<Stack className='mt-2' space={1}>
|
||||||
<HStack alignItems='center' space={2}>
|
<ValidationCheckmark
|
||||||
<Icon
|
isValid={meetsLengthRequirements}
|
||||||
src={require('@tabler/icons/icons/check.svg')}
|
text={intl.formatMessage(messages.minimumCharacters)}
|
||||||
className={classNames({
|
|
||||||
'w-4 h-4': true,
|
|
||||||
'text-gray-500': !meetsLengthRequirements,
|
|
||||||
'text-success-600': meetsLengthRequirements,
|
|
||||||
})}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Text theme='muted' size='sm'>8 characters</Text>
|
<ValidationCheckmark
|
||||||
</HStack>
|
isValid={meetsCapitalLetterRequirements}
|
||||||
|
text={intl.formatMessage(messages.capitalLetter)}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<ValidationCheckmark
|
||||||
|
isValid={meetsLowercaseLetterRequirements}
|
||||||
|
text={intl.formatMessage(messages.lowercaseLetter)}
|
||||||
|
/>
|
||||||
</Stack>
|
</Stack>
|
||||||
</FormGroup>
|
</FormGroup>
|
||||||
|
|
||||||
<div className='text-center'>
|
<div className='text-center'>
|
||||||
<Button block theme='primary' type='submit' disabled={isLoading}>Register</Button>
|
<Button
|
||||||
|
block
|
||||||
|
theme='primary'
|
||||||
|
type='submit'
|
||||||
|
disabled={isLoading || !hasValidPassword}
|
||||||
|
>
|
||||||
|
Register
|
||||||
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</Form>
|
</Form>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -833,6 +833,9 @@
|
||||||
"registration.sign_up": "Sign up",
|
"registration.sign_up": "Sign up",
|
||||||
"registration.tos": "Terms of Service",
|
"registration.tos": "Terms of Service",
|
||||||
"registration.username_unavailable": "Username is already taken.",
|
"registration.username_unavailable": "Username is already taken.",
|
||||||
|
"registration.validation.minimum_characters": "8 characters",
|
||||||
|
"registration.validation.capital_letter": "1 capital letter",
|
||||||
|
"registration.validation.lowercase_letter": "1 lowercase letter",
|
||||||
"relative_time.days": "{number}d",
|
"relative_time.days": "{number}d",
|
||||||
"relative_time.hours": "{number}h",
|
"relative_time.hours": "{number}h",
|
||||||
"relative_time.just_now": "now",
|
"relative_time.just_now": "now",
|
||||||
|
|
Loading…
Reference in New Issue