Skip to content

Commit

Permalink
Prevent form submission while uploading
Browse files Browse the repository at this point in the history
  • Loading branch information
TenType committed Oct 6, 2024
1 parent 8f811ce commit 828b27b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
17 changes: 16 additions & 1 deletion src/components/UploadButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,16 @@ export default function UploadButton({
);
return;
}
setState((prevState) => ({ ...prevState, value: "Uploading" }));

const accessToken = await getAccessToken();
const googleDrive = new GDrive();
googleDrive.accessToken = accessToken;
googleDrive.fetchTimeout = 20000; // 20 seconds

const file = await selectFile();
if (file == null) {
setState((prevState) => ({ ...prevState, value: null }));
return;
}
const fileData = await fs.readFile(file.uri, "base64");
Expand Down Expand Up @@ -146,14 +149,26 @@ export default function UploadButton({
alertError("Error uploading file: " + error);
}
setFileName("Upload failed");
setState((prevState) => ({ ...prevState, value: null }));
return;
}

if (file.size > 104857600) {
// 100 MB
Alert.alert(
"File size too large",
`Your file ${file.name} has a size of ${file.size} bytes. Please compress your file and upload a smaller version.`,
);
setFileName("Upload too large");
setState((prevState) => ({ ...prevState, value: null }));
return;
}

setFileName(file.name);

setState((prevState) => ({
...prevState,
value: [`https://drive.google.com/open?id=${id}`, file.size],
value: `https://drive.google.com/open?id=${id}`,
}));
}}
>
Expand Down
10 changes: 2 additions & 8 deletions src/utils/forms/LibraryMusicHour.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,7 @@ export default class LibraryMusicHour extends Form {
navigation={this.navigation}
/>
),
// Only PDF files can be uploaded
// Optional
validate: () =>
this.pianoAccompaniment[0].value == null ||
this.pianoAccompaniment[0].value[1] <= 104857600, // There are 104,857,600 bytes in 100 MB
validate: () => this.pianoAccompaniment[0].value != "Uploading",
}),

this.title == "Library Music Hour"
Expand All @@ -285,11 +281,9 @@ export default class LibraryMusicHour extends Form {
isVisible: () =>
this.performanceType[0].value?.includes("Ensemble"),

// Only PDF files can be uploaded
// Required only if visible (selected ensemble option)
validate: () =>
this.ensembleProfile[0].value != null &&
this.ensembleProfile[0].value[1] <= 104857600, // There are 104,857,600 bytes in 100 MB
this.ensembleProfile[0].value != "Uploading",
})
: null,

Expand Down

0 comments on commit 828b27b

Please sign in to comment.