Skip to content

Commit f86f473

Browse files
committed
feat: TT-190 resolve coments
1 parent 14711ce commit f86f473

File tree

3 files changed

+50
-33
lines changed

3 files changed

+50
-33
lines changed

src/app/modules/users/components/users-list/users-list.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
<div *ngIf="isUserGroupsToggleOn">
2323
<ui-switch
2424
size="small"
25-
(change)="switchGroup(user.id, user.groups, 'time-tracker-admin')"
25+
(change)="switchGroup('time-tracker-admin', user)"
2626
[checked]="user.groups.includes('time-tracker-admin')"
2727
></ui-switch>
2828
admin
2929
<ui-switch
3030
size="small"
31-
(change)="switchGroup(user.id, user.groups, 'time-tracker-tester')"
31+
(change)="switchGroup('time-tracker-tester', user)"
3232
[checked]="user.groups.includes('time-tracker-tester')"
3333
></ui-switch>
3434
test

src/app/modules/users/components/users-list/users-list.component.spec.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
AddUserToGroup,
1313
RemoveUserFromGroup,
1414
} from '../../store';
15+
import { User } from '../../../user/models/user';
1516
import { ActionsSubject } from '@ngrx/store';
1617
import { DataTablesModule } from 'angular-datatables';
1718
import { Observable, of } from 'rxjs';
@@ -118,12 +119,12 @@ describe('UsersListComponent', () => {
118119
});
119120
});
120121

121-
const actionsGroupsParams = [
122+
const actionGroupParams = [
122123
{ actionType: UserActionTypes.ADD_USER_TO_GROUP_SUCCESS },
123124
{ actionType: UserActionTypes.REMOVE_USER_FROM_GROUP_SUCCESS },
124125
];
125126

126-
actionsGroupsParams.map((param) => {
127+
actionGroupParams.map((param) => {
127128
it(`When action ${param.actionType} is dispatched should triggered load Users action`, () => {
128129
spyOn(store, 'dispatch');
129130

@@ -167,14 +168,21 @@ describe('UsersListComponent', () => {
167168
AddGroupTypes.map((param) => {
168169
it(`When user switchGroup to ${param.groupName} and doesn't belong to any group, should add ${param.groupName} group to user`, () => {
169170
const groupName = param.groupName;
170-
const userGroups = [];
171-
const userId = 'userId';
171+
const user = {
172+
name: 'name',
173+
email: 'email',
174+
roles: [],
175+
groups: [],
176+
id: 'id',
177+
tenant_id: 'tenant id',
178+
deleted: 'delete',
179+
} ;
172180

173181
spyOn(store, 'dispatch');
174182

175-
component.switchGroup(userId, userGroups, groupName);
183+
component.switchGroup(groupName, user);
176184

177-
expect(store.dispatch).toHaveBeenCalledWith(new AddUserToGroup(userId, groupName));
185+
expect(store.dispatch).toHaveBeenCalledWith(new AddUserToGroup(user.id, groupName));
178186
});
179187
});
180188

@@ -206,14 +214,22 @@ describe('UsersListComponent', () => {
206214
removeGroupTypes.map((param) => {
207215
it(`When user switchGroup to ${param.groupName} and belongs to group, should remove ${param.groupName} group from user`, () => {
208216
const groupName = param.groupName;
209-
const userGroups = param.userGroups;
210-
const userId = 'userId';
217+
const user = {
218+
name: 'name',
219+
email: 'email',
220+
roles: [],
221+
groups: param.userGroups,
222+
id: 'id',
223+
tenant_id: 'tenant id',
224+
deleted: 'delete',
225+
} ;
226+
211227

212228
spyOn(store, 'dispatch');
213229

214-
component.switchGroup(userId, userGroups, groupName);
230+
component.switchGroup(groupName, user);
215231

216-
expect(store.dispatch).toHaveBeenCalledWith(new RemoveUserFromGroup(userId, groupName));
232+
expect(store.dispatch).toHaveBeenCalledWith(new RemoveUserFromGroup(user.id, groupName));
217233
});
218234
});
219235

src/app/modules/users/components/users-list/users-list.component.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AfterViewInit, Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
2-
import { ActionsSubject, select, Store } from '@ngrx/store';
2+
import { ActionsSubject, select, Store, Action } from '@ngrx/store';
33
import { DataTableDirective } from 'angular-datatables';
44
import { Observable, Subject, Subscription } from 'rxjs';
55
import { delay, filter, map } from 'rxjs/operators';
@@ -54,17 +54,9 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
5454
this.isUserGroupsToggleOn = flag;
5555
});
5656

57-
this.switchGroupsSubscription = this.actionsSubject$
58-
.pipe(
59-
filter(
60-
(action: any) =>
61-
action.type === UserActionTypes.ADD_USER_TO_GROUP_SUCCESS ||
62-
action.type === UserActionTypes.REMOVE_USER_FROM_GROUP_SUCCESS
63-
)
64-
)
65-
.subscribe((action) => {
66-
this.store.dispatch(new LoadUsers());
67-
});
57+
this.switchGroupsSubscription = this.filterUserGroup().subscribe((action) => {
58+
this.store.dispatch(new LoadUsers());
59+
});
6860

6961
this.switchRoleSubscription = this.actionsSubject$
7062
.pipe(
@@ -106,17 +98,26 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
10698
: this.store.dispatch(new GrantRoleUser(userId, roleId));
10799
}
108100

109-
switchGroup(userId: string, userGroups: string[], groupName: string) {
110-
userGroups.includes(groupName)
111-
? this.store.dispatch(new RemoveUserFromGroup(userId, groupName))
112-
: this.store.dispatch(new AddUserToGroup(userId, groupName));
101+
switchGroup(groupName: string, user: User): void {
102+
this.store.dispatch(
103+
user.groups.includes(groupName)
104+
? new RemoveUserFromGroup(user.id, groupName)
105+
: new AddUserToGroup(user.id, groupName)
106+
);
107+
}
108+
109+
isFeatureToggleActivated(): Observable<boolean> {
110+
return this.featureManagerService.isToggleEnabledForUser('switch-group')
111+
.pipe(map((enabled: boolean) => enabled));
113112
}
114113

115-
isFeatureToggleActivated() {
116-
return this.featureManagerService.isToggleEnabledForUser('switch-group').pipe(
117-
map((enabled) => {
118-
return enabled === true ? true : false;
119-
})
114+
filterUserGroup(): Observable<Action> {
115+
return this.actionsSubject$.pipe(
116+
filter(
117+
(action: Action) =>
118+
action.type === UserActionTypes.ADD_USER_TO_GROUP_SUCCESS ||
119+
action.type === UserActionTypes.REMOVE_USER_FROM_GROUP_SUCCESS
120+
)
120121
);
121122
}
122123
}

0 commit comments

Comments
 (0)