All files / src/app/shell/header/mini-basket mini-basket.component.ts

76.31% Statements 29/38
66.66% Branches 12/18
41.66% Functions 5/12
78.37% Lines 29/37

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 1012x 2x                 2x 2x 2x   2x 2x     2x             2x           7x   7x     7x     7x 7x 7x 7x     4x 4x 4x 4x   4x               4x         4x               4x                 2x             1x 1x               2x 2x      
import { Location } from '@angular/common';
import {
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  DestroyRef,
  Input,
  OnInit,
  inject,
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { Observable, concat, of, timer } from 'rxjs';
import { filter, map, switchMap } from 'rxjs/operators';
 
import { AppFacade } from 'ish-core/facades/app.facade';
import { CheckoutFacade } from 'ish-core/facades/checkout.facade';
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { PriceItem } from 'ish-core/models/price-item/price-item.model';
import { whenTruthy } from 'ish-core/utils/operators';
 
@Component({
  selector: 'ish-mini-basket',
  templateUrl: './mini-basket.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MiniBasketComponent implements OnInit {
  basketAnimation$: Observable<string>;
  itemTotal$: Observable<PriceItem>;
  itemCount$: Observable<number>;
  basketLoading$: Observable<boolean>;
 
  isCollapsed = true;
 
  @Input() view: 'auto' | 'small' | 'full' = 'auto';
 
  private basketError$: Observable<HttpError>;
  private destroyRef = inject(DestroyRef);
 
  constructor(
    private checkoutFacade: CheckoutFacade,
    private appFacade: AppFacade,
    private location: Location,
    private cdRef: ChangeDetectorRef
  ) {}
  ngOnInit() {
    this.itemCount$ = this.checkoutFacade.basketItemCount$;
    this.itemTotal$ = this.checkoutFacade.basketItemTotal$;
    this.basketError$ = this.checkoutFacade.basketError$;
    this.basketLoading$ = this.checkoutFacade.basketLoading$;
 
    this.basketError$
      .pipe(
        whenTruthy(),
        filter(() => this.location.path() !== '/basket'),
        takeUntilDestroyed(this.destroyRef)
      )
      .subscribe(() => this.open());
 
    this.basketAnimation$ = this.checkoutFacade.basketChange$.pipe(
      filter(() => !this.location.path().startsWith('/basket')),
      switchMap(() => concat(of('mini-basket-animation'), timer(2500).pipe(map(() => ''))))
    );
 
    this.basketAnimation$.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(animation => {
      if (animation) {
        this.open();
      } else {
        this.collapse();
      }
    });
 
    this.appFacade.routingInProgress$.pipe(whenTruthy(), takeUntilDestroyed(this.destroyRef)).subscribe(() => {
      this.collapse();
    });
  }
 
  /**
   * Toggle the collapse state of the mini basket programmatically.
   */
  toggleCollapse() {
    this.isCollapsed = !this.isCollapsed;
  }
 
  /**
   * Collapse the mini basket programmatically.
   */
  collapse() {
    this.isCollapsed = true;
    this.cdRef.markForCheck();
  }
 
  /**
   * Open the mini basket programmatically.
   */
  // visible-for-testing
  open() {
    this.isCollapsed = false;
    this.cdRef.markForCheck();
  }
}