import { Component , OnInit } from '@angular/core' ; import { FormBuilder , FormGroup , Validators } from '@angular/forms' ; import { ActivatedRoute , Router } from '@angular/router' ; import { JhiLanguageHelper , User , UserService } from 'app/core' ; @ Component ( { selector : 'jhi-user-mgmt-update' , templateUrl : './user-management-update.component.html' } ) export class UserMgmtUpdateComponent implements OnInit { user : User ; languages : any [ ] ; authorities : any [ ] ; isSaving : boolean ; editForm = this . fb . group ( { id : [ null ] , login : [ '' , [ Validators . required , Validators . minLength ( 1 ) , Validators . maxLength ( 50 ) , Validators . pattern ( '^[_.@A-Za-z0-9-]*' ) ] ] , firstName : [ '' , [ Validators . maxLength ( 50 ) ] ] , lastName : [ '' , [ Validators . maxLength ( 50 ) ] ] , email : [ '' , [ Validators . minLength ( 5 ) , Validators . maxLength ( 254 ) , Validators . email ] ] , activated : [ true ] , langKey : [ ] , authorities : [ ] } ) ; constructor ( private languageHelper , private userService , private route , private router , private fb ) { } ngOnInit ( ) { this . isSaving = false ; this . route . data . subscribe ( ( { user } ) => { this . user = user . body ? user . body : user ; this . updateForm ( this . user ) ; } ) ; this . authorities = [ ] ; this . userService . authorities ( ) . subscribe ( authorities => { this . authorities = authorities ; } ) ; this . languageHelper . getAll ( ) . then ( languages => { this . languages = languages ; } ) ; } private updateForm ( user ) { this . editForm . patchValue ( { id : user . id , login : user . login , firstName : user . firstName , lastName : user . lastName , email : user . email , activated : user . activated , langKey : user . langKey , authorities : user . authorities } ) ; } previousState ( ) { window . history . back ( ) ; } save ( ) { this . isSaving = true ; this . updateUser ( this . user ) ; if ( this . user . id !== null ) { this . userService . update ( this . user ) . subscribe ( response => this . onSaveSuccess ( response ) , ( ) => this . onSaveError ( ) ) ; } else { this . userService . create ( this . user ) . subscribe ( response => this . onSaveSuccess ( response ) , ( ) => this . onSaveError ( ) ) ; } } private updateUser ( user ) { user . login = this . editForm . get ( [ 'login' ] ) . value ; user . firstName = this . editForm . get ( [ 'firstName' ] ) . value ; user . lastName = this . editForm . get ( [ 'lastName' ] ) . value ; user . email = this . editForm . get ( [ 'email' ] ) . value ; user . activated = this . editForm . get ( [ 'activated' ] ) . value ; user . langKey = this . editForm . get ( [ 'langKey' ] ) . value ; user . authorities = this . editForm . get ( [ 'authorities' ] ) . value ; } private onSaveSuccess ( result ) { this . isSaving = false ; this . previousState ( ) ; } private onSaveError ( ) { this . isSaving = false ; } }