import { Component , Input , OnDestroy } from '@angular/core' ; import { BaseSectionComponent } from '../base-section/base-section.component' ; import { TypeService } from '../../../services/type.service' ; import { CmdbMode } from '../../../modes.enum' ; import { CmdbType , CmdbTypeSection } from '../../../models/cmdb-type' ; import { ReplaySubject } from 'rxjs' ; import { takeUntil } from 'rxjs/operators' ; import { ToastService } from '../../../../layout/toast/toast.service' ; @ Component ( { selector : 'cmdb-reference-section' , templateUrl : './reference-section.component.html' , styleUrls : [ './reference-section.component.scss' ] } ) export class ReferenceSectionComponent extends BaseSectionComponent implements OnDestroy { private subscriber : ReplaySubject < void > = new ReplaySubject < void > ( ) ; public referencedType : CmdbType ; @ Input ( 'section' ) public set Section ( section ) { this . section = section ; if ( this . section ?. reference ?. type_id ) { this . loadRefType ( ) ; } } constructor ( private typeService , private toast ) { super ( ) ; } public loadRefType ( ) { if ( this . mode === CmdbMode . View ) { this . typeService . getType ( this . section . reference . type_id ) . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( ( apiResponse ) => { this . referencedType = apiResponse ; } , error => { this . toast . error ( error . error . message , { headerName : 'Error when setting the reference' } ) ; } ) ; } } public get referenceField ( ) : any { return this . fields . find ( f => f . name === `${ this . section . name }-field` ) ; } public getFieldByName ( name ) { const field = this . fields . find ( s => s . name === `${ this . section . name }-field` ) . references . fields . find ( f => f . name === name ) ; switch ( field . type ) { case 'ref' : { field . default = parseInt ( field . default , 10 ) ; break ; } default : { field . default = field . value ; break ; } } return field ; } public getValueByName ( name ) { const fieldFound = this . fields . find ( s => s . name === `${ this . section . name }-field` ) . references . fields . find ( field => field . name === name ) ; if ( fieldFound === undefined ) { return { } ; } return fieldFound . value ; } public ngOnDestroy ( ) { this . subscriber . next ( ) ; this . subscriber . complete ( ) ; } }