Fix cart management (import, save)

This commit is contained in:
Mario Zechner 2023-06-16 19:24:43 +02:00
parent c7e7d27b53
commit c1e703fd28
2 changed files with 52 additions and 20 deletions

View File

@ -1,4 +1,4 @@
const { getQueryParameter } = require("./js/misc");
const { getQueryParameter, today } = require("./js/misc");
const models = require("./model");
const { Model } = require("./model/model");
const { View } = require("./views/view");
@ -42,8 +42,19 @@ class CartHeader extends View {
const cart = this.model.cart;
let index = carts.findIndex((c) => c.name === cart.name);
if (index != -1) {
if (confirm("Existierenden Warenkorb '" + cart.name + " überschreiben?")) {
carts[index] = cart;
let newName = cart.name;
while (true) {
newName = prompt(
"Warenkorb '" + cart.name + " existiert bereits. Bitte einen anderen Namen für den zu speichernden Warenkorb eingeben",
cart.name + today()
);
if (!newName || newName.trim().length == 0) return;
newName = newName.trim();
if (newName != cart.name) {
cart.name = newName;
carts.push(cart);
break;
}
}
} else {
carts.push(cart);
@ -65,7 +76,7 @@ class CartHeader extends View {
for (const cartItem of cart.items) {
link += cartItem.store + cartItem.id + ";";
}
elements.share.href = "cart.html?cart=" + link + (this.stateToUrl ? stateToUrl() : "");
elements.share.href = "cart.html?cart=" + link + (this.stateToUrl ? this.stateToUrl() : "");
}
}
}

View File

@ -1,4 +1,4 @@
const { downloadJSON } = require("./js/misc");
const { downloadJSON, today } = require("./js/misc");
const model = require("./model");
require("./views");
@ -14,28 +14,49 @@ function newCart() {
location.href = `cart.html?name=${encodeURIComponent(name)}`;
}
function importCarts(importedCarts) {
for (const importedCart of importedCarts) {
const items = [];
for (const cartItem of importedCart.items) {
const item = model.items.lookup[cartItem.store + cartItem.id];
if (!item) continue;
items.push(item);
}
importedCart.items = items;
function isIterable(obj) {
return typeof obj[Symbol.iterator] === "function";
}
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;
function importCart(importedCart) {
const items = [];
for (const cartItem of importedCart.items) {
const item = model.items.lookup[cartItem.store + cartItem.id];
if (!item) continue;
items.push(item);
}
importedCart.items = items;
const index = model.carts.carts.findIndex((cart) => cart.name === importedCart.name);
if (index != -1) {
let newName = importedCart.name;
while (true) {
newName = prompt(
"Warenkorb '" + importedCart.name + " existiert bereits. Bitte einen anderen Namen für den zu importierenden Warenkorb eingeben",
importedCart.name + today()
);
if (!newName || newName.trim().length == 0) return;
newName = newName.trim();
if (newName != importedCart.name) {
importedCart.name = newName;
model.carts.carts.push(importedCart);
break;
}
} else {
model.carts.carts.push(importedCart);
}
} else {
model.carts.carts.push(importedCart);
}
model.carts.save();
}
function importCarts(importedCarts) {
if (isIterable(importedCarts)) {
importedCarts.forEach((cart) => importCart(cart));
} else {
importCart(importedCarts);
}
}
(async () => {
await model.load();
document.querySelector("#carts").model = model.carts;