Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ошибка 500 при сабмите формы отправки пьесы #488

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}