All files / src/app/pages/account-order/account-order-to-basket account-order-to-basket.component.ts

100% Statements 16/16
72.72% Branches 8/11
100% Functions 4/4
100% Lines 15/15

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 442x 2x 2x   2x 2x 2x                         2x           4x   4x 4x 4x           2x 2x 3x 2x          
import { ChangeDetectionStrategy, Component, Input, Signal } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { map } from 'rxjs';
 
import { CheckoutFacade } from 'ish-core/facades/checkout.facade';
import { ShoppingFacade } from 'ish-core/facades/shopping.facade';
import { Order } from 'ish-core/models/order/order.model';
 
/**
 * The Order to Basket Component displays a button that adds the current order to the basket.
 *
 * @example
 * <ish-account-order-to-basket [order]="order" />
 */
@Component({
  selector: 'ish-account-order-to-basket',
  templateUrl: './account-order-to-basket.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AccountOrderToBasketComponent {
  @Input({ required: true }) order: Order;
 
  @Input() cssClass: string;
 
  displaySpinner: Signal<boolean>;
  private initialLoading = true;
 
  constructor(private checkoutFacade: CheckoutFacade, private shoppingFacade: ShoppingFacade) {
    this.displaySpinner = toSignal(
      this.checkoutFacade.basketLoading$.pipe(map(loading => loading && !this.initialLoading)),
      { initialValue: false }
    );
  }
 
  addOrderToBasket() {
    this.initialLoading = false;
    this.order.lineItems.forEach(lineItem => {
      if (!lineItem.isFreeGift) {
        this.shoppingFacade.addProductToBasket(lineItem.productSKU, lineItem.quantity.value);
      }
    });
  }
}