libs/core/src/lib/pipes/conditional-label.pipe.ts
Name | conditionalLabel |
transform | ||||||
transform(labels: PageActionButtonLabel[] | string | undefined)
|
||||||
Parameters :
Returns :
Observable<string | undefined>
|
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)
);
}
}