optimising and making it better

This commit is contained in:
sdarbinyan
2026-02-26 21:54:21 +04:00
parent 7a00a8f1e3
commit 10b4974719
58 changed files with 318 additions and 1804 deletions

View File

@@ -1,6 +1,7 @@
import { Injectable, signal, computed, effect } from '@angular/core';
import { ApiService } from './api.service';
import { Item, CartItem } from '../models';
import { getDiscountedPrice } from '../utils/item.utils';
import { environment } from '../../environments/environment';
import type { } from '../types/telegram.types';
@@ -11,6 +12,7 @@ export class CartService {
private readonly STORAGE_KEY = `${environment.brandName.toLowerCase().replace(/\s+/g, '_')}_cart`;
private cartItems = signal<CartItem[]>([]);
private isTelegram = typeof window !== 'undefined' && !!window.Telegram?.WebApp;
private addingItems = new Set<number>();
items = this.cartItems.asReadonly();
itemCount = computed(() => {
@@ -22,8 +24,7 @@ export class CartService {
const items = this.cartItems();
if (!Array.isArray(items)) return 0;
return items.reduce((total, item) => {
const price = item.price * (1 - item.discount / 100);
return total + (price * item.quantity);
return total + (getDiscountedPrice(item) * item.quantity);
}, 0);
});
@@ -40,8 +41,8 @@ export class CartService {
private saveToStorage(items: CartItem[]): void {
const data = JSON.stringify(items);
// Always save to sessionStorage
sessionStorage.setItem(this.STORAGE_KEY, data);
// Always save to localStorage
localStorage.setItem(this.STORAGE_KEY, data);
// Also save to Telegram CloudStorage if available
if (this.isTelegram) {
@@ -59,21 +60,21 @@ export class CartService {
window.Telegram!.WebApp.CloudStorage.getItem(this.STORAGE_KEY, (err, value) => {
if (err) {
console.error('Error loading from Telegram CloudStorage:', err);
this.loadFromSessionStorage();
this.loadFromLocalStorage();
} else if (value) {
this.parseAndSetCart(value) || this.loadFromSessionStorage();
this.parseAndSetCart(value) || this.loadFromLocalStorage();
} else {
// No data in CloudStorage, try sessionStorage
this.loadFromSessionStorage();
// No data in CloudStorage, try localStorage
this.loadFromLocalStorage();
}
});
} else {
this.loadFromSessionStorage();
this.loadFromLocalStorage();
}
}
private loadFromSessionStorage(): void {
const stored = sessionStorage.getItem(this.STORAGE_KEY);
private loadFromLocalStorage(): void {
const stored = localStorage.getItem(this.STORAGE_KEY);
if (stored) {
this.parseAndSetCart(stored);
}
@@ -98,6 +99,9 @@ export class CartService {
}
addItem(itemID: number, quantity: number = 1): void {
// Prevent duplicate API calls for same item
if (this.addingItems.has(itemID)) return;
const currentItems = this.cartItems();
const existingItem = currentItems.find(i => i.itemID === itemID);
@@ -106,14 +110,16 @@ export class CartService {
this.updateQuantity(itemID, existingItem.quantity + quantity);
} else {
// Get item details from API and add to cart
this.addingItems.add(itemID);
this.apiService.getItem(itemID).subscribe({
next: (item) => {
const cartItem: CartItem = { ...item, quantity };
this.cartItems.set([...this.cartItems(), cartItem]);
this.addingItems.delete(itemID);
},
error: (err) => {
console.error('Error adding to cart:', err);
alert('Ошибка добавления в корзину: ' + (err.error?.message || err.message));
this.addingItems.delete(itemID);
}
});
}