heissepreise/site/carts.js

54 lines
2.0 KiB
JavaScript
Raw Normal View History

2023-06-09 01:31:30 +02:00
const { downloadJSON } = require("./misc");
const model = require("./model");
require("./views");
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;
2023-05-19 16:01:43 +02:00
}
2023-06-09 01:31:30 +02:00
model.carts.add(name);
location.href = `/cart.html?name=${encodeURIComponent(name)}`;
}
2023-06-09 01:31:30 +02:00
function importCarts(importedCarts) {
for (const importedCart of importedCarts) {
2023-05-27 20:56:26 +02:00
const items = [];
2023-06-09 01:31:30 +02:00
for (const cartItem of importedCart.items) {
const item = model.items.lookup[cartItem.store + cartItem.id];
2023-05-27 20:56:26 +02:00
if (!item) continue;
items.push(item);
}
2023-06-09 01:31:30 +02:00
importedCart.items = items;
2023-05-27 20:56:26 +02:00
2023-06-09 01:31:30 +02:00
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;
}
2023-06-09 01:31:30 +02:00
} else {
model.carts.carts.push(importedCart);
}
2023-06-09 01:31:30 +02:00
}
model.carts.save();
}
2023-05-27 20:56:26 +02:00
2023-06-09 01:31:30 +02:00
(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) {
2023-05-27 20:56:26 +02:00
const reader = new FileReader();
2023-06-09 01:31:30 +02:00
reader.onload = (event) => {
const importedCarts = JSON.parse(event.target.result);
importCarts(importedCarts);
2023-05-27 20:56:26 +02:00
};
2023-06-09 01:31:30 +02:00
reader.readAsText(event.target.files[0]);
2023-05-19 16:01:43 +02:00
});
2023-06-09 01:31:30 +02:00
})();