All files / src/app/shared/components/line-item/line-item-list-element line-item-list-element.component.ts

61.11% Statements 11/18
58.33% Branches 7/12
33.33% Functions 2/6
58.82% Lines 10/17

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 572x 2x 2x     2x 2x                   2x   10x       10x   10x                                     10x                          
import { ChangeDetectionStrategy, Component, DestroyRef, Input, OnChanges, SimpleChanges, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { isEqual } from 'lodash-es';
import { Subscription } from 'rxjs';
 
import { CheckoutFacade } from 'ish-core/facades/checkout.facade';
import { ProductContextFacade } from 'ish-core/facades/product-context.facade';
import { LineItemUpdate } from 'ish-core/models/line-item-update/line-item-update.model';
import { LineItemView } from 'ish-core/models/line-item/line-item.model';
import { OrderLineItem } from 'ish-core/models/order/order.model';
 
@Component({
  selector: 'ish-line-item-list-element',
  templateUrl: './line-item-list-element.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class LineItemListElementComponent implements OnChanges {
  @Input({ required: true }) pli: Partial<LineItemView & OrderLineItem>;
  @Input() editable = true;
  @Input() lineItemViewType: 'simple' | 'availability';
 
  private updateSubscription: Subscription;
  private destroyRef = inject(DestroyRef);
 
  constructor(private context: ProductContextFacade, private checkoutFacade: CheckoutFacade) {}
 
  ngOnChanges(changes: SimpleChanges): void {
    Iif (changes.pli) {
      Iif (this.updateSubscription) {
        // eslint-disable-next-line ban/ban
        this.updateSubscription.unsubscribe();
      }
 
      this.updateSubscription = this.context
        .validDebouncedQuantityUpdate$()
        .pipe(takeUntilDestroyed(this.destroyRef))
        .subscribe(quantity => {
          this.checkoutFacade.updateBasketItem({ itemId: this.pli.id, quantity });
        });
    }
  }
 
  get oldPrice() {
    return isEqual(this.pli.singleBasePrice, this.pli.undiscountedSingleBasePrice)
      ? undefined
      : this.pli.undiscountedSingleBasePrice;
  }
 
  onUpdateItem(update: LineItemUpdate) {
    this.checkoutFacade.updateBasketItem(update);
  }
 
  onDeleteItem(itemId: string) {
    this.checkoutFacade.deleteBasketItem(itemId);
  }
}