juice-shop

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

6
import { ProductReviewEditComponent } from '../product-review-edit/product-review-edit.component'
7
import { UserService } from '../Services/user.service'
8
import { ProductReviewService } from '../Services/product-review.service'
9
import { Component, Inject, type OnDestroy, type OnInit } from '@angular/core'
10
import { MAT_DIALOG_DATA, MatDialog } from '@angular/material/dialog'
11
import { library } from '@fortawesome/fontawesome-svg-core'
12
import { faArrowCircleLeft, faCrown, faPaperPlane, faThumbsUp, faUserEdit } from '@fortawesome/free-solid-svg-icons'
13
import { UntypedFormControl, Validators } from '@angular/forms'
14
import { MatSnackBar } from '@angular/material/snack-bar'
15
import { SnackBarHelperService } from '../Services/snack-bar-helper.service'
16
import { type Review } from '../Models/review.model'
17
import { type Product } from '../Models/product.model'
18

19
library.add(faPaperPlane, faArrowCircleLeft, faUserEdit, faThumbsUp, faCrown)
20

21
@Component({
22
  selector: 'app-product-details',
23
  templateUrl: './product-details.component.html',
24
  styleUrls: ['./product-details.component.scss']
25
})
26
export class ProductDetailsComponent implements OnInit, OnDestroy {
27
  public author: string = 'Anonymous'
28
  public reviews$: any
29
  public userSubscription: any
30
  public reviewControl: UntypedFormControl = new UntypedFormControl('', [Validators.maxLength(160)])
31
  constructor (private readonly dialog: MatDialog,
32
    @Inject(MAT_DIALOG_DATA) public data: { productData: Product }, private readonly productReviewService: ProductReviewService,
33
    private readonly userService: UserService, private readonly snackBar: MatSnackBar, private readonly snackBarHelperService: SnackBarHelperService) { }
34

35
  ngOnInit () {
36
    this.data.productData.points = Math.round(this.data.productData.price / 10)
37
    this.reviews$ = this.productReviewService.get(this.data.productData.id)
38
    this.userSubscription = this.userService.whoAmI().subscribe((user: any) => {
39
      if (user?.email) {
40
        this.author = user.email
41
      } else {
42
        this.author = 'Anonymous'
43
      }
44
    }, (err) => { console.log(err) })
45
  }
46

47
  ngOnDestroy () {
48
    if (this.userSubscription) {
49
      this.userSubscription.unsubscribe()
50
    }
51
  }
52

53
  addReview (textPut: HTMLTextAreaElement) {
54
    const review = { message: textPut.value, author: this.author }
55

56
    textPut.value = ''
57
    this.productReviewService.create(this.data.productData.id, review).subscribe(() => {
58
      this.reviews$ = this.productReviewService.get(this.data.productData.id)
59
    }, (err) => { console.log(err) })
60
    this.snackBarHelperService.open('CONFIRM_REVIEW_SAVED')
61
  }
62

63
  editReview (review: Review) {
64
    this.dialog.open(ProductReviewEditComponent, {
65
      width: '500px',
66
      height: 'max-content',
67
      data: {
68
        reviewData: review
69
      }
70
    }).afterClosed().subscribe(() => (this.reviews$ = this.productReviewService.get(this.data.productData.id)))
71
  }
72

73
  likeReview (review: Review) {
74
    this.productReviewService.like(review._id).subscribe(() => {
75
      console.log('Liked ' + review._id)
76
    })
77
    setTimeout(() => (this.reviews$ = this.productReviewService.get(this.data.productData.id)), 200)
78
  }
79

80
  isLoggedIn () {
81
    return localStorage.getItem('token')
82
  }
83
}
84

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

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

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

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