Search code examples
javascriptangulartypescriptngrx

Combine 3 async to one observable


I have 3 observables that I use for filter html part

Here is typescript part

   fiscalYear$ = this.store$.select(this.tableStoreService.getFiscalYear);
      isLoading$ = this.store$.select(this.tableStoreService.tableSelectors.getLoading);
isReportLoading$ = this.store$.select(isReportLoading);

All of this values has Observable<boolean> in return type.

Then I use it like this to filter html in [disabled]

<button class="btn btn-primary font-weight-700 font-size-200" [disabled]="(isReportLoading$ | async) || (fiscalYear$ | async) < 2022 || (isLoading$ | async) " (click)="clickExport()">
          {{ 'homepage.export' | translate }}
          <mat-icon class="btn-icon export-icon" svgIcon="export"></mat-icon>
        </button>

I try to do it like this

get isExportAvialable$() {
    return combineLatest(this.fiscalYear$, this.isLoading$, this.isReportLoading$, (fiscalYear, isLoading, isReportLoading) => isReportLoading || fiscalYear < 2022 || isLoading);
  }

But fiscalYear < 2022 is showing error

How I can combine it to use only one with async?


Solution

  • You can solve it like this:

    fiscalYear$ = this.store$.select(this.tableStoreService.getFiscalYear);
    isLoading$ = this.store$.select(this.tableStoreService.tableSelectors.getLoading);
    isReportLoading$ = this.store$.select(isReportLoading);
    isExportAvailable$ = combineLatest([
        this.fiscalYear$, 
        this.isLoading$, 
        this.isReportLoading$
    ]).pipe(map(([year, isLoading, isReportLoading]) => return year < 2022 && isLoading && isReportLoading));
    

    and then just use isExportAvailable$ with the async pipe in your template.