All files / src/app/extensions/quoting/shared/quote-widget quote-widget.component.ts

100% Statements 23/23
75% Branches 6/8
100% Functions 10/10
100% Lines 18/18

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 551x 1x 1x 1x   1x   1x               1x           3x   3x     3x   3x       10x       3x   5x       5x           3x 3x 3x        
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, DestroyRef, OnInit, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { Observable, combineLatest, iif, of } from 'rxjs';
import { distinctUntilChanged, map, switchMap } from 'rxjs/operators';
 
import { GenerateLazyComponent } from 'ish-core/utils/module-loader/generate-lazy-component.decorator';
 
import { QuotingFacade } from '../../facades/quoting.facade';
 
@Component({
  selector: 'ish-quote-widget',
  templateUrl: './quote-widget.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
@GenerateLazyComponent()
export class QuoteWidgetComponent implements OnInit {
  loading$: Observable<boolean>;
 
  respondedQuotes: number;
  submittedQuoteRequests: number;
 
  private destroyRef = inject(DestroyRef);
 
  constructor(private quotingFacade: QuotingFacade, private cd: ChangeDetectorRef) {}
 
  ngOnInit() {
    this.loading$ = this.quotingFacade.loading$;
 
    const quotingStates$ = this.quotingFacade
      .quotingEntities$()
      .pipe(
        switchMap(quotes =>
          iif(() => !quotes?.length, of([]), combineLatest(quotes.map(quote => this.quotingFacade.state$(quote.id))))
        )
      );
 
    combineLatest([
      quotingStates$.pipe(
        map(states => states.filter(state => state === 'Responded').length),
        distinctUntilChanged()
      ),
      quotingStates$.pipe(
        map(states => states.filter(state => state === 'Submitted').length),
        distinctUntilChanged()
      ),
    ])
      .pipe(takeUntilDestroyed(this.destroyRef))
      .subscribe(([responded, submitted]) => {
        this.respondedQuotes = responded;
        this.submittedQuoteRequests = submitted;
        this.cd.detectChanges();
      });
  }
}