Skip to content

Commit

Permalink
fix: ошибка 500 при сабмите формы отправки пьесы (#488)
Browse files Browse the repository at this point in the history
* fix: ошибка 500 при сабмите формы отправки пьесы

* fix: правки после ревью
  • Loading branch information
Aleksey-dev-crt authored Sep 20, 2023
1 parent 25db681 commit 99ca97b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
33 changes: 22 additions & 11 deletions src/pages/form/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,17 @@ const Participation = () => {

const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
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
Expand All @@ -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 (
<AppLayout>
<SEO
Expand All @@ -194,7 +204,7 @@ const Participation = () => {
value={form.values.firstName}
placeholder="Имя"
errorText={form.touched.firstName ? form.errors.firstName : ''}
onChange={(value) => form.setFieldValue('firstName', value)}
onChange={(value) => handleInput('firstName', value)}
/>
</FormField>
</Form.Field>
Expand All @@ -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)}
/>
</FormField>
</Form.Field>
Expand All @@ -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)}
/>
</FormField>
</Form.Field>
Expand All @@ -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)}
/>
</FormField>
</Form.Field>
Expand All @@ -246,6 +256,7 @@ const Participation = () => {
<PhoneNumberInput
value={form.values.phoneNumber}
errorText={form.touched.phoneNumber ? form.errors.phoneNumber : ''}
onChange={(value) => handleInput('phoneNumber', value)}
/>
</FormField>
</Form.Field>
Expand All @@ -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)}
/>
</FormField>
</Form.Field>
Expand All @@ -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)}
/>
</FormField>
</Form.Field>
Expand All @@ -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)}
/>
</FormField>
</Form.Field>
Expand All @@ -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)}
/>
</FormField>
</Form.Field>
Expand Down
16 changes: 9 additions & 7 deletions src/services/fetcher/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ const fetchResource = (httpClient: typeof fetch) => async <T>(path: string, opti
export const fetcher = fetchResource(fetch);

async function handleResponse<T>(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;
}

0 comments on commit 99ca97b

Please sign in to comment.