Archived
1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
SOD/public/javascripts/admin/orders/add.js

60 lines
2 KiB
JavaScript
Raw Permalink Normal View History

2020-09-28 22:55:35 +02:00
const orders = document.getElementById("orders");
const sandwich = document.getElementById("sandwich");
const day = document.getElementById("day");
const locals = {
2020-09-28 22:55:35 +02:00
sandwich: document.querySelector("#oderCreator label").innerHTML,
day: document.querySelector("label[for=day]").innerText,
give: document.querySelector("p label span").innerText
};
function lastOrderId() {
2020-09-28 22:55:35 +02:00
if (orders.lastChild)
return parseInt(orders.lastChild.id.replace("order", ""));
return 0;
}
2020-09-28 22:55:35 +02:00
document.getElementById("addOrder").addEventListener("click", () => {
if (!sandwich.value || !day.value)
return;
let id = lastOrderId() + 1;
2020-09-28 22:55:35 +02:00
orders.insertAdjacentHTML("beforeend", `<div id="order${id}" class="row">
<div class="input-field col s6">
<input id="sandwich${id}" type="text" name="sandwiches[${id}]" value="${sandwich.value}" readonly required>
<label for="sandwich${id}">${locals.sandwich}</label>
</div>
2020-09-28 22:55:35 +02:00
<div class="input-field col s6">
<input id="date${id}" type="date" name="dates[${id}]" value="${day.value}" readonly required>
<label for="date${id}">${locals.day}</label>
</div>
2020-09-28 22:55:35 +02:00
<div class="input-field col s12 center">
<p>
<label>
<input type="checkbox" name="give[${id}]">
<span>${locals.give}</span>
</label>
</p>
</div>
2020-09-28 22:55:35 +02:00
<a class="btn-floating btn-large waves-effect waves-light red"><i class="material-icons">remove</i></a>
</div>`);
2020-09-28 22:55:35 +02:00
sandwich.selectedIndex = 0;
day.value = "";
let order = document.getElementById("order"+id);
order.querySelector("a").addEventListener("click", () => {
order.remove();
if (lastOrderId() === 0) {
sandwich.required = true;
day.required = true;
}
});
document.getElementById("order"+id).scrollIntoView({behavior: "smooth"});
});
2020-09-28 22:55:35 +02:00
document.querySelector("form").addEventListener("submit", () => {
return lastOrderId();
});