import { Component , OnInit } from '@angular/core' ; import { FormControl , FormGroup , Validators } from '@angular/forms' ; import { StatusService } from '../../services/status.service' ; import { CmdbStatus } from '../../models/cmdb-status' ; import { ActivatedRoute , Router } from '@angular/router' ; @ Component ( { selector : 'cmdb-status-edit' , templateUrl : './status-edit.component.html' , styleUrls : [ './status-edit.component.scss' ] } ) export class StatusEditComponent implements OnInit { public statusID : number ; public shortMaxLength : number = 5 ; public editStatusForm : FormGroup ; public editStatus : CmdbStatus ; constructor ( private statusService , private route , private router ) { this . route . params . subscribe ( ( params ) => { this . statusID = params . publicID ; } ) ; this . editStatusForm = new FormGroup ( { name : new FormControl ( '' , Validators . required ) , label : new FormControl ( '' , Validators . required ) , short : new FormControl ( '' , Validators . maxLength ( this . shortMaxLength ) ) } ) ; } public ngOnInit ( ) { this . statusService . getStatus ( this . statusID ) . subscribe ( ( respStatus ) => { this . editStatus = respStatus ; this . editStatusForm . reset ( ) ; this . editStatusForm . patchValue ( this . editStatus ) ; this . editStatusForm . markAllAsTouched ( ) ; } ) ; } public get name ( ) { return this . editStatusForm . get ( 'name' ) ; } public get label ( ) { return this . editStatusForm . get ( 'label' ) ; } public get controls ( ) { return this . editStatusForm . controls ; } public saveStatus ( ) { this . editStatusForm . markAllAsTouched ( ) ; if ( this . editStatusForm . valid ) { Object . assign ( this . editStatus , this . editStatusForm . getRawValue ( ) ) ; this . statusService . putStatus ( this . editStatus ) . subscribe ( ( ) => { this . router . navigate ( [ '/framework/status/' ] ) ; } , error => { console . error ( error ) ; } ) ; } } }