From 1d47e191e8cdd668ee9004b58d8834359dc0e47a Mon Sep 17 00:00:00 2001 From: Michal Masrna <38047051+michalmasrna1@users.noreply.github.com> Date: Sat, 11 Nov 2023 21:33:54 +0100 Subject: [PATCH] RegisterForm already used email address error (#180) --- src/components/RegisterForm/RegisterForm.tsx | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/components/RegisterForm/RegisterForm.tsx b/src/components/RegisterForm/RegisterForm.tsx index 90bdc2fc..3e826f27 100644 --- a/src/components/RegisterForm/RegisterForm.tsx +++ b/src/components/RegisterForm/RegisterForm.tsx @@ -1,5 +1,5 @@ import {useMutation} from '@tanstack/react-query' -import axios from 'axios' +import axios, {AxiosError} from 'axios' import {useRouter} from 'next/router' import {FC} from 'react' import {SubmitHandler, useForm} from 'react-hook-form' @@ -24,6 +24,10 @@ interface RegisterFormValues extends SchoolSubFormValues { gdpr?: boolean } +interface RegisterErrorResponseData { + email?: string[] +} + const defaultValues: RegisterFormValues = { email: '', password1: '', @@ -41,7 +45,7 @@ const defaultValues: RegisterFormValues = { } export const RegisterForm: FC = () => { - const {handleSubmit, control, watch, setValue, getValues} = useForm({ + const {handleSubmit, control, watch, setValue, getValues, setError} = useForm({ defaultValues, values: defaultValues, }) @@ -78,6 +82,15 @@ export const RegisterForm: FC = () => { return axios.post(`/api/user/registration?seminar=${seminar}`, transformFormData(data)) }, onSuccess: () => router.push(`${router.asPath}/../verifikacia`), + onError: (error: AxiosError) => { + if (error.response?.status === 400) { + if (error.response.data.email) { + setError('email', {type: 'custom', message: `* ${error.response.data.email[0]}`}) + } + } else { + alert('Neznáma chyba pri registrácii. Skúste to prosím neskôr.') + } + }, }) const onSubmit: SubmitHandler = (data) => {