import { OnInit, Component, Input, OnDestroy, HostBinding } from '@angular/core';
import { SearchFactory, SearchService, HttpParams } from 'skf-search-angular-service';
import { PublicationService } from 'src/app/core/services/publication-service/publication.service';
import { UtilService } from 'src/app/core/services/util-service/util.service';
import { IdGeneratorService } from 'src/app/core/services/id-generator.service';
import { AppConfig } from '../../../../../app.config';
interface ProductInfoData {
title: string;
information: string;
}
@Component({
selector: 'div[product-spec-main]',
templateUrl: './product-spec-main.component.html',
styleUrls: ['./product-spec-main.component.scss']
})
export class ProductSpecMainComponent implements OnInit, OnDestroy {
@Input() entity;
data: ProductInfoData;
search: SearchService;
result;
tableId;
sortedColumn;
sortDir;
sortData;
searchSub;
noResults = false;
specId: string;
@HostBinding('class') get class() { return 'col-md-12 space-between'; }
constructor(
private searchFactory: SearchFactory,
private util: UtilService,
private pubService: PublicationService,
private idGenerator: IdGeneratorService
) {
this.specId = this.idGenerator.getId(); // ID for search service instance
this.search = this.searchFactory.get(`product-spec-${this.specId}`, `opc-categories-${AppConfig.settings.searchEnvironment.name}`);
}
ngOnInit() {
this.data = this.parseJson(this.entity);
this.searchSub = this.searchSubject();
// Initial search
this.tableId = this.util.extract(this.entity, 'ProductTableID');
const lang = this.pubService.getLanguage();
const pubId = this.pubService.getPublicationId();
const searchString = `id=${this.tableId}&language=${lang}&source=webpim&site=${pubId}`;
const httpParams = new HttpParams({fromString: searchString});
this.search.doSearchWithParameters({params: httpParams});
}
private searchSubject(): void {
this.search.result.subscribe(res => {
this.result = res;
this.sortData = {
systems: res['systems'],
sortOptions: res['documentList']['sortOptions'],
tableData: null
};
const docs = this.util.extract(res, 'documentList', 'documents');
this.noResults = (docs && docs.length === 0);
});
}
private parseJson(data: any): ProductInfoData {
const parsed: ProductInfoData = {
title: this.util.extract(data, 'Title'),
information: this.util.extract(data, 'Information')
};
return parsed;
}
ngOnDestroy(): void {
// Unsubscribe when component is destroyed
if (this.searchSub) { this.searchSub.unsubscribe(); }
}
}