import { AfterViewInit , Component , OnDestroy , OnInit , ViewChild } from '@angular/core' ; import { UserFormComponent } from '../components/user-form/user-form.component' ; import { userExistsValidator , UserService } from '../../services/user.service' ; import { GroupService } from '../../services/group.service' ; import { ReplaySubject } from 'rxjs' ; import { takeUntil } from 'rxjs/operators' ; import { Group } from '../../models/group' ; import { APIGetMultiResponse , APIInsertSingleResponse } from '../../../services/models/api-response' ; import { AuthService } from '../../../auth/services/auth.service' ; import { User } from '../../models/user' ; import { ToastService } from '../../../layout/toast/toast.service' ; import { ActivatedRoute , Router } from '@angular/router' ; @ Component ( { selector : 'cmdb-user-add' , templateUrl : './user-add.component.html' , styleUrls : [ './user-add.component.scss' ] } ) export class UserAddComponent implements AfterViewInit , OnDestroy { /**
     * UserFormComponent for static inserting of parameters.
     */ @ ViewChild ( UserFormComponent , { static : true } ) userFormComponent : UserFormComponent ; /**
     * Component un-subscriber.
     */ private subscriber : ReplaySubject < void > = new ReplaySubject < void > ( ) ; /**
     * List of possible groups.
     */ public groups : Array < Group > = [ ] ; /**
     * List of possible auth providers.
     */ public providers : Array < any > = [ ] ; constructor ( private route , private router , private userService , private toastService ) { this . groups = this . route . snapshot . data . groups as Array < Group > ; this . providers = this . route . snapshot . data . providers as Array < any > ; } public ngAfterViewInit ( ) { this . userFormComponent . usernameControl . setAsyncValidators ( userExistsValidator ( this . userService ) ) ; } public ngOnDestroy ( ) { this . subscriber . next ( ) ; this . subscriber . complete ( ) ; } public save ( user ) { this . userService . postUser ( user ) . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( ( apiUser ) => { this . toastService . success ( `User ${ apiUser . user_name } was added` ) ; this . router . navigate ( [ '/' , 'management' , 'users' ] ) ; } ) ; } }