diff --git a/apps/web/api/ius/query.ts b/apps/web/api/ius/query.ts index 3ff5919..14315bb 100644 --- a/apps/web/api/ius/query.ts +++ b/apps/web/api/ius/query.ts @@ -86,7 +86,7 @@ export async function getDocuments({ pageUrlParams }: Input) { .then((res) => res); } -export async function uploadDocument({ +export function uploadDocument({ pageUrlParams, document, formData, @@ -100,5 +100,15 @@ export async function uploadDocument({ return fetch(urls.URL_IUS + url, { body: formData, method: 'POST', - }); + }) + .then((res) => { + if (res.ok) { + return true; + } + + return res.json(); + }) + .catch((error) => { + throw error as t.HttpError; + }); } diff --git a/apps/web/components/Form/Documents/Buttons.tsx b/apps/web/components/Form/Documents/Buttons.tsx index 219e6f9..ac67c81 100644 --- a/apps/web/components/Form/Documents/Buttons.tsx +++ b/apps/web/components/Form/Documents/Buttons.tsx @@ -9,6 +9,7 @@ import { pick } from 'radash'; import { useCallback, useContext } from 'react'; const ERROR_UPLOAD_DOCUMENT = 'Произошла ошибка при загрузке документов'; +const SUCCESS_UPLOAD_DOCUMENTS = 'Файлы успешно загружены'; export function Buttons() { const { resetValidation } = useFormStore(); @@ -30,17 +31,31 @@ export function Buttons() { }); return Promise.all(uploadFiles) - .then(() => { - setFormState({ status: 'success' }); - setTimeout(() => { - window.location.reload(); - }, 500); + .then(async (res) => { + const errors = res.filter((x) => typeof x !== 'boolean') as HttpError[]; + + if (!errors.length) { + setFormState({ + status: 'success', + text: SUCCESS_UPLOAD_DOCUMENTS, + }); + + setTimeout(() => { + window.location.reload(); + }, 500); + + return; + } + + const error = errors.find((x) => x.errors.length)?.errors.at(0) || ERROR_UPLOAD_DOCUMENT; + + setFormState({ status: 'error', text: error }); }) .catch((error) => { - const _error = error as HttpError; - const text = _error?.errors?.at(0) || _error.title || ERROR_UPLOAD_DOCUMENT; - - setFormState({ status: 'error', text }); + setFormState({ + status: 'error', + text: error ? JSON.stringify(error) : ERROR_UPLOAD_DOCUMENT, + }); }); }, [formFiles, pageUrlParams, resetValidation, setFormState]);