import { Component , Input , ViewChild } from '@angular/core' ; import { Router } from '@angular/router' ; import { ToastService } from '../../layout/toast/toast.service' ; import { CmdbMode } from '../../framework/modes.enum' ; import { DocapiService } from '../docapi.service' ; import { DocapiBuilderSettingsStepComponent } from './docapi-builder-settings-step/docapi-builder-settings-step.component' ; import { DocapiBuilderTypeStepComponent } from './docapi-builder-type-step/docapi-builder-type-step.component' ; import { DocapiBuilderStyleStepComponent } from './docapi-builder-style-step/docapi-builder-style-step.component' ; import { DocapiBuilderContentStepComponent } from './docapi-builder-content-step/docapi-builder-content-step.component' ; import { DocTemplate } from '../models/cmdb-doctemplate' ; @ Component ( { selector : 'cmdb-docapi-settings-builder' , templateUrl : './docapi-builder.component.html' , styleUrls : [ './docapi-builder.component.scss' ] } ) export class DocapiBuilderComponent { @ Input ( ) public mode : number = CmdbMode . Create ; @ Input ( ) public docInstance ? : DocTemplate ; @ ViewChild ( DocapiBuilderSettingsStepComponent , { static : true } ) public settingsStep : DocapiBuilderSettingsStepComponent ; @ ViewChild ( DocapiBuilderTypeStepComponent , { static : true } ) public typeStep : DocapiBuilderTypeStepComponent ; public typeStepFormValid : boolean = false ; public typeParam : any = undefined ; @ ViewChild ( DocapiBuilderContentStepComponent , { static : true } ) public contentStep : DocapiBuilderContentStepComponent ; @ ViewChild ( DocapiBuilderStyleStepComponent , { static : true } ) public styleStep : DocapiBuilderStyleStepComponent ; constructor ( private docapiService , private router , private toast ) { } public saveDoc ( ) { if ( this . mode === CmdbMode . Create ) { this . docInstance = new DocTemplate ( ) ; } this . docInstance . name = this . settingsStep . settingsForm . get ( 'name' ) . value ; this . docInstance . label = this . settingsStep . settingsForm . get ( 'label' ) . value ; this . docInstance . active = this . settingsStep . settingsForm . get ( 'active' ) . value ; this . docInstance . description = this . settingsStep . settingsForm . get ( 'description' ) . value ; this . docInstance . template_type = this . typeStep . typeForm . get ( 'template_type' ) . value ; this . docInstance . template_parameters = this . typeStep . typeParamComponent . typeParamForm . value ; this . docInstance . template_data = this . contentStep . contentForm . get ( 'template_data' ) . value ; this . docInstance . template_style = this . styleStep . styleForm . get ( 'template_style' ) . value ; if ( this . mode === CmdbMode . Create ) { let newId = null ; this . docapiService . postDocTemplate ( this . docInstance ) . subscribe ( publicIdResp => { newId = publicIdResp ; this . router . navigate ( [ '/docapi/' ] , { queryParams : { docAddSuccess : newId } } ) ; } , ( error ) => { console . error ( error ) ; } ) ; } else if ( this . mode === CmdbMode . Edit ) { this . docapiService . putDocTemplate ( this . docInstance ) . subscribe ( ( updateResp ) => { this . toast . success ( `DocAPI document successfully edited: ${ updateResp . public_id }` ) ; this . router . navigate ( [ '/docapi/' ] , { queryParams : { docEditSuccess : updateResp . public_id } } ) ; } , ( error ) => { console . error ( error ) ; } ) ; } } }