-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcart.component.ts
46 lines (39 loc) · 1.11 KB
/
cart.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { Component } from '@angular/core';
import { CartService } from '../../services/cart.service';
import { Product } from '../../../data/products';
import { CurrencyPipe } from '@angular/common';
@Component({
selector: 'app-cart',
standalone: true,
imports: [CurrencyPipe],
templateUrl: './cart.component.html',
styleUrl: './cart.component.css',
})
export class CartComponent {
cartItems = this.cartService.getCart();
constructor(private cartService: CartService) {
cartService.cart$.subscribe({
next: (val: (Product & { quantity: number })[]) => {
this.cartItems = val;
}
})
}
increment(item: Product) {
this.cartService.incrementQuantity(item.id);
}
decrement(item: Product) {
this.cartService.decrementQuantity(item.id);
this.cartItems = this.cartService.getCart();
}
remove(item: Product) {
this.cartService.removeFromCart(item.id);
this.cartItems = this.cartService.getCart();
}
clearCart() {
this.cartService.clearCart();
this.cartItems = this.cartService.getCart();
}
getTotal() {
return this.cartService.getTotal();
}
}