diff --git a/src/pages/form/form.tsx b/src/pages/form/form.tsx index 00bbbce7f..a6506e178 100644 --- a/src/pages/form/form.tsx +++ b/src/pages/form/form.tsx @@ -152,16 +152,17 @@ const Participation = () => { const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); - + if (!canSubmit) { + return; + } try { await postParticipation(form.values); + router.push('/form/success'); } catch (error) { handleSubmitError(error); return; } - - router.push('/form/success'); }; const canSubmit = !form.nonFieldError @@ -174,6 +175,15 @@ const Participation = () => { ); } + const handleInput = ( + input: keyof ParticipationFormFields, + value: ParticipationFormFields[keyof ParticipationFormFields] + ) => { + return input === 'phoneNumber' + ? form.setFieldValue(input, '+7' + value) + : form.setFieldValue(input, value); + }; + return ( { value={form.values.firstName} placeholder="Имя" errorText={form.touched.firstName ? form.errors.firstName : ''} - onChange={(value) => form.setFieldValue('firstName', value)} + onChange={(value) => handleInput('firstName', value)} /> @@ -207,7 +217,7 @@ const Participation = () => { value={form.values.lastName} placeholder="Фамилия" errorText={form.touched.lastName ? form.errors.lastName : ''} - onChange={(value) => form.setFieldValue('lastName', value)} + onChange={(value) => handleInput('lastName', value)} /> @@ -221,7 +231,7 @@ const Participation = () => { placeholder="Год рождения" mask="9999" errorText={form.touched.birthYear ? form.errors.birthYear : ''} - onChange={(value) => form.setFieldValue('birthYear', value)} + onChange={(value) => handleInput('birthYear', value)} /> @@ -234,7 +244,7 @@ const Participation = () => { value={form.values.city} placeholder="Город проживания" errorText={form.touched.city ? form.errors.city : ''} - onChange={(value) => form.setFieldValue('city', value)} + onChange={(value) => handleInput('city', value)} /> @@ -246,6 +256,7 @@ const Participation = () => { handleInput('phoneNumber', value)} /> @@ -259,7 +270,7 @@ const Participation = () => { value={form.values.email} placeholder="E-mail" errorText={form.touched.email ? form.errors.email : ''} - onChange={(value) => form.setFieldValue('email', value)} + onChange={(value) => handleInput('email', value)} /> @@ -274,7 +285,7 @@ const Participation = () => { value={form.values.title} placeholder="Название" errorText={form.touched.title ? form.errors.title : ''} - onChange={(value) => form.setFieldValue('title', value)} + onChange={(value) => handleInput('title', value)} /> @@ -288,7 +299,7 @@ const Participation = () => { placeholder="Год написания" mask="9999" errorText={form.touched.year ? form.errors.year : ''} - onChange={(value) => form.setFieldValue('year', value)} + onChange={(value) => handleInput('year', value)} /> @@ -301,7 +312,7 @@ const Participation = () => { accept={ACCEPTABLE_FILE_TYPES} fileName={form.values.file?.name} errorText={form.touched.file ? form.errors.file : ''} - onChange={(value) => form.setFieldValue('file', value)} + onChange={(value) => handleInput('file', value)} /> diff --git a/src/services/fetcher/fetcher.ts b/src/services/fetcher/fetcher.ts index f9ea19991..1d084d372 100644 --- a/src/services/fetcher/fetcher.ts +++ b/src/services/fetcher/fetcher.ts @@ -27,14 +27,16 @@ const fetchResource = (httpClient: typeof fetch) => async (path: string, opti export const fetcher = fetchResource(fetch); async function handleResponse(response: Response) { + let payload; try { - const payload = await response.json(); - if (!response.ok) { - throw new HttpRequestError({ statusCode: response.status, payload }); - } - - return payload as T; + payload = await response.json(); } catch (err) { - return err as T; + payload = {}; + } + + if (!response.ok) { + throw new HttpRequestError({ statusCode: response.status, payload }); } + + return payload as T; }