import { Component , OnDestroy , OnInit , ViewChild } from '@angular/core' ; import { GroupService } from '../../services/group.service' ; import { ReplaySubject , Subject } from 'rxjs' ; import { Group } from '../../models/group' ; import { ActivatedRoute , Data , Router } from '@angular/router' ; import { GroupFormComponent } from '../components/group-form/group-form.component' ; import { Right } from '../../models/right' ; import { takeUntil } from 'rxjs/operators' ; import { ToastService } from '../../../layout/toast/toast.service' ; import { PermissionService } from '../../../auth/services/permission.service' ; @ Component ( { selector : 'cmdb-group-edit' , templateUrl : './group-edit.component.html' , styleUrls : [ './group-edit.component.scss' ] } ) export class GroupEditComponent implements OnInit , OnDestroy { @ ViewChild ( GroupFormComponent , { static : true } ) private groupForm : GroupFormComponent ; public rights : Array < Right > = [ ] ; public valid : boolean = false ; private subscriber : ReplaySubject < void > = new ReplaySubject < void > ( ) ; public group : Group ; private typeEditRightName = 'base.framework.type.edit' ; public typeEditRight = this . permissionService . hasRight ( this . typeEditRightName ) || this . permissionService . hasExtendedRight ( this . typeEditRightName ) ; constructor ( private route , private router , private groupService , private toastService , private permissionService ) { this . rights = this . route . snapshot . data . rights as Array < Right > ; this . group = this . route . snapshot . data . group as Group ; } public ngOnInit ( ) { this . group = this . route . snapshot . data . group as Group ; this . groupForm . nameControl . clearAsyncValidators ( ) ; this . groupForm . nameControl . disable ( ) ; } public ngOnDestroy ( ) { this . subscriber . next ( ) ; this . subscriber . complete ( ) ; } public edit ( group ) { const editGroup = Object . assign ( this . group , group ) ; if ( this . valid ) { this . groupService . putGroup ( this . group . public_id , editGroup ) . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( ( g ) => { this . toastService . success ( `Group ${ g . label } was updated!` ) ; this . router . navigate ( [ '/' , 'management' , 'groups' ] ) ; } ) ; } } }