import { Component, ChangeDetectionStrategy, ChangeDetectorRef, OnInit, OnDestroy } from '@angular/core'; import { DetailedItem } from '../detailed-item'; import { DetailedInfoService } from './detailed-info.service'; import { TranslateService } from '@ngx-translate/core'; import { TypeToHumanPipe } from '../type-to-human.pipe'; import { Subject } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; @Component({ selector: 'app-detailed-info', templateUrl: 'detailed-info.component.html', styleUrls: ['detailed-info.component.scss'], changeDetection: ChangeDetectionStrategy.OnPush }) export class DetailedInfoComponent implements OnInit, OnDestroy { destroy$ = new Subject(); item: DetailedItem; constructor (private detailedInfoService: DetailedInfoService, private translateService: TranslateService, private changeDetectorRef: ChangeDetectorRef) {} getDescription (item: DetailedItem) { let description: string; const lastCrumb = item.path[item.path.length - 1]; const fbaTypeToHumanPipe = new TypeToHumanPipe(this.translateService); switch (item.type) { case 'interests': description = this.translateService.instant('app-detailed-info.DESCRIBE_INTERESTS') + ` ${lastCrumb}`; break; default: description = this.translateService.instant('app-detailed-info.DESCRIBE_DEFAULT', { type: fbaTypeToHumanPipe.transform(item.type), name: lastCrumb }); } return description; } ngOnDestroy () { this.destroy$.next(); } ngOnInit () { this.detailedInfoService.item .pipe( takeUntil(this.destroy$) ) .subscribe((item: DetailedItem) => { this.item = item; this.changeDetectorRef.markForCheck(); }); } }