11import { AfterViewInit , Component , OnDestroy , OnInit , ViewChild } from '@angular/core' ;
22import { ActionsSubject , select , Store } from '@ngrx/store' ;
33import { DataTableDirective } from 'angular-datatables' ;
4- import { Observable , Subject , Subscription } from 'rxjs' ;
5- import { delay , filter } from 'rxjs/operators' ;
4+ import { Observable , Subject , Subscription } from 'rxjs' ;
5+ import { delay , filter , map } from 'rxjs/operators' ;
6+ import { FeatureManagerService } from 'src/app/modules/shared/feature-toggles/feature-toggle-manager.service' ;
67import { User } from '../../models/users' ;
7- import { GrantRoleUser , LoadUsers , RevokeRoleUser , UserActionTypes } from '../../store/user.actions' ;
8+ import { GrantRoleUser , LoadUsers , RevokeRoleUser , UserActionTypes , AddUserToGroup , RemoveUserToGroup } from '../../store/user.actions' ;
89import { getIsLoading } from '../../store/user.selectors' ;
910
1011@Component ( {
@@ -21,8 +22,16 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
2122 @ViewChild ( DataTableDirective , { static : false } )
2223 dtElement : DataTableDirective ;
2324 dtOptions : any = { } ;
25+ switchGroupsSubscription : Subscription ;
26+ isEnableToggleSubscription : Subscription ;
27+ isUserRoleToggleOn ;
28+ flakyToggle : true ;
2429
25- constructor ( private store : Store < User > , private actionsSubject$ : ActionsSubject ) {
30+ constructor (
31+ private store : Store < User > ,
32+ private actionsSubject$ : ActionsSubject ,
33+ private featureManagerService : FeatureManagerService
34+ ) {
2635 this . isLoading$ = store . pipe ( delay ( 0 ) , select ( getIsLoading ) ) ;
2736 }
2837
@@ -35,6 +44,24 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
3544 this . rerenderDataTable ( ) ;
3645 } ) ;
3746
47+ this . isEnableToggleSubscription = this . isFeatureToggleActivated ( ) . subscribe ( ( flag ) => {
48+ this . isUserRoleToggleOn = flag ;
49+ console . log ( 'in subscription' , this . isUserRoleToggleOn ) ;
50+ } ) ;
51+
52+ this . switchGroupsSubscription = this . actionsSubject$
53+ . pipe (
54+ filter (
55+ ( action : any ) =>
56+ action . type === UserActionTypes . ADD_USER_TO_GROUP_SUCCESS ||
57+ action . type === UserActionTypes . REMOVE_USER_TO_GROUP_SUCCESS
58+ )
59+ )
60+ . subscribe ( ( action ) => {
61+ this . store . dispatch ( new LoadUsers ( ) ) ;
62+ this . rerenderDataTable ( ) ;
63+ } ) ;
64+
3865 this . switchRoleSubscription = this . actionsSubject$
3966 . pipe (
4067 filter (
@@ -55,6 +82,7 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
5582 ngOnDestroy ( ) {
5683 this . loadUsersSubscription . unsubscribe ( ) ;
5784 this . dtTrigger . unsubscribe ( ) ;
85+ this . isEnableToggleSubscription . unsubscribe ( ) ;
5886 }
5987
6088 private rerenderDataTable ( ) : void {
@@ -73,4 +101,18 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
73101 ? this . store . dispatch ( new RevokeRoleUser ( userId , roleId ) )
74102 : this . store . dispatch ( new GrantRoleUser ( userId , roleId ) ) ;
75103 }
104+
105+ isFeatureToggleActivated ( ) {
106+ return this . featureManagerService . isToggleEnabledForUser ( 'ui-list-technologies' ) . pipe (
107+ map ( ( enabled ) => {
108+ return enabled === true ? true : false ;
109+ } )
110+ ) ;
111+ }
112+
113+ switchGroups ( userId : string , userGroups : string [ ] , groupname : string , groupValue : string ) {
114+ userGroups . includes ( groupValue )
115+ ? this . store . dispatch ( new RemoveUserToGroup ( userId , groupname ) )
116+ : this . store . dispatch ( new AddUserToGroup ( userId , groupname ) ) ;
117+ }
76118}
0 commit comments