fix handle disable button when upload business industries
This commit is contained in:
@@ -5,9 +5,9 @@ Dropzone.autoDiscover = false;
|
|||||||
var previewTemplate,
|
var previewTemplate,
|
||||||
dropzone,
|
dropzone,
|
||||||
dropzonePreviewNode = document.querySelector("#dropzone-preview-list");
|
dropzonePreviewNode = document.querySelector("#dropzone-preview-list");
|
||||||
console.log(previewTemplate);
|
|
||||||
console.log(dropzone);
|
const uploadButton = document.getElementById("btnUploadBusinessIndustry");
|
||||||
console.log(dropzonePreviewNode);
|
const spinner = document.getElementById("spinner");
|
||||||
|
|
||||||
const toastNotification = document.getElementById("toastNotification");
|
const toastNotification = document.getElementById("toastNotification");
|
||||||
const toast = new bootstrap.Toast(toastNotification);
|
const toast = new bootstrap.Toast(toastNotification);
|
||||||
@@ -29,97 +29,50 @@ const toast = new bootstrap.Toast(toastNotification);
|
|||||||
.getAttribute("content")}`,
|
.getAttribute("content")}`,
|
||||||
},
|
},
|
||||||
init: function () {
|
init: function () {
|
||||||
// Listen for the success event
|
|
||||||
this.on("success", function (file, response) {
|
this.on("success", function (file, response) {
|
||||||
console.log("File successfully uploaded:", file);
|
|
||||||
console.log("API Response:", response);
|
|
||||||
|
|
||||||
// Show success toast
|
|
||||||
document.getElementById("toast-message").innerText =
|
document.getElementById("toast-message").innerText =
|
||||||
response.message;
|
response.message;
|
||||||
toast.show();
|
toast.show();
|
||||||
document.getElementById("submit-upload").innerHTML =
|
|
||||||
"Upload Files";
|
|
||||||
// Tunggu sebentar lalu reload halaman
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
window.location.href = "/data/business-industries";
|
window.location.href = "/data/business-industries";
|
||||||
}, 2000);
|
}, 2000);
|
||||||
});
|
});
|
||||||
// Listen for the error event
|
|
||||||
this.on("error", function (file, errorMessage) {
|
this.on("error", function (file, errorMessage) {
|
||||||
console.error("Error uploading file:", file);
|
console.error("Error uploading file:", file);
|
||||||
console.error("Error message:", errorMessage);
|
console.error("Error message:", errorMessage);
|
||||||
// Handle the error response
|
|
||||||
|
|
||||||
// Show error toast
|
|
||||||
document.getElementById("toast-message").innerText =
|
document.getElementById("toast-message").innerText =
|
||||||
errorMessage.message;
|
errorMessage.message;
|
||||||
toast.show();
|
toast.show();
|
||||||
document.getElementById("submit-upload").innerHTML =
|
uploadButton.disabled = false;
|
||||||
"Upload Files";
|
spinner.classList.add("d-none");
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
})));
|
})));
|
||||||
|
|
||||||
// Add event listener to control the submission manually
|
// Add event listener to control the submission manually
|
||||||
document.querySelector("#submit-upload").addEventListener("click", function () {
|
document
|
||||||
console.log("Ini adalah value dropzone", dropzone.files[0]);
|
.querySelector("#btnUploadBusinessIndustry")
|
||||||
const formData = new FormData();
|
.addEventListener("click", function () {
|
||||||
console.log("Dropzonefiles", dropzone.files);
|
console.log("Ini adalah value dropzone", dropzone.files[0]);
|
||||||
|
const formData = new FormData();
|
||||||
|
|
||||||
this.innerHTML =
|
if (dropzone.files.length > 0) {
|
||||||
'<span class="spinner-border spinner-border-sm me-1" role="status" aria-hidden="true"></span>Loading...';
|
formData.append("file", dropzone.files[0]);
|
||||||
|
dropzone.processQueue(); // Ini akan manual memicu upload
|
||||||
|
uploadButton.disabled = true;
|
||||||
|
spinner.classList.remove("d-none");
|
||||||
|
} else {
|
||||||
|
document.getElementById("toast-message").innerText =
|
||||||
|
"Please add a file first.";
|
||||||
|
toast.show();
|
||||||
|
uploadButton.disabled = false;
|
||||||
|
spinner.classList.add("d-none");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Pastikan ada file dalam queue sebelum memprosesnya
|
dropzone.on("addedfile", function (file) {});
|
||||||
if (dropzone.files.length > 0) {
|
|
||||||
formData.append("file", dropzone.files[0]);
|
|
||||||
console.log("ini adalah form data on submit", ...formData);
|
|
||||||
dropzone.processQueue(); // Ini akan manual memicu upload
|
|
||||||
} else {
|
|
||||||
// Show error toast when no file is selected
|
|
||||||
document.getElementById("toast-message").innerText =
|
|
||||||
"Please add a file first.";
|
|
||||||
toast.show();
|
|
||||||
|
|
||||||
document.getElementById("submit-upload").innerHTML = "Upload Files";
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Optional: Listen for the 'addedfile' event to log or control file add behavior
|
|
||||||
dropzone.on("addedfile", function (file) {
|
|
||||||
console.log("File ditambahkan:", file);
|
|
||||||
console.log("Nama File:", file.name);
|
|
||||||
console.log("Tipe File:", file.type);
|
|
||||||
console.log("Ukuran File:", (file.size / 1024).toFixed(2) + " KB");
|
|
||||||
});
|
|
||||||
|
|
||||||
dropzone.on("complete", function (file) {
|
dropzone.on("complete", function (file) {
|
||||||
dropzone.removeFile(file);
|
dropzone.removeFile(file);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Function to show toast
|
|
||||||
// function showToast(iconClass, iconColor, message) {
|
|
||||||
// const toastElement = document.getElementById("toastUploadAdvertisement");
|
|
||||||
// const toastBody = toastElement.querySelector(".toast-body");
|
|
||||||
// const toastHeader = toastElement.querySelector(".toast-header");
|
|
||||||
|
|
||||||
// // Remove existing icon (if any) before adding the new one
|
|
||||||
// const existingIcon = toastHeader.querySelector(".bx");
|
|
||||||
// if (existingIcon) {
|
|
||||||
// toastHeader.querySelector(".auth-logo").removeChild(existingIcon); // Remove the existing icon
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Add the new icon to the toast header
|
|
||||||
// const icon = document.createElement("i");
|
|
||||||
// icon.classList.add("bx", iconClass);
|
|
||||||
// icon.style.fontSize = "25px";
|
|
||||||
// icon.style.color = iconColor;
|
|
||||||
// toastHeader.querySelector(".auth-logo").appendChild(icon);
|
|
||||||
|
|
||||||
// // Set the toast message
|
|
||||||
// toastBody.textContent = message;
|
|
||||||
|
|
||||||
// // Show the toast
|
|
||||||
// const toast = new bootstrap.Toast(toastElement); // Inisialisasi Bootstrap Toast
|
|
||||||
// toast.show();
|
|
||||||
// }
|
|
||||||
|
|||||||
@@ -64,7 +64,10 @@
|
|||||||
<!-- end dropzon-preview -->
|
<!-- end dropzon-preview -->
|
||||||
</div>
|
</div>
|
||||||
<div class="d-flex justify-content-end">
|
<div class="d-flex justify-content-end">
|
||||||
<button id="submit-upload" class="btn btn-primary">Upload Files</button>
|
<button type="button" class="btn btn-primary" id="btnUploadBusinessIndustry">
|
||||||
|
<span id="spinner" class="spinner-border spinner-border-sm me-1 d-none" role="status" aria-hidden="true"></span>
|
||||||
|
Upload
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div> <!-- end card body -->
|
</div> <!-- end card body -->
|
||||||
</div> <!-- end card -->
|
</div> <!-- end card -->
|
||||||
|
|||||||
Reference in New Issue
Block a user