2022-10-15 19:37:01 +02:00
|
|
|
document.addEventListener("DOMContentLoaded", () => {
|
|
|
|
// elements
|
2022-11-04 20:35:28 +01:00
|
|
|
let supplyDescriptionElement = document.getElementById("supplydescription");
|
|
|
|
let supplyPriceElement = document.getElementById("supplyprice");
|
|
|
|
let supplyFormElement = document.getElementById("supplyform");
|
|
|
|
let statusInfoElement = document.getElementById("statusinfo");
|
|
|
|
let supplySubmitButton = document.getElementById("supplysubmitbtn");
|
2022-10-15 19:37:01 +02:00
|
|
|
// custom submit method
|
2022-11-04 20:35:28 +01:00
|
|
|
supplyFormElement.addEventListener("submit", (event) => {
|
|
|
|
supplySubmitButton.disabled = true;
|
2022-10-15 19:37:01 +02:00
|
|
|
event.preventDefault(); // Don't do the default submit action!
|
2022-11-04 20:35:28 +01:00
|
|
|
if (isNaN(parseFloat(supplyPriceElement.value)) || supplyDescriptionElement.value == "") {
|
|
|
|
statusInfoElement.innerText = "Please enter a description and price."
|
|
|
|
supplySubmitButton.disabled = false;
|
2022-10-15 19:37:01 +02:00
|
|
|
}
|
|
|
|
let xhr = new XMLHttpRequest();
|
2022-11-04 20:35:28 +01:00
|
|
|
let formData = new FormData(supplyFormElement);
|
2022-10-15 19:37:01 +02:00
|
|
|
xhr.addEventListener("load", (event) => {
|
|
|
|
status_ = event.target.status;
|
|
|
|
response_ = event.target.responseText;
|
|
|
|
if (status_ == 200 && response_ == "success") {
|
2022-11-04 20:35:28 +01:00
|
|
|
statusInfoElement.innerText = "Success.";
|
2022-10-15 19:37:01 +02:00
|
|
|
window.location.replace("/");
|
|
|
|
}
|
|
|
|
else {
|
2022-11-04 20:35:28 +01:00
|
|
|
statusInfoElement.classList.add("errortext");
|
|
|
|
statusInfoElement.innerText = "An error occured.";
|
2022-10-15 19:37:01 +02:00
|
|
|
window.setTimeout(() => { window.location.reload() }, 5000);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
xhr.addEventListener("error", (event) => {
|
2022-11-04 20:35:28 +01:00
|
|
|
statusInfoElement.classList.add("errortext");
|
|
|
|
statusInfoElement.innerText = "An error occured.";
|
2022-10-15 19:37:01 +02:00
|
|
|
window.setTimeout(() => { window.location.reload() }, 5000);
|
|
|
|
})
|
|
|
|
xhr.open("POST", "/api/supply");
|
|
|
|
xhr.send(formData);
|
|
|
|
});
|
2023-02-17 22:01:09 +01:00
|
|
|
});
|