juice-shop
120 строк · 4.7 Кб
1/*
2* Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
3* SPDX-License-Identifier: MIT
4*/
5
6import { ConfigurationService } from '../Services/configuration.service'
7import { UserService } from '../Services/user.service'
8import { RecycleService } from '../Services/recycle.service'
9import { Component, type OnInit, ViewChild } from '@angular/core'
10import { UntypedFormControl, Validators } from '@angular/forms'
11import { library } from '@fortawesome/fontawesome-svg-core'
12import { faPaperPlane } from '@fortawesome/free-solid-svg-icons'
13import { FormSubmitService } from '../Services/form-submit.service'
14import { AddressComponent } from '../address/address.component'
15import { TranslateService } from '@ngx-translate/core'
16import { SnackBarHelperService } from '../Services/snack-bar-helper.service'
17
18library.add(faPaperPlane)
19
20@Component({
21selector: 'app-recycle',
22templateUrl: './recycle.component.html',
23styleUrls: ['./recycle.component.scss']
24})
25export class RecycleComponent implements OnInit {
26@ViewChild('addressComp', { static: true }) public addressComponent: AddressComponent
27public requestorControl: UntypedFormControl = new UntypedFormControl({ value: '', disabled: true }, [])
28public recycleQuantityControl: UntypedFormControl = new UntypedFormControl('', [Validators.required, Validators.min(10), Validators.max(1000)])
29public pickUpDateControl: UntypedFormControl = new UntypedFormControl()
30public pickup: UntypedFormControl = new UntypedFormControl(false)
31public topImage?: string
32public bottomImage?: string
33public recycles: any
34public recycle: any = {}
35public userEmail: any
36public confirmation: any
37public addressId: any = undefined
38constructor (private readonly recycleService: RecycleService, private readonly userService: UserService,
39private readonly configurationService: ConfigurationService, private readonly formSubmitService: FormSubmitService,
40private readonly translate: TranslateService, private readonly snackBarHelperService: SnackBarHelperService) { }
41
42ngOnInit () {
43this.configurationService.getApplicationConfiguration().subscribe((config: any) => {
44if (config?.application?.recyclePage) {
45// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
46this.topImage = `assets/public/images/products/${config.application.recyclePage.topProductImage}`
47// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
48this.bottomImage = `assets/public/images/products/${config.application.recyclePage.bottomProductImage}`
49}
50}, (err) => { console.log(err) })
51
52this.initRecycle()
53this.findAll()
54
55this.formSubmitService.attachEnterKeyHandler('recycle-form', 'recycleButton', () => { this.save() })
56}
57
58initRecycle () {
59this.userService.whoAmI().subscribe((data) => {
60this.recycle = {}
61this.recycle.UserId = data.id
62this.userEmail = data.email
63this.requestorControl.setValue(this.userEmail)
64}, (err) => { console.log(err) })
65}
66
67save () {
68this.recycle.AddressId = this.addressId
69this.recycle.quantity = this.recycleQuantityControl.value
70if (this.pickup.value) {
71this.recycle.isPickUp = this.pickup.value
72this.recycle.date = this.pickUpDateControl.value
73}
74
75this.recycleService.save(this.recycle).subscribe((savedRecycle: any) => {
76if (savedRecycle.isPickup) {
77this.translate.get('CONFIRM_RECYCLING_PICKUP', { pickupdate: savedRecycle.pickupDate }).subscribe((confirmRecyclingPickup) => {
78this.snackBarHelperService.open(confirmRecyclingPickup, 'confirmBar')
79}, (translationId) => {
80this.snackBarHelperService.open(translationId, 'confirmBar')
81})
82} else {
83this.translate.get('CONFIRM_RECYCLING_BOX').subscribe((confirmRecyclingBox) => {
84this.snackBarHelperService.open(confirmRecyclingBox, 'confirmBar')
85}, (translationId) => {
86this.snackBarHelperService.open(translationId, 'confirmBar')
87})
88}
89this.addressComponent.load()
90this.initRecycle()
91this.resetForm()
92}, (err) => {
93this.snackBarHelperService.open(err.error?.error, 'errorBar')
94console.log(err)
95})
96}
97
98findAll () {
99this.recycleService.find().subscribe((recycles) => {
100this.recycles = recycles
101}, (error) => {
102console.log(error)
103})
104}
105
106resetForm () {
107this.addressId = undefined
108this.recycleQuantityControl.setValue('')
109this.recycleQuantityControl.markAsPristine()
110this.recycleQuantityControl.markAsUntouched()
111this.pickUpDateControl.setValue('')
112this.pickUpDateControl.markAsPristine()
113this.pickUpDateControl.markAsUntouched()
114this.pickup.setValue(false)
115}
116
117getMessage (id) {
118this.addressId = id
119}
120}
121