wip: cart

This commit is contained in:
Nikita Kiselev
2025-07-20 22:22:14 +03:00
parent 1ffb1cef12
commit ee67bd55df
12 changed files with 541 additions and 19 deletions

63
spa/src/ShoppingCart.js Normal file
View File

@@ -0,0 +1,63 @@
import {reactive} from "vue";
class ShoppingCart {
constructor() {
this.items = reactive([]);
this.storageKey = 'shoppingCart';
this.storage = Telegram.WebApp.DeviceStorage;
this._load()
.then(items => {
this.items = items;
console.log(items);
})
.catch(error => console.log(error));
}
async addItem(productId, productName, quantity, options = {}) {
this.items.push({ productId: productId, productName: productName, quantity, options });
this._save(this.items);
}
has(productId) {
const item = this.getItem(productId);
console.log(item);
return this.getItem(productId) !== null;
}
getItem(productId) {
return this.items.find(item => item.productId === productId) ?? null;
}
getItems() {
return this.items;
}
clear() {
this.storage.deleteItem(this.storageKey)
}
async _load() {
return new Promise((resolve, reject) => {
this.storage.getItem(this.storageKey, (error, value) => {
if (error) {
console.error(error);
reject([]);
}
try {
resolve(value ? JSON.parse(value) : []);
} catch (error) {
console.error(error);
reject([]);
}
});
});
}
_save(items) {
this.storage.setItem(this.storageKey, JSON.stringify(items));
}
}
export default ShoppingCart;