diff --git a/site/carts-new.html b/site/carts-new.html
deleted file mode 100644
index 74c98f7..0000000
--- a/site/carts-new.html
+++ /dev/null
@@ -1,17 +0,0 @@
-%%_templates/_header.html%% %%_templates/_menu.html%%
-
-
-
-
-
-
+
+
%%_templates/_footer.html%%
diff --git a/site/carts.js b/site/carts.js
index a50e312..2820332 100644
--- a/site/carts.js
+++ b/site/carts.js
@@ -1,162 +1,53 @@
-const shoppingCarts = new ShoppingCarts();
-shoppingCarts.load();
+const { downloadJSON } = require("./misc");
+const model = require("./model");
+require("./views");
-async function load() {
- const items = await loadItems();
- const lookup = {};
- for (const item of items) {
- lookup[item.store + item.id] = item;
+function newCart() {
+ let name = prompt("Name für Warenkorb eingeben:");
+ if (!name || name.trim().length == 0) return;
+ name = name.trim();
+ if (model.carts.carts.some((cart) => cart.name === name)) {
+ alert("Warenkorb mit Namen '" + name + "' existiert bereits");
+ return;
}
+ model.carts.add(name);
+ location.href = `/cart.html?name=${encodeURIComponent(name)}`;
+}
- // Update carts with latest price info
- for (const cart of shoppingCarts.carts) {
+function importCarts(importedCarts) {
+ for (const importedCart of importedCarts) {
const items = [];
- for (const cartItem of cart.items) {
- const item = lookup[cartItem.store + cartItem.id];
+ for (const cartItem of importedCart.items) {
+ const item = model.items.lookup[cartItem.store + cartItem.id];
if (!item) continue;
items.push(item);
}
- cart.items = items;
- }
- shoppingCarts.save();
+ importedCart.items = items;
- if (shoppingCarts.carts.findIndex((cart) => cart.name === "Momentum Eigenmarken Vergleich") == -1) {
- response = await fetch("data/momentum-cart.json");
- momentumCart = await response.json();
- shoppingCarts.carts.unshift(momentumCart);
- shoppingCarts.save();
- }
-
- const newCartButton = document.querySelector("#newcart");
- newCartButton.addEventListener("click", () => {
- let name = prompt("Name für Warenkorb eingeben:");
- if (!name || name.trim().length == 0) return;
- name = name.trim();
- for (cart of shoppingCarts.carts) {
- if (cart.name === name) {
- alert("Warenkorb mit Namen '" + name + "' existiert bereits");
- return;
+ const index = model.carts.carts.findIndex((cart) => cart.name === importedCart.name);
+ if (index != -1) {
+ if (confirm("Existierenden Warenkorb '" + importedCart.name + " überschreiben?")) {
+ model.carts.carts[index] = importedCart;
}
+ } else {
+ model.carts.carts.push(importedCart);
}
- shoppingCarts.add(name);
- location.href = `/cart.html?name=${encodeURIComponent(name)}`;
- });
-
- const exportButton = document.querySelector("#export");
- exportButton.addEventListener("click", () => {
- downloadFile("carts.json", JSON.stringify(shoppingCarts.carts, null, 2));
- });
-
- const importButton = document.querySelector("#import");
- importButton.addEventListener("click", () => {
- document.getElementById("fileInput").value = null;
- document.getElementById("fileInput").click();
- });
+ }
+ model.carts.save();
+}
+(async () => {
+ await model.load();
+ document.querySelector("#carts").model = model.carts;
+ document.querySelector("#new").addEventListener("click", () => newCart());
+ document.querySelector("#export").addEventListener("click", () => downloadJSON("carts.json", model.carts.carts));
+ document.querySelector("#import").addEventListener("click", () => document.querySelector("#fileInput").click());
document.querySelector("#fileInput").addEventListener("change", function (event) {
- const file = event.target.files[0];
const reader = new FileReader();
- reader.onload = function (event) {
- const contents = event.target.result;
- const importedCarts = JSON.parse(contents);
- for (const importedCart of importedCarts) {
- const items = [];
- for (const cartItem of importedCart.items) {
- const item = lookup[cartItem.store + cartItem.id];
- if (!item) continue;
- items.push(item);
- }
- importedCart.items = items;
-
- const index = shoppingCarts.carts.findIndex((cart) => cart.name === importedCart.name);
- if (index != -1) {
- if (confirm("Existierenden Warenkorb '" + importedCart.name + " überschreiben?")) {
- console.log(shoppingCarts.carts[index]);
- shoppingCarts.carts[index] = importedCart;
- console.log(shoppingCarts.carts[index]);
- }
- } else {
- shoppingCarts.carts.push(importedCart);
- }
- }
- shoppingCarts.save();
- showCarts(lookup);
+ reader.onload = (event) => {
+ const importedCarts = JSON.parse(event.target.result);
+ importCarts(importedCarts);
};
- reader.readAsText(file);
+ reader.readAsText(event.target.files[0]);
});
-
- showCarts(lookup);
-}
-
-function showCarts(lookup) {
- const cartsTable = document.querySelector("#carts");
- cartsTable.innerHTML = "";
- cartsTable.appendChild(
- dom(
- "thead",
- `
-
- Name |
- Produkte |
- Preis |
- Aktionen |
-
- `
- )
- );
-
- shoppingCarts.carts.forEach((cart) => {
- let oldPrice = 0;
- let currPrice = 0;
- let link = encodeURIComponent(cart.name) + ";";
- for (const cartItem of cart.items) {
- const item = lookup[cartItem.store + cartItem.id];
- if (!item) continue;
- oldPrice += item.priceHistory[item.priceHistory.length - 1].price;
- currPrice += item.priceHistory[0].price;
- link += item.store + item.id + ";";
- }
- const increase = oldPrice != 0 ? Math.round(((currPrice - oldPrice) / oldPrice) * 100) : 0;
- const cartUrl = `cart.html?name=${encodeURIComponent(cart.name)}`;
-
- const row = dom(
- "tr",
- `
-
${cart.name} |
-
Produkte: ${cart.items.length} |
-
- Preisänderungen:
- ${currPrice.toFixed(2)} ${
- (increase > 0 ? "+" : "") + increase + "%"
- }
- |
-
-
- Teilen
- JSON
- ${
- cart.name != "Momentum Eigenmarken Vergleich"
- ? ` `
- : ""
- }
-
- |
- `
- );
- row.classList.add("grid", "grid-cols-3", "hover:bg-gray-100", "border", "border-gray-200", "rounded-md", "p-2", "md:table-row");
- row.querySelector("td").addEventListener("click", () => (location.href = cartUrl));
- row.querySelector(".cartjson").addEventListener("click", (event) => {
- event.preventDefault();
- downloadFile(cart.name + ".json", JSON.stringify(cart, null, 2));
- });
- if (cart.name != "Momentum Eigenmarken Vergleich") {
- row.querySelector(".cartdelete").addEventListener("click", () => {
- shoppingCarts.remove(cart.name);
- showCarts(lookup);
- });
- }
- cartsTable.appendChild(row);
- });
-}
-
-load();
+})();
diff --git a/site/views/carts-list.js b/site/views/carts-list.js
index c16a35a..0d5be5d 100644
--- a/site/views/carts-list.js
+++ b/site/views/carts-list.js
@@ -5,7 +5,7 @@ class CartsList extends View {
constructor() {
super();
this.innerHTML = /*html*/ `
-