import { Component , HostListener , OnInit } from '@angular/core' ; import { ApiCallService } from '../../../services/api-call.service' ; import { ObjectService } from '../../services/object.service' ; import { ActivatedRoute , Router } from '@angular/router' ; import { CmdbMode } from '../../modes.enum' ; import { CmdbObject } from '../../models/cmdb-object' ; import { FormControl , FormGroup } from '@angular/forms' ; import { ToastService } from '../../../layout/toast/toast.service' ; import { RenderResult } from '../../models/cmdb-render' ; import { TypeService } from '../../services/type.service' ; import { CmdbType } from '../../models/cmdb-type' ; import { APIUpdateMultiResponse } from '../../../services/models/api-response' ; import { text } from "@fortawesome/fontawesome-svg-core" ; @ Component ( { selector : 'cmdb-object-edit' , templateUrl : './object-edit.component.html' , styleUrls : [ './object-edit.component.scss' ] } ) export class ObjectEditComponent implements OnInit { public mode : CmdbMode = CmdbMode . Edit ; public objectInstance : CmdbObject ; public typeInstance : CmdbType ; public renderResult : RenderResult ; public renderForm : FormGroup ; public commitForm : FormGroup ; private objectID : number ; constructor ( private api , private objectService , private typeService , private route , private router , private toastService ) { this . route . params . subscribe ( ( params ) => { this . objectID = params . publicID ; } ) ; this . renderForm = new FormGroup ( { } ) ; this . commitForm = new FormGroup ( { comment : new FormControl ( '' ) } ) ; } public ngOnInit ( ) { this . objectService . getObject ( this . objectID ) . subscribe ( ( rr ) => { this . renderResult = rr ; } , error => { console . error ( error ) ; } , ( ) => { this . objectService . getObject < CmdbObject > ( this . objectID , true ) . subscribe ( ob => { this . objectInstance = ob ; } ) ; this . typeService . getType ( this . renderResult . type_information . type_id ) . subscribe ( ( value ) => { this . typeInstance = value ; } ) ; } ) ; } @ HostListener ( 'window:scroll' , [ '$event' ] ) onWindowScroll ( $event ) { const dialog = document . getElementById ( 'object-form-action' ) ; if ( document . body . scrollTop > 20 || document . documentElement . scrollTop > 20 ) { dialog . style . visibility = 'visible' ; } else { dialog . style . visibility = 'hidden' ; } } public editObject ( ) { this . renderForm . markAllAsTouched ( ) ; if ( this . renderForm . valid ) { const patchValue = [ ] ; Object . keys ( this . renderForm . value ) . forEach ( ( key ) => { let val = this . renderForm . value [ key ] ; if ( val === undefined || val == null ) { val = '' ; } patchValue . push ( { name : key , value : val } ) ; } ) ; this . objectInstance . fields = patchValue ; this . objectInstance . comment = this . commitForm . get ( 'comment' ) . value ; this . objectService . putObject ( this . objectID , this . objectInstance ) . subscribe ( ( res ) => { if ( res . failed . length === 0 ) { this . toastService . success ( 'Object was successfully updated!' ) ; this . router . navigate ( [ '/framework/object/view/' + this . objectID ] ) ; } else { for ( const err of res . failed ) { this . toastService . error ( err . error_message ) ; } this . router . navigate ( [ '/framework/object/type/' + this . objectInstance . type_id ] ) ; } } , error => { this . toastService . error ( error ) ; this . router . navigate ( [ '/framework/object/type/' + this . objectInstance . type_id ] ) ; } ) ; } } }