import { Component , OnDestroy , OnInit , ViewChild } from '@angular/core' ; import { ActivatedRoute } from '@angular/router' ; import { User } from '../../models/user' ; import { UserService } from '../../services/user.service' ; import { GroupService } from '../../services/group.service' ; import { Group } from '../../models/group' ; import { ReplaySubject , Subject } from 'rxjs' ; import { takeUntil } from 'rxjs/operators' ; import { FormGroup } from '@angular/forms' ; import { UserFormComponent } from '../components/user-form/user-form.component' ; import { ToastService } from '../../../layout/toast/toast.service' ; import { NgbModal , NgbModalRef } from '@ng-bootstrap/ng-bootstrap' ; import { UsersPasswdModalComponent } from '../modals/users-passwd-modal/users-passwd-modal.component' ; @ Component ( { selector : 'cmdb-user-view' , templateUrl : './user-view.component.html' , styleUrls : [ './user-view.component.scss' ] } ) export class UserViewComponent implements OnInit , OnDestroy { @ ViewChild ( UserFormComponent , { static : true } ) userFormComponent : UserFormComponent ; private modalRef : NgbModalRef ; private subscriber : ReplaySubject < void > = new ReplaySubject < void > ( ) ; public userID : number ; public user : User ; public group : Group ; constructor ( private route , public userService , public groupService , private toastService , private modalService ) { this . user = this . route . snapshot . data . user as User ; this . group = this . route . snapshot . data . group as Group ; } public save ( user ) { const editUser = Object . assign ( this . user , user ) ; this . userService . putUser ( this . user . public_id , editUser ) . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( ( apiUser ) => { this . toastService . success ( 'Your profile was updated!' ) ; } ) ; } public ngOnInit ( ) { this . userFormComponent . usernameControl . disable ( ) ; this . userFormComponent . form . removeControl ( 'password' ) ; this . userFormComponent . form . removeControl ( 'authenticator' ) ; this . userFormComponent . form . removeControl ( 'group_id' ) ; } public ngOnDestroy ( ) { if ( this . modalRef ) { this . modalRef . close ( ) ; } this . subscriber . next ( ) ; this . subscriber . complete ( ) ; } public openPasswordModal ( ) { this . modalRef = this . modalService . open ( UsersPasswdModalComponent , { size : 'lg' } ) ; this . modalRef . componentInstance . user = this . user ; this . modalRef . result . then ( result => { if ( typeof result === typeof this . user ) { this . user = result ; } } ) ; } }