All files / src/app/extensions/rating/shared/product-reviews product-reviews.component.ts

100% Statements 30/30
78.57% Branches 11/14
100% Functions 12/12
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 681x 1x 1x 1x   1x 1x   1x   1x                   1x               10x     10x 10x 10x       9x 9x   9x 9x       9x 9x 9x   9x   9x 9x       45x       1x       10x      
import { ChangeDetectionStrategy, Component, OnDestroy, OnInit } from '@angular/core';
import { range } from 'lodash-es';
import { Observable, combineLatest, switchMap } from 'rxjs';
import { map, shareReplay } from 'rxjs/operators';
 
import { AccountFacade } from 'ish-core/facades/account.facade';
import { ProductContextFacade } from 'ish-core/facades/product-context.facade';
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { GenerateLazyComponent } from 'ish-core/utils/module-loader/generate-lazy-component.decorator';
 
import { ProductReviewsFacade } from '../../facades/product-reviews.facade';
import { ProductReview } from '../../models/product-reviews/product-review.model';
import { RatingFilledType } from '../product-rating-star/product-rating-star.component';
 
@Component({
  selector: 'ish-product-reviews',
  templateUrl: './product-reviews.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
@GenerateLazyComponent()
export class ProductReviewsComponent implements OnInit, OnDestroy {
  recentProductReviews$: Observable<ProductReview[]>;
  ownProductReview$: Observable<ProductReview>;
 
  error$: Observable<HttpError>;
  loading$: Observable<boolean>;
  isUserLoggedIn$: Observable<boolean>;
 
  private maxReviewItems = 10;
 
  constructor(
    private accountFacade: AccountFacade,
    private context: ProductContextFacade,
    private productReviewsFacade: ProductReviewsFacade
  ) {}
 
  ngOnInit() {
    const sku$ = this.context.select('sku').pipe(shareReplay(1));
    this.isUserLoggedIn$ = this.accountFacade.isLoggedIn$;
 
    const productReviews$ = combineLatest([sku$, this.isUserLoggedIn$]).pipe(
      switchMap(([sku]) => this.productReviewsFacade.getProductReviews$(sku)),
      shareReplay(1)
    );
 
    this.recentProductReviews$ = productReviews$.pipe(
      map(reviews => reviews?.filter(review => !review.own)),
      map(reviews => reviews?.slice(0, this.maxReviewItems))
    );
    this.ownProductReview$ = productReviews$.pipe(map(reviews => reviews?.find(review => review.own)));
 
    this.error$ = this.productReviewsFacade.productReviewsError$;
    this.loading$ = this.productReviewsFacade.productReviewsLoading$;
  }
 
  getStars(rating: number): RatingFilledType[] {
    return range(1, 6).map(index => (index <= rating ? 'full' : 'empty'));
  }
 
  deleteReview(review: ProductReview) {
    this.productReviewsFacade.deleteProductReview(this.context.get('sku'), review);
  }
 
  ngOnDestroy() {
    this.productReviewsFacade.resetProductReviewError();
  }
}