From 45ea4217245b331a23a3aaaa95c26a0569932317 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Thu, 21 Sep 2023 03:21:19 -0400 Subject: [PATCH] fix(striker-ui): prepare handle error in add file form --- striker-ui/components/Files/AddFileForm.tsx | 74 ++++++++++++--------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/striker-ui/components/Files/AddFileForm.tsx b/striker-ui/components/Files/AddFileForm.tsx index 22c9eb35..5ca20a94 100644 --- a/striker-ui/components/Files/AddFileForm.tsx +++ b/striker-ui/components/Files/AddFileForm.tsx @@ -60,43 +60,53 @@ const AddFileForm: FC = (props) => { }, {}), ); - files.forEach(({ file, name, uuid }) => { - if (!file) return; + const promises = files.reduce[]>( + (chain, { file, name, uuid }) => { + if (!file) return chain; - const data = new FormData(); + const data = new FormData(); - data.append('file', new File([file], name, { ...file })); + data.append('file', new File([file], name, { ...file })); - api - .post('/file', data, { - headers: { - 'Content-Type': 'multipart/form-data', - }, - onUploadProgress: ( - (fileUuid: string) => - ({ loaded, total }) => { - setUploads((previous) => - setUploadProgress( - previous, - fileUuid, - Math.round( - (loaded / total) * REQUEST_INCOMPLETE_UPLOAD_LIMIT, + const promise = api + .post('/file', data, { + headers: { + 'Content-Type': 'multipart/form-data', + }, + onUploadProgress: ( + (fileUuid: string) => + ({ loaded, total }) => { + setUploads((previous) => + setUploadProgress( + previous, + fileUuid, + Math.round( + (loaded / total) * REQUEST_INCOMPLETE_UPLOAD_LIMIT, + ), ), - ), + ); + } + )(uuid), + }) + .then( + ((fileUuid: string) => () => { + setUploads((previous) => + setUploadProgress(previous, fileUuid, 100), ); - } - )(uuid), - }) - .then( - ((fileUuid: string) => () => { - setUploads((previous) => - setUploadProgress(previous, fileUuid, 100), - ); - })(uuid), - ) - .catch((error) => { - handleAPIError(error); - }); + })(uuid), + ); + + chain.push(promise); + + return chain; + }, + [], + ); + + Promise.all(promises).catch((error) => { + const emsg = handleAPIError(error); + + emsg.children = <>Failed to add file. {emsg.children}; }); }, validationSchema: fileListSchema,