feat: update cart service and components to handle productVariantId, improve item removal and addition feedback
This commit is contained in:
@@ -8,6 +8,7 @@ import { CartService } from '../../../core/services/cart/cart.service';
|
||||
import { ToastService } from '../../../core/services/toast.service';
|
||||
|
||||
export interface CartItemMock {
|
||||
productVariantId?: number;
|
||||
imageUrl: string | null;
|
||||
product: string;
|
||||
originalPrice: number | null;
|
||||
@@ -68,19 +69,29 @@ export class CartComponent {
|
||||
}
|
||||
|
||||
protected onItemQuantityChange(index: number, newQuantity: number): void {
|
||||
const item = this.cartService.cart()?.items[index];
|
||||
if (item) {
|
||||
const mockItem = this.items()[index];
|
||||
const productVariantId = mockItem?.productVariantId;
|
||||
if (productVariantId) {
|
||||
this.quantityUpdates$.next({
|
||||
productVariantId: item.product_variant_id,
|
||||
productVariantId,
|
||||
quantity: newQuantity
|
||||
});
|
||||
} else {
|
||||
const item = this.cartService.cart()?.items[index];
|
||||
if (item) {
|
||||
this.quantityUpdates$.next({
|
||||
productVariantId: item.product_variant_id,
|
||||
quantity: newQuantity
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected onItemRemove(index: number): void {
|
||||
const item = this.cartService.cart()?.items[index];
|
||||
if (item) {
|
||||
this.cartService.removeItem(item.product_variant_id).subscribe({
|
||||
const mockItem = this.items()[index];
|
||||
const productVariantId = mockItem?.productVariantId;
|
||||
if (productVariantId) {
|
||||
this.cartService.removeItem(productVariantId).subscribe({
|
||||
next: (res) => {
|
||||
const msg = res.message || 'Producto eliminado del carrito.';
|
||||
this.toastService.success(msg);
|
||||
@@ -91,6 +102,21 @@ export class CartComponent {
|
||||
this.toastService.danger(msg);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
const item = this.cartService.cart()?.items[index];
|
||||
if (item) {
|
||||
this.cartService.removeItem(item.product_variant_id).subscribe({
|
||||
next: (res) => {
|
||||
const msg = res.message || 'Producto eliminado del carrito.';
|
||||
this.toastService.success(msg);
|
||||
},
|
||||
error: (err: HttpErrorResponse) => {
|
||||
console.error('Error removing item from cart', err);
|
||||
const msg = err.error?.message || 'Error al eliminar el producto del carrito.';
|
||||
this.toastService.danger(msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user