Skip to content

Commit

Permalink
Move username profanity validation func into yup schema
Browse files Browse the repository at this point in the history
  • Loading branch information
SamIvan-ark committed Jan 13, 2024
1 parent 79249b9 commit 96a59ed
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions frontend/src/components/SignupForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const SignupForm = () => {
const { t } = useTranslation();
const auth = hooks.useAuth();
const [authFailed, setAuthFailed] = useState(false);
const [isUsernameProfanity, setIsUsernameProfanity] = useState(false);
const inputRef = useRef();
const navigate = useNavigate();

Expand All @@ -23,7 +22,8 @@ const SignupForm = () => {
.string()
.min(3, t('errors.lengthFromTo', { from: 3, to: 20 }))
.max(20, t('errors.lengthFromTo', { from: 3, to: 20 }))
.required(t('errors.emptyField')),
.required(t('errors.emptyField'))
.test('not profanity', t('errors.profanityUsername'), (value) => value === filterProfanity(value)),
password: yup
.string()
.min(6, t('errors.minLength', { length: 6 }))
Expand All @@ -39,14 +39,6 @@ const SignupForm = () => {
validationSchema,
onSubmit: async (values) => {
setAuthFailed(false);
setIsUsernameProfanity(false);
if (values.username !== filterProfanity(values.username)) {
setIsUsernameProfanity(true);
formik.setErrors({ username: t('errors.profanityUsername') });
inputRef.current.select();
formik.setSubmitting(false);
return;
}
try {
const { data: authData } = await sendCredentials(serverRoutes.signupPath(), values);
auth.logIn(authData);
Expand Down Expand Up @@ -81,10 +73,10 @@ const SignupForm = () => {
placeholder={t('credentials.username')}
onChange={formik.handleChange}
value={formik.values.username}
isInvalid={(!!formik.errors.username && formik.touched.username) || isUsernameProfanity}
isInvalid={(!!formik.errors.username && formik.touched.username)}
/>
<Form.Label htmlFor="username">{t('credentials.username')}</Form.Label>
{(formik.errors.username && formik.touched.username) || isUsernameProfanity
{(formik.errors.username && formik.touched.username)
? <Form.Control.Feedback type="invalid" tooltip>{formik.errors.username}</Form.Control.Feedback>
: null}
</Form.Group>
Expand Down

0 comments on commit 96a59ed

Please sign in to comment.