import { Component , OnInit , OnDestroy , Input , forwardRef , EventEmitter , Output } from '@angular/core' ; import { FormControl , NG_VALUE_ACCESSOR } from '@angular/forms' ; import { filter , map , Observable , of as observableOf } from 'rxjs' ; import { tap } from 'rxjs/operators' ; import { NbComponentSize } from '@nebular/theme' ; import { UntilDestroy , untilDestroyed } from '@ngneat/until-destroy' ; import { IRole , IUser , RolesEnum } from '@gauzy/contracts' ; import { RoleService , Store } from './../../../../../@core/services' ; @ UntilDestroy ( { checkProperties : true } ) @ Component ( { selector : 'ngx-role-form-field' , templateUrl : './role.component.html' , styleUrls : [ ] , providers : [ { provide : NG_VALUE_ACCESSOR , useExisting : forwardRef ( ( ) => RoleFormFieldComponent ) , multi : true } ] } ) export class RoleFormFieldComponent implements OnInit , OnDestroy { roles : IRole [ ] = [ ] ; roles$ : Observable < IRole [ ] > = observableOf ( [ ] ) ; onChange : any = ( ) => { } ; onTouched : any = ( ) => { } ; /**
     * Getter & Setter for dynamic remove role from options
     */ private _excludes : RolesEnum [ ] = [ ] ; get excludes ( ) : RolesEnum [ ] { return this . _excludes ; } @ Input ( ) set excludes ( value ) { this . _excludes = value ; } private _id : string ; get id ( ) : string { return this . _id ; } @ Input ( ) set id ( value ) { this . _id = value ; } private _size : NbComponentSize = 'medium' ; get size ( ) : NbComponentSize { return this . _size ; } @ Input ( ) set size ( value ) { this . _size = value ; } private _placeholder : string ; get placeholder ( ) : string { return this . _placeholder ; } @ Input ( ) set placeholder ( value ) { this . _placeholder = value ; } private _label : string ; get label ( ) : string { return this . _label ; } @ Input ( ) set label ( value ) { this . _label = value ; } private _ctrl : FormControl = new FormControl ( ) ; get ctrl ( ) : FormControl { return this . _ctrl ; } @ Input ( ) set ctrl ( value ) { this . _ctrl = value ; } private _role : IRole ; set role ( value ) { this . _role = value ; this . onChange ( value ) ; this . onTouched ( value ) ; } get role ( ) : IRole { return this . _role ; } /**
     * Getter & Setter for internal [(NgModel)]
     */ private _roleId : string ; get roleId ( ) : string { return this . _roleId ; } set roleId ( value ) { this . _roleId = value ; } @ Output ( ) selectedChange = new EventEmitter < IRole > ( ) ; constructor ( private readonly store , private readonly rolesService ) { } ngOnInit ( ) { this . store . user$ . pipe ( filter ( ( user ) => ! ! user ) , tap ( ( ) => this . renderRoles ( ) ) , untilDestroyed ( this ) ) . subscribe ( ) ; } /**
    * GET all tenant roles
    * Excludes role if needed
    */ async renderRoles ( ) { this . roles$ = observableOf ( ( await ( this . rolesService . getAll ( ) ) ) . items ) . pipe ( map ( ( roles ) => roles . filter ( ( role ) => ! this . excludes . includes ( role . name as RolesEnum ) ) ) , tap ( ( roles ) => this . roles = roles ) ) ; } param value writeValue ( value ) { if ( value ) { this . roleId = value . id ; } } registerOnChange ( fn ) { this . onChange = fn ; } registerOnTouched ( fn ) { this . onTouched = fn ; } param role onSelectionChange ( roleId ) { if ( roleId ) { this . role = this . getRoleById ( roleId ) ; if ( this . role ) { this . selectedChange . emit ( this . role ) ; } } } param value returns getRoleById ( value ) { return this . roles . find ( ( role ) => value === role . id ) ; } ngOnDestroy ( ) { } }