improvments are done
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
import { Injectable, signal, computed, effect } from '@angular/core';
|
||||
import { ApiService } from './api.service';
|
||||
import { Item, CartItem } from '../models';
|
||||
import { environment } from '../../environments/environment';
|
||||
import type { } from '../types/telegram.types';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class CartService {
|
||||
private readonly STORAGE_KEY = 'dexarmarket_cart';
|
||||
private readonly STORAGE_KEY = `${environment.brandName.toLowerCase().replace(/\s+/g, '_')}_cart`;
|
||||
private cartItems = signal<CartItem[]>([]);
|
||||
private isTelegram = typeof window !== 'undefined' && !!window.Telegram?.WebApp;
|
||||
|
||||
@@ -60,29 +61,13 @@ export class CartService {
|
||||
console.error('Error loading from Telegram CloudStorage:', err);
|
||||
this.loadFromSessionStorage();
|
||||
} else if (value) {
|
||||
try {
|
||||
const items = JSON.parse(value);
|
||||
if (Array.isArray(items)) {
|
||||
// Migrate old items without quantity field
|
||||
const migratedItems: CartItem[] = items.map(item => ({
|
||||
...item,
|
||||
quantity: item.quantity || 1
|
||||
}));
|
||||
this.cartItems.set(migratedItems);
|
||||
} else {
|
||||
this.cartItems.set([]);
|
||||
}
|
||||
} catch (parseErr) {
|
||||
console.error('Error parsing cart data:', parseErr);
|
||||
this.loadFromSessionStorage();
|
||||
}
|
||||
this.parseAndSetCart(value) || this.loadFromSessionStorage();
|
||||
} else {
|
||||
// No data in CloudStorage, try sessionStorage
|
||||
this.loadFromSessionStorage();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// Load from sessionStorage
|
||||
this.loadFromSessionStorage();
|
||||
}
|
||||
}
|
||||
@@ -90,27 +75,28 @@ export class CartService {
|
||||
private loadFromSessionStorage(): void {
|
||||
const stored = sessionStorage.getItem(this.STORAGE_KEY);
|
||||
if (stored) {
|
||||
try {
|
||||
const items = JSON.parse(stored);
|
||||
if (Array.isArray(items)) {
|
||||
// Migrate old items without quantity field
|
||||
const migratedItems: CartItem[] = items.map(item => ({
|
||||
...item,
|
||||
quantity: item.quantity || 1
|
||||
}));
|
||||
this.cartItems.set(migratedItems);
|
||||
} else {
|
||||
this.cartItems.set([]);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error parsing cart from sessionStorage:', err);
|
||||
this.cartItems.set([]);
|
||||
}
|
||||
} else {
|
||||
this.cartItems.set([]);
|
||||
this.parseAndSetCart(stored);
|
||||
}
|
||||
}
|
||||
|
||||
/** Parse JSON cart data, migrate legacy items, and set the signal. Returns true on success. */
|
||||
private parseAndSetCart(json: string): boolean {
|
||||
try {
|
||||
const items = JSON.parse(json);
|
||||
if (Array.isArray(items)) {
|
||||
this.cartItems.set(items.map(item => ({
|
||||
...item,
|
||||
quantity: item.quantity || 1
|
||||
})));
|
||||
return true;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error parsing cart data:', err);
|
||||
}
|
||||
this.cartItems.set([]);
|
||||
return false;
|
||||
}
|
||||
|
||||
addItem(itemID: number, quantity: number = 1): void {
|
||||
const currentItems = this.cartItems();
|
||||
const existingItem = currentItems.find(i => i.itemID === itemID);
|
||||
|
||||
Reference in New Issue
Block a user