2023-06-13 01:16:27 +02:00
|
|
|
const misc = require("../js/misc");
|
2023-06-09 00:37:29 +02:00
|
|
|
const { Model } = require("./model");
|
2023-06-08 13:48:08 +02:00
|
|
|
|
2023-06-09 00:37:29 +02:00
|
|
|
class Carts extends Model {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this._carts = [];
|
|
|
|
}
|
2023-06-08 13:48:08 +02:00
|
|
|
|
2023-06-09 00:37:29 +02:00
|
|
|
get carts() {
|
|
|
|
return this._carts;
|
2023-06-08 13:48:08 +02:00
|
|
|
}
|
|
|
|
|
2023-06-09 00:37:29 +02:00
|
|
|
async load(itemsLookup) {
|
|
|
|
const val = localStorage.getItem("carts");
|
|
|
|
const carts = (this._carts = val ? JSON.parse(val) : []);
|
|
|
|
|
|
|
|
// Add Momentum cart if it is not in the list of carts
|
2023-06-17 01:11:21 +02:00
|
|
|
if (!localStorage.getItem("updatedMomentum") || !carts.some((cart) => cart.name === "Momentum Eigenmarken Vergleich")) {
|
|
|
|
localStorage.setItem("updatedMomentum", "true");
|
|
|
|
const momentumCart = await misc.fetchJSON("data/momentum-cart.new.json");
|
2023-06-09 00:37:29 +02:00
|
|
|
carts.unshift(momentumCart);
|
2023-06-08 13:48:08 +02:00
|
|
|
}
|
2023-06-09 00:37:29 +02:00
|
|
|
|
|
|
|
// Update items in cart to their latest version.
|
|
|
|
for (const cart of carts) {
|
|
|
|
const items = [];
|
|
|
|
for (const cartItem of cart.items) {
|
|
|
|
const item = itemsLookup[cartItem.store + cartItem.id];
|
|
|
|
if (!item) items.push(cartItem);
|
|
|
|
else items.push(item);
|
|
|
|
}
|
|
|
|
cart.items = items;
|
|
|
|
}
|
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
|
|
|
localStorage.setItem("carts", JSON.stringify(this._carts, null, 2));
|
|
|
|
this.notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
add(name) {
|
|
|
|
this._carts.push({ name: name, items: [] });
|
|
|
|
this.save();
|
2023-06-08 13:48:08 +02:00
|
|
|
}
|
2023-06-09 00:37:29 +02:00
|
|
|
|
|
|
|
remove(name) {
|
|
|
|
this._carts = this._carts.filter((cart) => cart.name !== name);
|
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.Carts = Carts;
|