juice-shop

Форк
0
/
purchase-basket.component.ts 
100 строк · 3.6 Кб
1
/*
2
 * Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
3
 * SPDX-License-Identifier: MIT
4
 */
5

6
import { Component, EventEmitter, Input, type OnInit, Output } from '@angular/core'
7
import { BasketService } from '../Services/basket.service'
8
import { UserService } from '../Services/user.service'
9
import { library } from '@fortawesome/fontawesome-svg-core'
10
import { faTrashAlt } from '@fortawesome/free-regular-svg-icons/'
11
import { faMinusSquare, faPlusSquare } from '@fortawesome/free-solid-svg-icons'
12
import { DeluxeGuard } from '../app.guard'
13
import { SnackBarHelperService } from '../Services/snack-bar-helper.service'
14

15
library.add(faTrashAlt, faMinusSquare, faPlusSquare)
16

17
@Component({
18
  selector: 'app-purchase-basket',
19
  templateUrl: './purchase-basket.component.html',
20
  styleUrls: ['./purchase-basket.component.scss']
21
})
22
export 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()
28
  public tableColumns = ['image', 'product', 'quantity', 'price']
29
  public dataSource = []
30
  public bonus = 0
31
  public itemTotal = 0
32
  public userEmail: string
33
  constructor (private readonly deluxeGuard: DeluxeGuard, private readonly basketService: BasketService,
34
    private readonly userService: UserService, private readonly snackBarHelperService: SnackBarHelperService) { }
35

36
  ngOnInit () {
37
    if (this.allowEdit && !this.tableColumns.includes('remove')) {
38
      this.tableColumns.push('remove')
39
    }
40
    this.load()
41
    this.userService.whoAmI().subscribe((data) => {
42
      this.userEmail = data.email || 'anonymous'
43
      this.userEmail = '(' + this.userEmail + ')'
44
    }, (err) => { console.log(err) })
45
  }
46

47
  load () {
48
    this.basketService.find(parseInt(sessionStorage.getItem('bid'), 10)).subscribe((basket) => {
49
      if (this.isDeluxe()) {
50
        basket.Products.forEach(product => {
51
          product.price = product.deluxePrice
52
        })
53
      }
54
      this.dataSource = basket.Products
55
      // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
56
      this.itemTotal = basket.Products.reduce((itemTotal, product) => itemTotal + product.price * product.BasketItem.quantity, 0)
57
      // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
58
      this.bonus = basket.Products.reduce((bonusPoints, product) => bonusPoints + Math.round(product.price / 10) * product.BasketItem.quantity, 0)
59
      this.sendToParent(this.dataSource.length)
60
    }, (err) => { console.log(err) })
61
  }
62

63
  delete (id) {
64
    this.basketService.del(id).subscribe(() => {
65
      this.load()
66
      this.basketService.updateNumberOfCartItems()
67
    }, (err) => { console.log(err) })
68
  }
69

70
  inc (id) {
71
    this.addToQuantity(id, 1)
72
  }
73

74
  dec (id) {
75
    this.addToQuantity(id, -1)
76
  }
77

78
  addToQuantity (id, value) {
79
    this.basketService.get(id).subscribe((basketItem) => {
80
      // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
81
      const newQuantity = basketItem.quantity + value
82
      this.basketService.put(id, { quantity: newQuantity < 1 ? 1 : newQuantity }).subscribe(() => {
83
        this.load()
84
        this.basketService.updateNumberOfCartItems()
85
      }, (err) => {
86
        this.snackBarHelperService.open(err.error?.error, 'errorBar')
87
        console.log(err)
88
      })
89
    }, (err) => { console.log(err) })
90
  }
91

92
  sendToParent (count) {
93
    this.emitTotal.emit([this.itemTotal, this.bonus])
94
    this.emitProductCount.emit(count)
95
  }
96

97
  isDeluxe () {
98
    return this.deluxeGuard.isDeluxe()
99
  }
100
}
101

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.