Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat: TT-190 resolve coments
  • Loading branch information
LEON12699 committed Mar 30, 2021
commit f86f4731aba5f355d474ceabed03df94f50d5fb4
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
<div *ngIf="isUserGroupsToggleOn">
<ui-switch
size="small"
(change)="switchGroup(user.id, user.groups, 'time-tracker-admin')"
(change)="switchGroup('time-tracker-admin', user)"
[checked]="user.groups.includes('time-tracker-admin')"
></ui-switch>
admin
<ui-switch
size="small"
(change)="switchGroup(user.id, user.groups, 'time-tracker-tester')"
(change)="switchGroup('time-tracker-tester', user)"
[checked]="user.groups.includes('time-tracker-tester')"
></ui-switch>
test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
AddUserToGroup,
RemoveUserFromGroup,
} from '../../store';
import { User } from '../../../user/models/user';
import { ActionsSubject } from '@ngrx/store';
import { DataTablesModule } from 'angular-datatables';
import { Observable, of } from 'rxjs';
Expand Down Expand Up @@ -118,12 +119,12 @@ describe('UsersListComponent', () => {
});
});

const actionsGroupsParams = [
const actionGroupParams = [
{ actionType: UserActionTypes.ADD_USER_TO_GROUP_SUCCESS },
{ actionType: UserActionTypes.REMOVE_USER_FROM_GROUP_SUCCESS },
];

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

Expand Down Expand Up @@ -167,14 +168,21 @@ describe('UsersListComponent', () => {
AddGroupTypes.map((param) => {
it(`When user switchGroup to ${param.groupName} and doesn't belong to any group, should add ${param.groupName} group to user`, () => {
const groupName = param.groupName;
const userGroups = [];
const userId = 'userId';
const user = {
name: 'name',
email: 'email',
roles: [],
groups: [],
id: 'id',
tenant_id: 'tenant id',
deleted: 'delete',
} ;

spyOn(store, 'dispatch');

component.switchGroup(userId, userGroups, groupName);
component.switchGroup(groupName, user);

expect(store.dispatch).toHaveBeenCalledWith(new AddUserToGroup(userId, groupName));
expect(store.dispatch).toHaveBeenCalledWith(new AddUserToGroup(user.id, groupName));
});
});

Expand Down Expand Up @@ -206,14 +214,22 @@ describe('UsersListComponent', () => {
removeGroupTypes.map((param) => {
it(`When user switchGroup to ${param.groupName} and belongs to group, should remove ${param.groupName} group from user`, () => {
const groupName = param.groupName;
const userGroups = param.userGroups;
const userId = 'userId';
const user = {
name: 'name',
email: 'email',
roles: [],
groups: param.userGroups,
id: 'id',
tenant_id: 'tenant id',
deleted: 'delete',
} ;


spyOn(store, 'dispatch');

component.switchGroup(userId, userGroups, groupName);
component.switchGroup(groupName, user);

expect(store.dispatch).toHaveBeenCalledWith(new RemoveUserFromGroup(userId, groupName));
expect(store.dispatch).toHaveBeenCalledWith(new RemoveUserFromGroup(user.id, groupName));
});
});

Expand Down
43 changes: 22 additions & 21 deletions src/app/modules/users/components/users-list/users-list.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AfterViewInit, Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { ActionsSubject, select, Store } from '@ngrx/store';
import { ActionsSubject, select, Store, Action } from '@ngrx/store';
import { DataTableDirective } from 'angular-datatables';
import { Observable, Subject, Subscription } from 'rxjs';
import { delay, filter, map } from 'rxjs/operators';
Expand Down Expand Up @@ -54,17 +54,9 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
this.isUserGroupsToggleOn = flag;
});

this.switchGroupsSubscription = this.actionsSubject$
.pipe(
filter(
(action: any) =>
action.type === UserActionTypes.ADD_USER_TO_GROUP_SUCCESS ||
action.type === UserActionTypes.REMOVE_USER_FROM_GROUP_SUCCESS
)
)
.subscribe((action) => {
this.store.dispatch(new LoadUsers());
});
this.switchGroupsSubscription = this.filterUserGroup().subscribe((action) => {
this.store.dispatch(new LoadUsers());
});

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

switchGroup(userId: string, userGroups: string[], groupName: string) {
userGroups.includes(groupName)
? this.store.dispatch(new RemoveUserFromGroup(userId, groupName))
: this.store.dispatch(new AddUserToGroup(userId, groupName));
switchGroup(groupName: string, user: User): void {
this.store.dispatch(
user.groups.includes(groupName)
? new RemoveUserFromGroup(user.id, groupName)
: new AddUserToGroup(user.id, groupName)
);
}

isFeatureToggleActivated(): Observable<boolean> {
return this.featureManagerService.isToggleEnabledForUser('switch-group')
.pipe(map((enabled: boolean) => enabled));
}

isFeatureToggleActivated() {
return this.featureManagerService.isToggleEnabledForUser('switch-group').pipe(
map((enabled) => {
return enabled === true ? true : false;
})
filterUserGroup(): Observable<Action> {
return this.actionsSubject$.pipe(
filter(
(action: Action) =>
action.type === UserActionTypes.ADD_USER_TO_GROUP_SUCCESS ||
action.type === UserActionTypes.REMOVE_USER_FROM_GROUP_SUCCESS
)
);
}
}