juice-shop
100 строк · 3.6 Кб
1/*
2* Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
3* SPDX-License-Identifier: MIT
4*/
5
6import { Component, EventEmitter, Input, type OnInit, Output } from '@angular/core'
7import { BasketService } from '../Services/basket.service'
8import { UserService } from '../Services/user.service'
9import { library } from '@fortawesome/fontawesome-svg-core'
10import { faTrashAlt } from '@fortawesome/free-regular-svg-icons/'
11import { faMinusSquare, faPlusSquare } from '@fortawesome/free-solid-svg-icons'
12import { DeluxeGuard } from '../app.guard'
13import { SnackBarHelperService } from '../Services/snack-bar-helper.service'
14
15library.add(faTrashAlt, faMinusSquare, faPlusSquare)
16
17@Component({
18selector: 'app-purchase-basket',
19templateUrl: './purchase-basket.component.html',
20styleUrls: ['./purchase-basket.component.scss']
21})
22export class PurchaseBasketComponent implements OnInit {
23@Input('allowEdit') public allowEdit: boolean = false
24@Input('displayTotal') public displayTotal: boolean = false
25@Input('totalPrice') public totalPrice: boolean = true
26@Output() emitTotal = new EventEmitter()
27@Output() emitProductCount = new EventEmitter()
28public tableColumns = ['image', 'product', 'quantity', 'price']
29public dataSource = []
30public bonus = 0
31public itemTotal = 0
32public userEmail: string
33constructor (private readonly deluxeGuard: DeluxeGuard, private readonly basketService: BasketService,
34private readonly userService: UserService, private readonly snackBarHelperService: SnackBarHelperService) { }
35
36ngOnInit () {
37if (this.allowEdit && !this.tableColumns.includes('remove')) {
38this.tableColumns.push('remove')
39}
40this.load()
41this.userService.whoAmI().subscribe((data) => {
42this.userEmail = data.email || 'anonymous'
43this.userEmail = '(' + this.userEmail + ')'
44}, (err) => { console.log(err) })
45}
46
47load () {
48this.basketService.find(parseInt(sessionStorage.getItem('bid'), 10)).subscribe((basket) => {
49if (this.isDeluxe()) {
50basket.Products.forEach(product => {
51product.price = product.deluxePrice
52})
53}
54this.dataSource = basket.Products
55// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
56this.itemTotal = basket.Products.reduce((itemTotal, product) => itemTotal + product.price * product.BasketItem.quantity, 0)
57// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
58this.bonus = basket.Products.reduce((bonusPoints, product) => bonusPoints + Math.round(product.price / 10) * product.BasketItem.quantity, 0)
59this.sendToParent(this.dataSource.length)
60}, (err) => { console.log(err) })
61}
62
63delete (id) {
64this.basketService.del(id).subscribe(() => {
65this.load()
66this.basketService.updateNumberOfCartItems()
67}, (err) => { console.log(err) })
68}
69
70inc (id) {
71this.addToQuantity(id, 1)
72}
73
74dec (id) {
75this.addToQuantity(id, -1)
76}
77
78addToQuantity (id, value) {
79this.basketService.get(id).subscribe((basketItem) => {
80// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
81const newQuantity = basketItem.quantity + value
82this.basketService.put(id, { quantity: newQuantity < 1 ? 1 : newQuantity }).subscribe(() => {
83this.load()
84this.basketService.updateNumberOfCartItems()
85}, (err) => {
86this.snackBarHelperService.open(err.error?.error, 'errorBar')
87console.log(err)
88})
89}, (err) => { console.log(err) })
90}
91
92sendToParent (count) {
93this.emitTotal.emit([this.itemTotal, this.bonus])
94this.emitProductCount.emit(count)
95}
96
97isDeluxe () {
98return this.deluxeGuard.isDeluxe()
99}
100}
101