cart.component.ts (1380B)
1 import { Component, inject, signal } from '@angular/core'; 2 import { CartService } from '../services/cart.service'; 3 import { CommonModule } from '@angular/common'; 4 import { Router, RouterLink } from '@angular/router'; 5 6 @Component({ 7 selector: 'aside.cart', 8 templateUrl: './cart.component.html', 9 styleUrls: ['./cart.component.scss'], 10 standalone: true, 11 imports: [CommonModule, RouterLink], 12 }) 13 export class CartComponent { 14 private router = inject(Router); 15 readonly cartService = inject(CartService); 16 readonly checkoutInProgress = signal(false); 17 constructor() { 18 this.router.events.pipe().subscribe(() => { 19 this.checkoutInProgress.set(this.router.url === '/checkout'); 20 }); 21 } 22 23 updateQuantity(item: CartItem, ev: Event): void { 24 ev.preventDefault(); 25 ev.stopPropagation(); 26 this.cartService.updateQuantity(item.product.eid, item.quantity + 1); 27 } 28 29 discount(product: ProductCard): number { 30 const initial = parseFloat(product.initial_price || '0'); 31 const discount = parseFloat(product.discount || '0'); 32 return initial > 0 ? Math.round((discount / initial) * 100) : 0; 33 } 34 35 removeItem(productId: string, ev: Event): void { 36 ev.preventDefault(); 37 ev.stopPropagation(); 38 this.cartService.removeFromCart(productId); 39 } 40 41 clearCart(): void { 42 this.cartService.clearCart(); 43 this.router.navigate(['/products']); 44 } 45 }