Skip to content

Commit

Permalink
feat: show error if number of uploaded files is different from the re…
Browse files Browse the repository at this point in the history
…sponse size

refactor: use for loops instead of foreach everywhere
closes #73
  • Loading branch information
harshkhandeparkar committed Jun 29, 2024
1 parent 17cfa5e commit e26eb4c
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions frontend/src/pages/UploadPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const UploadPage: Component = () => {
if (!awaitingResponse()) {
try {
const formData = new FormData();

const numPapers = qPapers().length;
for (const qp of qPapers()) {
const {
file,
Expand All @@ -152,8 +152,7 @@ const UploadPage: Component = () => {
`${course_code}_${course_name}_${year}_${exam}_${semester}`
);
}

toast(`Uploading ${qPapers().length} file${qPapers().length > 1 ? 's' : ''}.`);
toast(`Uploading ${numPapers} file${numPapers > 1 ? 's' : ''}.`);

setAwaitingResponse(true);
const response = await fetch(
Expand All @@ -163,9 +162,9 @@ const UploadPage: Component = () => {
body: formData
}
);
const data: UploadResults = await response.json();
const upload_results: UploadResults = await response.json();

data.forEach((result) => {
for (const result of upload_results) {
if (result.status === "success") {
toast.success(
`File ${result.filename} uploaded successfully`
Expand All @@ -175,7 +174,12 @@ const UploadPage: Component = () => {
`Failed to upload file ${result.filename}: ${result.description}`
);
}
});
}

if (upload_results.length < numPapers) {
const failedPapers = numPapers - upload_results.length;
toast.error(`${failedPapers} paper${failedPapers > 1 ? 's' : ''} failed to upload.`)
}

clearQPapers();
setAwaitingResponse(false);
Expand Down

0 comments on commit e26eb4c

Please sign in to comment.