Fix file clear for errors (#2302)

* Prevent file input from being removed when an error occurs

* Fix a bug preventing fetch when 'Bored waiting' btn isn't present
This commit is contained in:
Rafael Encinas 2024-11-22 10:11:23 -07:00 committed by GitHub
parent 61bccd1d8b
commit 543ad083a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -39,7 +39,9 @@
const originalButtonText = submitButton.textContent; const originalButtonText = submitButton.textContent;
var boredWaiting = localStorage.getItem("boredWaiting") || "disabled"; var boredWaiting = localStorage.getItem("boredWaiting") || "disabled";
showGameBtn.style.display = "none"; if (showGameBtn) {
showGameBtn.style.display = "none";
}
// Remove empty file entries // Remove empty file entries
for (let [key, value] of formData.entries()) { for (let [key, value] of formData.entries()) {
@ -73,8 +75,10 @@
clearFileInput(); clearFileInput();
clearTimeout(timeoutId); clearTimeout(timeoutId);
showGameBtn.style.display = "none"; if (showGameBtn) {
showGameBtn.style.marginTop = ""; showGameBtn.style.display = "none";
showGameBtn.style.marginTop = "";
}
submitButton.textContent = originalButtonText; submitButton.textContent = originalButtonText;
submitButton.disabled = false; submitButton.disabled = false;
@ -95,7 +99,6 @@
} }
} catch (error) { } catch (error) {
clearFileInput();
clearTimeout(timeoutId); clearTimeout(timeoutId);
showGameBtn.style.display = "none"; showGameBtn.style.display = "none";
submitButton.textContent = originalButtonText; submitButton.textContent = originalButtonText;
@ -117,13 +120,13 @@
return null; return null;
} }
} }
async function handleSingleDownload(url, formData, isMulti = false, isZip = false) { async function handleSingleDownload(url, formData, isMulti = false, isZip = false) {
const startTime = performance.now(); const startTime = performance.now();
const file = formData.get('fileInput'); const file = formData.get('fileInput');
let success = false; let success = false;
let errorMessage = null; let errorMessage = null;
try { try {
const response = await fetch(url, { method: "POST", body: formData }); const response = await fetch(url, { method: "POST", body: formData });
const contentType = response.headers.get("content-type"); const contentType = response.headers.get("content-type");
@ -146,7 +149,7 @@
const blob = await response.blob(); const blob = await response.blob();
success = true; success = true;
if (contentType.includes("application/pdf") || contentType.includes("image/")) { if (contentType.includes("application/pdf") || contentType.includes("image/")) {
clearFileInput(); clearFileInput();
return handleResponse(blob, filename, !isMulti, isZip); return handleResponse(blob, filename, !isMulti, isZip);
@ -158,12 +161,11 @@
} catch (error) { } catch (error) {
success = false; success = false;
errorMessage = error.message; errorMessage = error.message;
clearFileInput();
console.error("Error in handleSingleDownload:", error); console.error("Error in handleSingleDownload:", error);
throw error; throw error;
} finally { } finally {
const processingTime = performance.now() - startTime; const processingTime = performance.now() - startTime;
// Capture analytics // Capture analytics
const pageCount = file && file.type === 'application/pdf' ? await getPDFPageCount(file) : null; const pageCount = file && file.type === 'application/pdf' ? await getPDFPageCount(file) : null;
if(analyticsEnabled) { if(analyticsEnabled) {
@ -191,7 +193,7 @@
return filename; return filename;
} }
async function handleJsonResponse(response) { async function handleJsonResponse(response) {
const json = await response.json(); const json = await response.json();
const errorMessage = JSON.stringify(json, null, 2); const errorMessage = JSON.stringify(json, null, 2);