|
| 1 | +import { Component, OnInit } from '@angular/core'; |
| 2 | +import { CartItem } from 'src/app/common/cart-item'; |
| 3 | +import { CartService } from 'src/app/services/cart.service'; |
| 4 | +import { SessionStorageService } from 'ngx-webstorage'; |
| 5 | + |
| 6 | +@Component({ |
| 7 | + selector: 'app-cart-details', |
| 8 | + templateUrl: './cart-details.component.html', |
| 9 | + styleUrls: ['./cart-details.component.css'] |
| 10 | +}) |
| 11 | +export class CartDetailsComponent implements OnInit { |
| 12 | + |
| 13 | + cartItems: CartItem[] = []; |
| 14 | + totalPrice:number; |
| 15 | + totalQuantity:number; |
| 16 | + |
| 17 | + constructor(private cartService: CartService, |
| 18 | + private storage: SessionStorageService) { } |
| 19 | + |
| 20 | + ngOnInit(): void { |
| 21 | + |
| 22 | + if(this.storage.retrieve('totalPrice') === undefined ){ |
| 23 | + this.totalPrice = 0.00; |
| 24 | + } |
| 25 | + else { |
| 26 | + this.totalPrice = this.storage.retrieve('totalPrice'); |
| 27 | + } |
| 28 | + |
| 29 | + if(this.storage.retrieve('totalQuantity') === undefined ){ |
| 30 | + this.totalQuantity = 0.00; |
| 31 | + } |
| 32 | + else { |
| 33 | + this.totalQuantity = this.storage.retrieve('totalQuantity'); |
| 34 | + } |
| 35 | + |
| 36 | + this.listCartDetails(); |
| 37 | + } |
| 38 | + listCartDetails() { |
| 39 | + |
| 40 | + this.cartItems = this.cartService.cartItems; |
| 41 | + |
| 42 | + this.cartService.totalPrice.subscribe( |
| 43 | + data => this.totalPrice = data |
| 44 | + ); |
| 45 | + |
| 46 | + this.cartService.totalQuantity.subscribe( |
| 47 | + data => this.totalQuantity = data |
| 48 | + ); |
| 49 | + |
| 50 | + this.cartService.computeCartTotals(); |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + incrementQuantity(cartItem:CartItem){ |
| 55 | + this.cartService.addToCart(cartItem); |
| 56 | + } |
| 57 | + |
| 58 | + decrementQuantity(cartItem:CartItem){ |
| 59 | + this.cartService.decrementQuantity(cartItem); |
| 60 | + } |
| 61 | + |
| 62 | + removeItem(cartItem:CartItem){ |
| 63 | + this.cartService.removeFromCart(cartItem); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | +} |
0 commit comments