Skip to content

Commit

Permalink
RegisterForm already used email address error (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
michalmasrna1 authored Nov 11, 2023
1 parent 33685f8 commit 1d47e19
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/components/RegisterForm/RegisterForm.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -24,6 +24,10 @@ interface RegisterFormValues extends SchoolSubFormValues {
gdpr?: boolean
}

interface RegisterErrorResponseData {
email?: string[]
}

const defaultValues: RegisterFormValues = {
email: '',
password1: '',
Expand All @@ -41,7 +45,7 @@ const defaultValues: RegisterFormValues = {
}

export const RegisterForm: FC = () => {
const {handleSubmit, control, watch, setValue, getValues} = useForm<RegisterFormValues>({
const {handleSubmit, control, watch, setValue, getValues, setError} = useForm<RegisterFormValues>({
defaultValues,
values: defaultValues,
})
Expand Down Expand Up @@ -78,6 +82,15 @@ export const RegisterForm: FC = () => {
return axios.post<IGeneralPostResponse>(`/api/user/registration?seminar=${seminar}`, transformFormData(data))
},
onSuccess: () => router.push(`${router.asPath}/../verifikacia`),
onError: (error: AxiosError<RegisterErrorResponseData>) => {
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<RegisterFormValues> = (data) => {
Expand Down

0 comments on commit 1d47e19

Please sign in to comment.