2023-05-31 16:42:41 +02:00
|
|
|
const shoppingCarts = new ShoppingCarts();
|
|
|
|
shoppingCarts.load();
|
2023-05-27 20:56:26 +02:00
|
|
|
|
2023-05-18 18:14:51 +02:00
|
|
|
async function load() {
|
2023-05-23 11:21:48 +02:00
|
|
|
const items = await loadItems();
|
2023-06-04 03:30:05 +02:00
|
|
|
const lookup = {};
|
|
|
|
for (const item of items) {
|
2023-05-30 20:02:03 +02:00
|
|
|
lookup[item.store + item.id] = item;
|
2023-05-19 16:01:43 +02:00
|
|
|
}
|
2023-05-18 18:14:51 +02:00
|
|
|
|
2023-05-27 20:56:26 +02:00
|
|
|
// Update carts with latest price info
|
2023-06-04 03:30:05 +02:00
|
|
|
for (const cart of shoppingCarts.carts) {
|
2023-05-27 20:56:26 +02:00
|
|
|
const items = [];
|
2023-06-04 03:30:05 +02:00
|
|
|
for (const cartItem of cart.items) {
|
2023-05-30 20:02:03 +02:00
|
|
|
const item = lookup[cartItem.store + cartItem.id];
|
2023-05-27 20:56:26 +02:00
|
|
|
if (!item) continue;
|
|
|
|
items.push(item);
|
|
|
|
}
|
|
|
|
cart.items = items;
|
|
|
|
}
|
2023-05-29 19:05:02 +02:00
|
|
|
shoppingCarts.save();
|
2023-05-27 20:56:26 +02:00
|
|
|
|
2023-06-02 16:45:54 +02:00
|
|
|
if (shoppingCarts.carts.findIndex((cart) => cart.name === "Momentum Eigenmarken Vergleich") == -1) {
|
2023-06-03 22:00:52 +02:00
|
|
|
response = await fetch("data/momentum-cart.json");
|
2023-05-22 16:02:01 +02:00
|
|
|
momentumCart = await response.json();
|
2023-05-29 19:05:02 +02:00
|
|
|
shoppingCarts.carts.unshift(momentumCart);
|
|
|
|
shoppingCarts.save();
|
2023-05-22 16:02:01 +02:00
|
|
|
}
|
2023-05-22 14:39:57 +02:00
|
|
|
|
2023-05-18 18:14:51 +02:00
|
|
|
const newCartButton = document.querySelector("#newcart");
|
|
|
|
newCartButton.addEventListener("click", () => {
|
|
|
|
let name = prompt("Name für Warenkorb eingeben:");
|
2023-05-29 17:31:00 +02:00
|
|
|
if (name.length === 0) return;
|
2023-05-29 19:05:02 +02:00
|
|
|
for (cart of shoppingCarts.carts) {
|
2023-05-29 17:31:00 +02:00
|
|
|
if (cart.name === name) {
|
2023-05-18 18:14:51 +02:00
|
|
|
alert("Warenkorb mit Namen '" + name + "' existiert bereits");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-05-29 19:05:02 +02:00
|
|
|
shoppingCarts.add(name);
|
2023-06-02 08:00:54 +02:00
|
|
|
location.href = `/cart.html?name=${encodeURIComponent(name)}`;
|
2023-05-18 18:14:51 +02:00
|
|
|
});
|
|
|
|
|
2023-05-27 20:56:26 +02:00
|
|
|
const exportButton = document.querySelector("#export");
|
|
|
|
exportButton.addEventListener("click", () => {
|
2023-05-29 19:05:02 +02:00
|
|
|
downloadFile("carts.json", JSON.stringify(shoppingCarts.carts, null, 2));
|
2023-05-27 20:56:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const importButton = document.querySelector("#import");
|
|
|
|
importButton.addEventListener("click", () => {
|
2023-06-02 16:45:54 +02:00
|
|
|
document.getElementById("fileInput").value = null;
|
|
|
|
document.getElementById("fileInput").click();
|
2023-05-27 20:56:26 +02:00
|
|
|
});
|
|
|
|
|
2023-06-02 16:45:54 +02:00
|
|
|
document.querySelector("#fileInput").addEventListener("change", function (event) {
|
2023-05-27 20:56:26 +02:00
|
|
|
const file = event.target.files[0];
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.onload = function (event) {
|
|
|
|
const contents = event.target.result;
|
|
|
|
const importedCarts = JSON.parse(contents);
|
2023-05-29 17:31:00 +02:00
|
|
|
for (const importedCart of importedCarts) {
|
2023-05-27 20:56:26 +02:00
|
|
|
const items = [];
|
2023-06-04 03:30:05 +02:00
|
|
|
for (const cartItem of importedCart.items) {
|
2023-05-30 20:02:03 +02:00
|
|
|
const item = lookup[cartItem.store + cartItem.id];
|
2023-05-27 20:56:26 +02:00
|
|
|
if (!item) continue;
|
|
|
|
items.push(item);
|
|
|
|
}
|
|
|
|
importedCart.items = items;
|
|
|
|
|
2023-06-02 16:45:54 +02:00
|
|
|
const index = shoppingCarts.carts.findIndex((cart) => cart.name === importedCart.name);
|
2023-05-27 20:56:26 +02:00
|
|
|
if (index != -1) {
|
|
|
|
if (confirm("Existierenden Warenkorb '" + importedCart.name + " überschreiben?")) {
|
2023-05-29 19:05:02 +02:00
|
|
|
console.log(shoppingCarts.carts[index]);
|
|
|
|
shoppingCarts.carts[index] = importedCart;
|
2023-06-02 16:45:54 +02:00
|
|
|
console.log(shoppingCarts.carts[index]);
|
2023-05-27 20:56:26 +02:00
|
|
|
}
|
|
|
|
} else {
|
2023-05-29 19:05:02 +02:00
|
|
|
shoppingCarts.carts.push(importedCart);
|
2023-05-27 20:56:26 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-29 19:05:02 +02:00
|
|
|
shoppingCarts.save();
|
2023-05-27 20:56:26 +02:00
|
|
|
showCarts(lookup);
|
|
|
|
};
|
|
|
|
reader.readAsText(file);
|
|
|
|
});
|
|
|
|
|
2023-05-19 16:01:43 +02:00
|
|
|
showCarts(lookup);
|
2023-05-18 18:14:51 +02:00
|
|
|
}
|
|
|
|
|
2023-05-19 16:01:43 +02:00
|
|
|
function showCarts(lookup) {
|
2023-05-18 18:14:51 +02:00
|
|
|
const cartsTable = document.querySelector("#carts");
|
|
|
|
cartsTable.innerHTML = "";
|
2023-06-02 16:45:54 +02:00
|
|
|
cartsTable.appendChild(
|
|
|
|
dom(
|
|
|
|
"thead",
|
|
|
|
`
|
2023-05-26 14:09:15 +02:00
|
|
|
<tr>
|
|
|
|
<th>Name</th>
|
|
|
|
<th>Produkte</th>
|
|
|
|
<th>Preis</th>
|
2023-06-04 14:45:05 +02:00
|
|
|
<th>Aktionen</th>
|
2023-05-26 14:09:15 +02:00
|
|
|
</tr>
|
2023-06-02 16:45:54 +02:00
|
|
|
`
|
|
|
|
)
|
|
|
|
);
|
2023-05-18 18:14:51 +02:00
|
|
|
|
2023-06-02 16:45:54 +02:00
|
|
|
shoppingCarts.carts.forEach((cart) => {
|
2023-05-19 16:01:43 +02:00
|
|
|
let oldPrice = 0;
|
|
|
|
let currPrice = 0;
|
2023-06-02 16:45:54 +02:00
|
|
|
let link = encodeURIComponent(cart.name) + ";";
|
2023-06-04 03:30:05 +02:00
|
|
|
for (const cartItem of cart.items) {
|
2023-05-30 20:02:03 +02:00
|
|
|
const item = lookup[cartItem.store + cartItem.id];
|
2023-05-19 16:01:43 +02:00
|
|
|
if (!item) continue;
|
|
|
|
oldPrice += item.priceHistory[item.priceHistory.length - 1].price;
|
|
|
|
currPrice += item.priceHistory[0].price;
|
2023-05-30 20:02:03 +02:00
|
|
|
link += item.store + item.id + ";";
|
2023-05-19 16:01:43 +02:00
|
|
|
}
|
2023-06-02 16:45:54 +02:00
|
|
|
const increase = oldPrice != 0 ? Math.round(((currPrice - oldPrice) / oldPrice) * 100) : 0;
|
2023-06-04 14:45:05 +02:00
|
|
|
const cartUrl = `cart.html?name=${encodeURIComponent(cart.name)}`;
|
2023-05-19 16:01:43 +02:00
|
|
|
|
2023-06-04 14:45:05 +02:00
|
|
|
const row = dom(
|
|
|
|
"tr",
|
|
|
|
`
|
|
|
|
<td data-label="Name"><a href="${cartUrl}">${cart.name}</a></td>
|
|
|
|
<td data-label="Produkte">${cart.items.length}</td>
|
|
|
|
<td data-label="Preis">
|
|
|
|
<span style="color: ${currPrice > oldPrice ? "red" : "green"}">${currPrice.toFixed(2)} ${
|
|
|
|
(increase > 0 ? "+" : "") + increase + "%"
|
|
|
|
}</span>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<div class="cartactions">
|
|
|
|
<a href="cart.html?cart=${link}">Teilen</a>
|
|
|
|
<a class="cartjson" href="">JSON</a>
|
|
|
|
${cart.name != "Momentum Eigenmarken Vergleich" ? `<input class="cartdelete" type="button" value="Löschen">` : ""}
|
|
|
|
</div>
|
|
|
|
</td>
|
|
|
|
`
|
2023-06-02 16:45:54 +02:00
|
|
|
);
|
2023-06-04 14:45:05 +02:00
|
|
|
row.querySelector("td").addEventListener("click", () => (location.href = cartUrl));
|
|
|
|
row.querySelector(".cartjson").addEventListener("click", (event) => {
|
2023-05-31 16:42:41 +02:00
|
|
|
event.preventDefault();
|
|
|
|
downloadFile(cart.name + ".json", JSON.stringify(cart, null, 2));
|
2023-06-02 16:45:54 +02:00
|
|
|
});
|
2023-05-22 14:39:57 +02:00
|
|
|
if (cart.name != "Momentum Eigenmarken Vergleich") {
|
2023-06-04 14:45:05 +02:00
|
|
|
row.querySelector(".cartdelete").addEventListener("click", () => {
|
2023-05-29 19:05:02 +02:00
|
|
|
shoppingCarts.remove(cart.name);
|
2023-05-22 14:39:57 +02:00
|
|
|
showCarts(lookup);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
cartsTable.appendChild(row);
|
2023-05-19 16:01:43 +02:00
|
|
|
});
|
2023-05-18 18:14:51 +02:00
|
|
|
}
|
|
|
|
|
2023-06-02 16:45:54 +02:00
|
|
|
load();
|