juice-shop
83 строки · 3.3 Кб
1/*
2* Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
3* SPDX-License-Identifier: MIT
4*/
5
6import { ProductReviewEditComponent } from '../product-review-edit/product-review-edit.component'
7import { UserService } from '../Services/user.service'
8import { ProductReviewService } from '../Services/product-review.service'
9import { Component, Inject, type OnDestroy, type OnInit } from '@angular/core'
10import { MAT_DIALOG_DATA, MatDialog } from '@angular/material/dialog'
11import { library } from '@fortawesome/fontawesome-svg-core'
12import { faArrowCircleLeft, faCrown, faPaperPlane, faThumbsUp, faUserEdit } from '@fortawesome/free-solid-svg-icons'
13import { UntypedFormControl, Validators } from '@angular/forms'
14import { MatSnackBar } from '@angular/material/snack-bar'
15import { SnackBarHelperService } from '../Services/snack-bar-helper.service'
16import { type Review } from '../Models/review.model'
17import { type Product } from '../Models/product.model'
18
19library.add(faPaperPlane, faArrowCircleLeft, faUserEdit, faThumbsUp, faCrown)
20
21@Component({
22selector: 'app-product-details',
23templateUrl: './product-details.component.html',
24styleUrls: ['./product-details.component.scss']
25})
26export class ProductDetailsComponent implements OnInit, OnDestroy {
27public author: string = 'Anonymous'
28public reviews$: any
29public userSubscription: any
30public reviewControl: UntypedFormControl = new UntypedFormControl('', [Validators.maxLength(160)])
31constructor (private readonly dialog: MatDialog,
32@Inject(MAT_DIALOG_DATA) public data: { productData: Product }, private readonly productReviewService: ProductReviewService,
33private readonly userService: UserService, private readonly snackBar: MatSnackBar, private readonly snackBarHelperService: SnackBarHelperService) { }
34
35ngOnInit () {
36this.data.productData.points = Math.round(this.data.productData.price / 10)
37this.reviews$ = this.productReviewService.get(this.data.productData.id)
38this.userSubscription = this.userService.whoAmI().subscribe((user: any) => {
39if (user?.email) {
40this.author = user.email
41} else {
42this.author = 'Anonymous'
43}
44}, (err) => { console.log(err) })
45}
46
47ngOnDestroy () {
48if (this.userSubscription) {
49this.userSubscription.unsubscribe()
50}
51}
52
53addReview (textPut: HTMLTextAreaElement) {
54const review = { message: textPut.value, author: this.author }
55
56textPut.value = ''
57this.productReviewService.create(this.data.productData.id, review).subscribe(() => {
58this.reviews$ = this.productReviewService.get(this.data.productData.id)
59}, (err) => { console.log(err) })
60this.snackBarHelperService.open('CONFIRM_REVIEW_SAVED')
61}
62
63editReview (review: Review) {
64this.dialog.open(ProductReviewEditComponent, {
65width: '500px',
66height: 'max-content',
67data: {
68reviewData: review
69}
70}).afterClosed().subscribe(() => (this.reviews$ = this.productReviewService.get(this.data.productData.id)))
71}
72
73likeReview (review: Review) {
74this.productReviewService.like(review._id).subscribe(() => {
75console.log('Liked ' + review._id)
76})
77setTimeout(() => (this.reviews$ = this.productReviewService.get(this.data.productData.id)), 200)
78}
79
80isLoggedIn () {
81return localStorage.getItem('token')
82}
83}
84