juice-shop

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

6
import { ConfigurationService } from '../Services/configuration.service'
7
import { UserService } from '../Services/user.service'
8
import { RecycleService } from '../Services/recycle.service'
9
import { Component, type OnInit, ViewChild } from '@angular/core'
10
import { UntypedFormControl, Validators } from '@angular/forms'
11
import { library } from '@fortawesome/fontawesome-svg-core'
12
import { faPaperPlane } from '@fortawesome/free-solid-svg-icons'
13
import { FormSubmitService } from '../Services/form-submit.service'
14
import { AddressComponent } from '../address/address.component'
15
import { TranslateService } from '@ngx-translate/core'
16
import { SnackBarHelperService } from '../Services/snack-bar-helper.service'
17

18
library.add(faPaperPlane)
19

20
@Component({
21
  selector: 'app-recycle',
22
  templateUrl: './recycle.component.html',
23
  styleUrls: ['./recycle.component.scss']
24
})
25
export class RecycleComponent implements OnInit {
26
  @ViewChild('addressComp', { static: true }) public addressComponent: AddressComponent
27
  public requestorControl: UntypedFormControl = new UntypedFormControl({ value: '', disabled: true }, [])
28
  public recycleQuantityControl: UntypedFormControl = new UntypedFormControl('', [Validators.required, Validators.min(10), Validators.max(1000)])
29
  public pickUpDateControl: UntypedFormControl = new UntypedFormControl()
30
  public pickup: UntypedFormControl = new UntypedFormControl(false)
31
  public topImage?: string
32
  public bottomImage?: string
33
  public recycles: any
34
  public recycle: any = {}
35
  public userEmail: any
36
  public confirmation: any
37
  public addressId: any = undefined
38
  constructor (private readonly recycleService: RecycleService, private readonly userService: UserService,
39
    private readonly configurationService: ConfigurationService, private readonly formSubmitService: FormSubmitService,
40
    private readonly translate: TranslateService, private readonly snackBarHelperService: SnackBarHelperService) { }
41

42
  ngOnInit () {
43
    this.configurationService.getApplicationConfiguration().subscribe((config: any) => {
44
      if (config?.application?.recyclePage) {
45
        // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
46
        this.topImage = `assets/public/images/products/${config.application.recyclePage.topProductImage}`
47
        // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
48
        this.bottomImage = `assets/public/images/products/${config.application.recyclePage.bottomProductImage}`
49
      }
50
    }, (err) => { console.log(err) })
51

52
    this.initRecycle()
53
    this.findAll()
54

55
    this.formSubmitService.attachEnterKeyHandler('recycle-form', 'recycleButton', () => { this.save() })
56
  }
57

58
  initRecycle () {
59
    this.userService.whoAmI().subscribe((data) => {
60
      this.recycle = {}
61
      this.recycle.UserId = data.id
62
      this.userEmail = data.email
63
      this.requestorControl.setValue(this.userEmail)
64
    }, (err) => { console.log(err) })
65
  }
66

67
  save () {
68
    this.recycle.AddressId = this.addressId
69
    this.recycle.quantity = this.recycleQuantityControl.value
70
    if (this.pickup.value) {
71
      this.recycle.isPickUp = this.pickup.value
72
      this.recycle.date = this.pickUpDateControl.value
73
    }
74

75
    this.recycleService.save(this.recycle).subscribe((savedRecycle: any) => {
76
      if (savedRecycle.isPickup) {
77
        this.translate.get('CONFIRM_RECYCLING_PICKUP', { pickupdate: savedRecycle.pickupDate }).subscribe((confirmRecyclingPickup) => {
78
          this.snackBarHelperService.open(confirmRecyclingPickup, 'confirmBar')
79
        }, (translationId) => {
80
          this.snackBarHelperService.open(translationId, 'confirmBar')
81
        })
82
      } else {
83
        this.translate.get('CONFIRM_RECYCLING_BOX').subscribe((confirmRecyclingBox) => {
84
          this.snackBarHelperService.open(confirmRecyclingBox, 'confirmBar')
85
        }, (translationId) => {
86
          this.snackBarHelperService.open(translationId, 'confirmBar')
87
        })
88
      }
89
      this.addressComponent.load()
90
      this.initRecycle()
91
      this.resetForm()
92
    }, (err) => {
93
      this.snackBarHelperService.open(err.error?.error, 'errorBar')
94
      console.log(err)
95
    })
96
  }
97

98
  findAll () {
99
    this.recycleService.find().subscribe((recycles) => {
100
      this.recycles = recycles
101
    }, (error) => {
102
      console.log(error)
103
    })
104
  }
105

106
  resetForm () {
107
    this.addressId = undefined
108
    this.recycleQuantityControl.setValue('')
109
    this.recycleQuantityControl.markAsPristine()
110
    this.recycleQuantityControl.markAsUntouched()
111
    this.pickUpDateControl.setValue('')
112
    this.pickUpDateControl.markAsPristine()
113
    this.pickUpDateControl.markAsUntouched()
114
    this.pickup.setValue(false)
115
  }
116

117
  getMessage (id) {
118
    this.addressId = id
119
  }
120
}
121

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

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

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

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