@@ -2,10 +2,12 @@ import { AfterViewInit, Component, OnDestroy, OnInit, ViewChild } from '@angular
22import { ActionsSubject , select , Store } from '@ngrx/store' ;
33import { DataTableDirective } from 'angular-datatables' ;
44import { Observable , Subject , Subscription } from 'rxjs' ;
5- import { delay , filter } from 'rxjs/operators' ;
5+ import { delay , filter , map } from 'rxjs/operators' ;
66import { User } from '../../models/users' ;
7- import { LoadUsers , UserActionTypes } from '../../store/user.actions' ;
7+ import { GrantRoleUser , LoadUsers , RevokeRoleUser , UserActionTypes } from '../../store/user.actions' ;
88import { getIsLoading } from '../../store/user.selectors' ;
9+ import { FeatureManagerService } from 'src/app/modules/shared/feature-toggles/feature-toggle-manager.service' ;
10+
911@Component ( {
1012 selector : 'app-users-list' ,
1113 templateUrl : './users-list.component.html' ,
@@ -18,19 +20,42 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
1820 dtTrigger : Subject < any > = new Subject ( ) ;
1921 @ViewChild ( DataTableDirective , { static : false } )
2022 dtElement : DataTableDirective ;
23+ dtOptions : any = { } ;
24+ isFlagOn ;
2125
22- constructor ( private store : Store < User > , private actionsSubject$ : ActionsSubject ) {
26+ constructor (
27+ private store : Store < User > ,
28+ private actionsSubject$ : ActionsSubject ,
29+ private featureManagerService : FeatureManagerService
30+ ) {
2331 this . isLoading$ = store . pipe ( delay ( 0 ) , select ( getIsLoading ) ) ;
32+ this . isFeatureToggleAactivated ( ) . subscribe ( ( flag ) => {
33+ this . isFlagOn = flag ;
34+ console . log ( this . isFlagOn ) ;
35+ } ) ;
2436 }
2537
2638 ngOnInit ( ) : void {
39+ this . store . dispatch ( new LoadUsers ( ) ) ;
2740 this . loadUsersSubscription = this . actionsSubject$
2841 . pipe ( filter ( ( action : any ) => action . type === UserActionTypes . LOAD_USERS_SUCCESS ) )
2942 . subscribe ( ( action ) => {
3043 this . users = action . payload ;
3144 this . rerenderDataTable ( ) ;
3245 } ) ;
33- this . store . dispatch ( new LoadUsers ( ) ) ;
46+
47+ this . loadUsersSubscription = this . actionsSubject$
48+ . pipe (
49+ filter (
50+ ( action : any ) =>
51+ action . type === UserActionTypes . GRANT_USER_ROLE_SUCCESS ||
52+ action . type === UserActionTypes . REVOKE_USER_ROLE_SUCCESS
53+ )
54+ )
55+ . subscribe ( ( action ) => {
56+ this . store . dispatch ( new LoadUsers ( ) ) ;
57+ this . rerenderDataTable ( ) ;
58+ } ) ;
3459 }
3560
3661 ngAfterViewInit ( ) : void {
@@ -52,4 +77,26 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
5277 this . dtTrigger . next ( ) ;
5378 }
5479 }
80+
81+ isAdmin ( role ) {
82+ return role ? true : false ;
83+ }
84+
85+ revokeOrGrantRole ( userId : string , userRole : string ) {
86+ userRole
87+ ? this . store . dispatch ( new RevokeRoleUser ( userId , 'admin' ) )
88+ : this . store . dispatch ( new GrantRoleUser ( userId , 'admin' ) ) ;
89+ }
90+
91+ isFeatureToggleAactivated ( ) {
92+ return this . featureManagerService . isToggleEnabledForUser ( 'ui-list-test-users' ) . pipe (
93+ map ( ( enabled ) => {
94+ if ( enabled === true ) {
95+ return true ;
96+ } else {
97+ return false ;
98+ }
99+ } )
100+ ) ;
101+ }
55102}
0 commit comments