import { Component , OnInit } from '@angular/core' ; import { HttpResponse , HttpErrorResponse } from '@angular/common/http' ; import { FormBuilder , Validators } from '@angular/forms' ; import { ActivatedRoute } from '@angular/router' ; import { Observable } from 'rxjs' ; import { IEvent , Event } from 'app/shared/model/event.model' ; import { EventService } from './event.service' ; @ Component ( { selector : 'jhi-event-update' , templateUrl : './event-update.component.html' } ) export class EventUpdateComponent implements OnInit { event : IEvent ; isSaving : boolean ; editForm = this . fb . group ( { id : [ ] , name : [ null , [ Validators . required , Validators . minLength ( 3 ) , Validators . maxLength ( 100 ) ] ] , description : [ ] } ) ; constructor ( protected eventService , protected activatedRoute , private fb ) { } ngOnInit ( ) { this . isSaving = false ; this . activatedRoute . data . subscribe ( ( { event } ) => { this . updateForm ( event ) ; this . event = event ; } ) ; } updateForm ( event ) { this . editForm . patchValue ( { id : event . id , name : event . name , description : event . description } ) ; } previousState ( ) { window . history . back ( ) ; } save ( ) { this . isSaving = true ; const event = this . createFromForm ( ) ; if ( event . id !== undefined ) { this . subscribeToSaveResponse ( this . eventService . update ( event ) ) ; } else { this . subscribeToSaveResponse ( this . eventService . create ( event ) ) ; } } private createFromForm ( ) { const entity = { ... new Event ( ) , id : this . editForm . get ( [ 'id' ] ) . value , name : this . editForm . get ( [ 'name' ] ) . value , description : this . editForm . get ( [ 'description' ] ) . value } ; return entity ; } protected subscribeToSaveResponse ( result ) { result . subscribe ( ( res ) => this . onSaveSuccess ( ) , ( res ) => this . onSaveError ( ) ) ; } protected onSaveSuccess ( ) { this . isSaving = false ; this . previousState ( ) ; } protected onSaveError ( ) { this . isSaving = false ; } }