heissepreise/site/carts.js

82 lines
2.7 KiB
JavaScript
Raw Normal View History

async function load() {
const items = await loadItems();
2023-05-19 16:01:43 +02:00
lookup = {};
for (item of items) {
lookup[item.id] = item;
}
2023-05-22 16:02:01 +02:00
if (carts.findIndex(cart => cart.name == "Momentum Eigenmarken Vergleich") == -1) {
response = await fetch("momentum-cart.json");
momentumCart = await response.json();
carts.unshift(momentumCart);
saveCarts();
}
const newCartButton = document.querySelector("#newcart");
newCartButton.addEventListener("click", () => {
let name = prompt("Name für Warenkorb eingeben:");
if (name.length == 0) return;
for (cart of carts) {
if (cart.name == name) {
alert("Warenkorb mit Namen '" + name + "' existiert bereits");
return;
}
}
addCart(name);
location.href = "/cart.html?name=" + name;
});
2023-05-19 16:01:43 +02:00
showCarts(lookup);
}
2023-05-19 16:01:43 +02:00
function showCarts(lookup) {
const cartsTable = document.querySelector("#carts");
cartsTable.innerHTML = "";
2023-05-26 14:09:15 +02:00
cartsTable.appendChild(dom("thead", `
<tr>
<th>Name</th>
<th>Produkte</th>
<th>Preis</th>
<th></th>
</tr>
`));
2023-05-19 16:01:43 +02:00
carts.forEach(cart => {
let oldPrice = 0;
let currPrice = 0;
for (cartItem of cart.items) {
const item = lookup[cartItem.id];
if (!item) continue;
oldPrice += item.priceHistory[item.priceHistory.length - 1].price;
currPrice += item.priceHistory[0].price;
}
const increase = Math.round((currPrice - oldPrice) / oldPrice * 100);
const row = dom("tr", ``);
2023-05-26 14:21:17 +02:00
const nameDom = dom("td", `<a href="cart.html?name=${cart.name}">${cart.name}</a>`);
nameDom.setAttribute("data-label", "Name");
row.appendChild(nameDom);
const itemsDom = dom("td", cart.items.length);
itemsDom.setAttribute("data-label", "Produkte");
row.appendChild(itemsDom);
const priceDom = dom("td", `<span style="color: ${currPrice > oldPrice ? "red" : "green"}">${currPrice.toFixed(2)}`);
priceDom.setAttribute("data-label", "Preis");
row.appendChild(priceDom);
if (cart.name != "Momentum Eigenmarken Vergleich") {
let deleteButton = dom("input");
deleteButton.setAttribute("type", "button");
deleteButton.setAttribute("value", "Löschen");
2023-05-26 14:21:17 +02:00
const deleteDom = dom("td", ``);
deleteDom.appendChild(deleteButton);
row.appendChild(deleteDom);
2023-05-19 16:01:43 +02:00
deleteButton.addEventListener("click", () => {
removeCart(cart.name);
showCarts(lookup);
});
}
cartsTable.appendChild(row);
2023-05-19 16:01:43 +02:00
});
}
load();