Skip to content

Commit

Permalink
fix: wait for both upload and selection
Browse files Browse the repository at this point in the history
issue #177
  • Loading branch information
C4illin committed Nov 21, 2024
1 parent f04fe76 commit 4c05fd7
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions public/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const dropZone = document.getElementById("dropzone");
const convertButton = document.querySelector("input[type='submit']");
const fileNames = [];
let fileType;
let pendingFiles = 0;
let formatSelected = false;

dropZone.addEventListener("dragover", () => {
dropZone.classList.add("dragover");
Expand Down Expand Up @@ -62,7 +64,10 @@ const updateSearchBar = () => {
target.onmousedown = () => {
convertToElement.value = target.dataset.value;
convertToInput.value = `${target.dataset.target} using ${target.dataset.converter}`;
convertButton.disabled = false;
formatSelected = true;
if (pendingFiles === 0 && fileNames.length > 0) {
convertButton.disabled = false;
}
showMatching("");
};
}
Expand All @@ -77,6 +82,7 @@ const updateSearchBar = () => {
convertToInput.addEventListener("search", () => {
// when the user clears the search bar using the 'x' button
convertButton.disabled = true;
formatSelected = false;
});

convertToInput.addEventListener("blur", (e) => {
Expand Down Expand Up @@ -170,10 +176,14 @@ const deleteRow = (target) => {
const index = fileNames.indexOf(filename);
fileNames.splice(index, 1);

// reset fileInput
fileInput.value = "";

// if fileNames is empty, reset fileType
if (fileNames.length === 0) {
fileType = null;
fileInput.removeAttribute("accept");
convertButton.disabled = true;
setTitle();
}

Expand All @@ -184,16 +194,13 @@ const deleteRow = (target) => {
"Content-Type": "application/json",
},
})
.then((res) => res.json())
.then((data) => {
console.log(data);
})
.catch((err) => console.log(err));
};

const uploadFiles = (files) => {
convertButton.disabled = true;
convertButton.textContent = "Uploading...";
pendingFiles += 1;

const formData = new FormData();

Expand All @@ -207,8 +214,13 @@ const uploadFiles = (files) => {
})
.then((res) => res.json())
.then((data) => {
convertButton.disabled = false;
convertButton.textContent = "Convert";
pendingFiles -= 1;
if (pendingFiles === 0) {
if (formatSelected) {
convertButton.disabled = false;
}
convertButton.textContent = "Convert";
}
console.log(data);
})
.catch((err) => console.log(err));
Expand Down

0 comments on commit 4c05fd7

Please sign in to comment.