File

libs/core/src/lib/pipes/conditional-label.pipe.ts

Metadata

Methods

transform
transform(labels: PageActionButtonLabel[] | string | undefined)
Parameters :
Name Type Optional
labels PageActionButtonLabel[] | string | undefined No
import { inject, Pipe, PipeTransform } from '@angular/core';
import { combineLatest, Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { type PageActionButtonLabel } from '../services/taly-page-data.model';
import { TalyStateService } from '../services/taly-state.service';

@Pipe({
  name: 'conditionalLabel',
  standalone: true
})
export class ConditionalLabelPipe implements PipeTransform {
  private talyStateService = inject(TalyStateService, { optional: true });

  transform(labels: PageActionButtonLabel[] | string | undefined): Observable<string | undefined> {
    if (!Array.isArray(labels)) {
      return of(labels || undefined);
    }

    if (!this.talyStateService) {
      return of(undefined);
    }

    const visibleLabels$ = labels.map(({ condition, label }) =>
      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
      this.talyStateService!.evaluateCondition$(condition).pipe(
        map((visible) => (visible ? label : null))
      )
    );

    return combineLatest(visibleLabels$).pipe(
      map((labels) => labels.find((label) => label !== null) || undefined)
    );
  }
}

results matching ""

    No results matching ""