Support variant-aware cart lines
This commit is contained in:
@@ -5,6 +5,8 @@ import { normalizeDeliveryOption, normalizeOptionalNumber } from '../utils/norma
|
||||
import { environment } from '../../environments/environment';
|
||||
import type { } from '../types/telegram.types';
|
||||
|
||||
type CartVariant = { colour?: string; size?: string; price?: number; currency?: string };
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
@@ -187,16 +189,30 @@ export class CartService {
|
||||
return false;
|
||||
}
|
||||
|
||||
addItem(itemID: number, quantity: number = 1, variant?: { colour?: string; size?: string; price?: number; currency?: string }): void {
|
||||
private normalizeVariantSize(size?: string): string {
|
||||
return size && size.toLowerCase() !== 'default' ? size : '';
|
||||
}
|
||||
|
||||
private isSameCartLine(item: CartItem, itemID: number, variant?: CartVariant): boolean {
|
||||
return item.itemID === itemID
|
||||
&& (item.colour ?? '') === (variant?.colour ?? '')
|
||||
&& this.normalizeVariantSize(item.size) === this.normalizeVariantSize(variant?.size);
|
||||
}
|
||||
|
||||
private findCartLineIndex(itemID: number, variant?: CartVariant): number {
|
||||
return this.cartItems().findIndex(item => this.isSameCartLine(item, itemID, variant));
|
||||
}
|
||||
|
||||
addItem(itemID: number, quantity: number = 1, variant?: CartVariant): 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);
|
||||
const existingItem = currentItems.find(i => this.isSameCartLine(i, itemID, variant));
|
||||
|
||||
if (existingItem) {
|
||||
// Item exists, increase quantity
|
||||
this.updateQuantity(itemID, existingItem.quantity + quantity);
|
||||
this.updateQuantity(itemID, existingItem.quantity + quantity, variant);
|
||||
} else {
|
||||
// Get item details from API and add to cart
|
||||
this.addingItems.add(itemID);
|
||||
@@ -226,15 +242,15 @@ export class CartService {
|
||||
}
|
||||
}
|
||||
|
||||
updateQuantity(itemID: number, quantity: number): void {
|
||||
updateQuantity(itemID: number, quantity: number, variant?: CartVariant): void {
|
||||
if (quantity <= 0) {
|
||||
this.removeItem(itemID);
|
||||
this.removeItem(itemID, variant);
|
||||
return;
|
||||
}
|
||||
|
||||
const currentItems = this.cartItems();
|
||||
const updatedItems = currentItems.map(item =>
|
||||
item.itemID === itemID ? { ...item, quantity } : item
|
||||
this.isSameCartLine(item, itemID, variant) ? { ...item, quantity } : item
|
||||
);
|
||||
this.cartItems.set(updatedItems);
|
||||
}
|
||||
@@ -261,8 +277,19 @@ export class CartService {
|
||||
this.cartItems.set(updatedItems);
|
||||
}
|
||||
|
||||
removeItem(itemID: number): void {
|
||||
this.removeItems([itemID]);
|
||||
removeItem(itemID: number, variant?: CartVariant): void {
|
||||
if (!variant) {
|
||||
this.removeItems([itemID]);
|
||||
return;
|
||||
}
|
||||
|
||||
const lineIndex = this.findCartLineIndex(itemID, variant);
|
||||
if (lineIndex === -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
const updatedItems = this.cartItems().filter((_item, index) => index !== lineIndex);
|
||||
this.cartItems.set(updatedItems);
|
||||
}
|
||||
|
||||
clearCart(): void {
|
||||
|
||||
Reference in New Issue
Block a user