All files / src/app/extensions/rating/store/product-reviews product-reviews.effects.ts

100% Statements 23/23
80% Branches 8/10
100% Functions 10/10
100% Lines 22/22

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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 872x 2x 2x   2x 2x 2x   2x   2x                         2x 10x   10x 10x         7x 4x             10x 10x         6x 3x                                     10x 10x       5x 2x                            
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { map, mergeMap } from 'rxjs/operators';
 
import { displayInfoMessage, displaySuccessMessage } from 'ish-core/store/core/messages';
import { loadProduct } from 'ish-core/store/shopping/products';
import { mapErrorToAction, mapToPayload, mapToPayloadProperty, whenTruthy } from 'ish-core/utils/operators';
 
import { ReviewsService } from '../../services/reviews/reviews.service';
 
import {
  createProductReview,
  createProductReviewFail,
  createProductReviewSuccess,
  deleteProductReview,
  deleteProductReviewFail,
  deleteProductReviewSuccess,
  loadProductReviews,
  loadProductReviewsFail,
  loadProductReviewsSuccess,
} from './product-reviews.actions';
 
@Injectable()
export class ProductReviewsEffects {
  constructor(private actions$: Actions, private reviewService: ReviewsService) {}
 
  loadProductReviews$ = createEffect(() =>
    this.actions$.pipe(
      ofType(loadProductReviews),
      mapToPayloadProperty('sku'),
      whenTruthy(),
      mergeMap(sku =>
        this.reviewService.getProductReviews(sku).pipe(
          map(reviews => loadProductReviewsSuccess({ reviews })),
          mapErrorToAction(loadProductReviewsFail)
        )
      )
    )
  );
 
  createProductReview$ = createEffect(() =>
    this.actions$.pipe(
      ofType(createProductReview),
      mapToPayload(),
      whenTruthy(),
      mergeMap(payload =>
        this.reviewService.createProductReview(payload.sku, payload.review).pipe(
          mergeMap(reviews => [
            createProductReviewSuccess({ reviews }),
            reviews.reviews?.[0]?.status === 'NEW'
              ? displayInfoMessage({
                  message: 'product.reviews.create.approval.message',
                  messageParams: { 0: reviews.reviews?.[0]?.title },
                })
              : displaySuccessMessage({
                  message: 'product.reviews.create.success.message',
                  messageParams: { 0: reviews.reviews?.[0]?.title },
                }),
            loadProduct({ sku: payload.sku }),
          ]),
          mapErrorToAction(createProductReviewFail)
        )
      )
    )
  );
 
  deleteProductReview$ = createEffect(() =>
    this.actions$.pipe(
      ofType(deleteProductReview),
      mapToPayload(),
      mergeMap(payload =>
        this.reviewService.deleteProductReview(payload.sku, payload.review?.id).pipe(
          mergeMap(() => [
            deleteProductReviewSuccess(payload),
            displaySuccessMessage({
              message: 'product.reviews.delete.success.message',
              messageParams: { 0: payload.review.title },
            }),
            loadProduct({ sku: payload.sku }),
          ]),
          mapErrorToAction(deleteProductReviewFail)
        )
      )
    )
  );
}