import { Component , OnInit } from '@angular/core' ; import { FormControl , FormGroup , Validators } from '@angular/forms' ; import { ImportService } from '../import.service' ; @ Component ( { selector : 'cmdb-import-types' , templateUrl : './import-types.component.html' , styleUrls : [ './import-types.component.scss' ] } ) export class ImportTypesComponent implements OnInit { public constructor ( private importService ) { } public fileForm : FormGroup ; public preview : any ; public done : boolean = false ; public errorHandling = [ ] ; ngOnInit ( ) { this . fileForm = new FormGroup ( { format : new FormControl ( 'json' , Validators . required ) , name : new FormControl ( '' , Validators . required ) , size : new FormControl ( '' , Validators . required ) , file : new FormControl ( null , Validators . required ) , action : new FormControl ( 'create' , Validators . required ) , } ) ; this . fileForm . valueChanges . subscribe ( newValue => { this . fileForm . get ( 'file' ) . patchValue ( newValue . file , { onlySelf : true } ) ; } ) ; } public importTypeFile ( ) { const action = this . fileForm . get ( 'action' ) . value ; const theJSON = JSON . stringify ( this . fileForm . get ( 'file' ) . value ) ; const formData = new FormData ( ) ; formData . append ( 'uploadFile' , theJSON ) ; if ( action === 'update' ) { this . importService . postUpdateTypeParser ( formData ) . subscribe ( res => { if ( Object . keys ( res ) . length > 0 ) { this . errorHandling . push ( res ) ; } this . done = true ; } ) ; } else { this . importService . postCreateTypeParser ( formData ) . subscribe ( res => { if ( Object . keys ( res ) . length > 0 ) { this . errorHandling . push ( res ) ; } this . done = true ; } ) ; } } }