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

82.85% Statements 29/35
66.66% Branches 6/9
100% Functions 7/7
81.81% Lines 27/33

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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 1664x                   4x 4x 4x 4x 4x     4x           4x 4x               4x                     5x     5x 1x 1x 1x                               5x             5x 5x 5x 5x       5x     5x   10x       5x       5x                   5x                                                                                 5x                                            
import {
  ChangeDetectionStrategy,
  Component,
  ElementRef,
  Inject,
  Input,
  OnChanges,
  OnDestroy,
  ViewChild,
} from '@angular/core';
import { isEqual } from 'lodash-es';
import { Observable, combineLatest, of } from 'rxjs';
import { distinctUntilChanged, map } from 'rxjs/operators';
import Swiper from 'swiper';
import { A11y, Navigation, Pagination } from 'swiper/modules';
import { SwiperOptions } from 'swiper/types';
 
import {
  LARGE_BREAKPOINT_WIDTH,
  MEDIUM_BREAKPOINT_WIDTH,
  SMALL_BREAKPOINT_WIDTH,
} from 'ish-core/configurations/injection-keys';
import { ProductContextDisplayProperties } from 'ish-core/facades/product-context.facade';
import { ShoppingFacade } from 'ish-core/facades/shopping.facade';
import { InjectSingle } from 'ish-core/utils/injection';
import { ProductItemDisplayType } from 'ish-shared/components/product/product-item/product-item.component';
 
@Component({
  selector: 'ish-products-list',
  templateUrl: './products-list.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ProductsListComponent implements OnChanges, OnDestroy {
  @Input({ required: true }) productSKUs: string[];
  @Input() listStyle: string;
  @Input() slideItems: number;
  @Input() listItemStyle: ProductItemDisplayType;
  @Input() listItemCSSClass: string;
  @Input() listItemConfiguration: Partial<ProductContextDisplayProperties>;
 
  productSKUs$: Observable<string[]>;
 
  private swiper: Swiper;
  private swiperInitialized = false;
 
  @ViewChild('swiper') set swiperRef(ref: ElementRef) {
    if (ref && !this.swiperInitialized && !SSR) {
      this.swiperInitialized = true;
      const swiperEl = ref.nativeElement;
      this.swiper = new Swiper(swiperEl, {
        modules: [A11y, Navigation, Pagination],
        ...this.swiperConfig,
        navigation: {
          nextEl: swiperEl.querySelector('.swiper-button-next'),
          prevEl: swiperEl.querySelector('.swiper-button-prev'),
        },
        pagination: {
          el: swiperEl.querySelector('.swiper-pagination'),
          clickable: true,
        },
      });
    }
  }
 
  // configuration of swiper carousel: https://swiperjs.com/swiper-api
  private swiperConfig: SwiperOptions = {
    watchSlidesProgress: true,
    direction: 'horizontal',
    breakpoints: {},
  };
 
  constructor(
    @Inject(SMALL_BREAKPOINT_WIDTH) private smallBreakpointWidth: InjectSingle<typeof SMALL_BREAKPOINT_WIDTH>,
    @Inject(MEDIUM_BREAKPOINT_WIDTH) private mediumBreakpointWidth: InjectSingle<typeof MEDIUM_BREAKPOINT_WIDTH>,
    @Inject(LARGE_BREAKPOINT_WIDTH) private largeBreakpointWidth: InjectSingle<typeof LARGE_BREAKPOINT_WIDTH>,
    private shoppingFacade: ShoppingFacade
  ) {}
 
  ngOnChanges(): void {
    this.configureSlides(this.slideItems);
 
    // remove all SKUs from the productSKUs Array that are also contained in the failed products Array
    this.productSKUs$ = combineLatest([of(this.productSKUs), this.shoppingFacade.failedProducts$]).pipe(
      distinctUntilChanged<[string[], string[]]>(isEqual),
      map(([skus, failed]) => skus.filter(x => !failed.includes(x)))
    );
 
    // update swiper when slides change
    this.swiper?.update();
  }
 
  ngOnDestroy(): void {
    this.swiper?.destroy();
  }
 
  /**
   * Configure Swiper slidesPerView/slidesPerGroup settings
   * with breakpoint responsive design considerations based on the given slide items.
   *
   * @param slideItems The amount of slide items that should be rendered if enough screen space is available.
   */
  private configureSlides(slideItems: number) {
    switch (slideItems) {
      case 1: {
        this.swiperConfig.breakpoints = {
          0: {
            slidesPerView: 1,
            slidesPerGroup: 1,
          },
        };
        break;
      }
      case 2: {
        this.swiperConfig.breakpoints = {
          0: {
            slidesPerView: 1,
            slidesPerGroup: 1,
          },
          [this.smallBreakpointWidth]: {
            slidesPerView: 2,
            slidesPerGroup: 2,
          },
        };
        break;
      }
      case 3: {
        this.swiperConfig.breakpoints = {
          0: {
            slidesPerView: 1,
            slidesPerGroup: 1,
          },
          [this.smallBreakpointWidth]: {
            slidesPerView: 2,
            slidesPerGroup: 2,
          },
          [this.mediumBreakpointWidth]: {
            slidesPerView: 3,
            slidesPerGroup: 3,
          },
        };
        break;
      }
      default: {
        this.swiperConfig.breakpoints = {
          0: {
            slidesPerView: 1,
            slidesPerGroup: 1,
          },
          [this.smallBreakpointWidth]: {
            slidesPerView: 2,
            slidesPerGroup: 2,
          },
          [this.mediumBreakpointWidth]: {
            slidesPerView: 3,
            slidesPerGroup: 3,
          },
          [this.largeBreakpointWidth]: {
            slidesPerView: 4,
            slidesPerGroup: 4,
          },
        };
      }
    }
  }
}