All files / src/app/shared/cms/components/cms-product-list-rest cms-product-list-rest.component.ts

100% Statements 14/14
87.5% Branches 7/8
100% Functions 4/4
100% Lines 14/14

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  1x 1x   1x   1x               1x         4x     3x 3x         3x   3x       3x         3x 1x     3x          
// eslint-disable-next-line ish-custom-rules/ban-imports-file-pattern
import { HttpClient } from '@angular/common/http';
import { ChangeDetectionStrategy, Component, Input, OnChanges } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
 
import { ContentPageletView } from 'ish-core/models/content-view/content-view.model';
import { CMSComponent } from 'ish-shared/cms/models/cms-component/cms-component.model';
 
@Component({
  selector: 'ish-cms-product-list-rest',
  templateUrl: './cms-product-list-rest.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CMSProductListRestComponent implements CMSComponent, OnChanges {
  @Input({ required: true }) pagelet: ContentPageletView;
 
  productSKUs$: Observable<string[]>;
 
  constructor(private httpClient: HttpClient) {}
 
  ngOnChanges() {
    if (this.pagelet.hasParam('ProductsRestEndpoint')) {
      this.productSKUs$ = this.getProductSKUs$();
    }
  }
 
  private getProductSKUs$(): Observable<string[]> {
    return this.httpClient.get<unknown>(this.pagelet.stringParam('ProductsRestEndpoint')).pipe(
      map(data => {
        let skus: string[] = [];
 
        // if the REST response is not already an Array of SKUs
        // a given mapper function can be applied to the REST 'data' to map the data to an Array of SKUs
        skus = this.pagelet.hasParam('ProductsRestResponseMapper')
          ? Function('data', `"use strict"; return ${this.pagelet.stringParam('ProductsRestResponseMapper')}`)(data)
          : data;
 
        // limit the number of rendered products
        if (this.pagelet.hasParam('MaxNumberOfProducts')) {
          skus = skus.splice(0, this.pagelet.numberParam('MaxNumberOfProducts'));
        }
 
        return skus;
      })
    );
  }
}