From f86f4731aba5f355d474ceabed03df94f50d5fb4 Mon Sep 17 00:00:00 2001 From: Israel Leon Date: Tue, 30 Mar 2021 15:35:21 -0500 Subject: [PATCH] feat: TT-190 resolve coments --- .../users-list/users-list.component.html | 4 +- .../users-list/users-list.component.spec.ts | 36 +++++++++++----- .../users-list/users-list.component.ts | 43 ++++++++++--------- 3 files changed, 50 insertions(+), 33 deletions(-) diff --git a/src/app/modules/users/components/users-list/users-list.component.html b/src/app/modules/users/components/users-list/users-list.component.html index 605a6811c..498d5e0dd 100644 --- a/src/app/modules/users/components/users-list/users-list.component.html +++ b/src/app/modules/users/components/users-list/users-list.component.html @@ -22,13 +22,13 @@
admin test diff --git a/src/app/modules/users/components/users-list/users-list.component.spec.ts b/src/app/modules/users/components/users-list/users-list.component.spec.ts index 642fa0bea..efdc37085 100644 --- a/src/app/modules/users/components/users-list/users-list.component.spec.ts +++ b/src/app/modules/users/components/users-list/users-list.component.spec.ts @@ -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'; @@ -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'); @@ -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)); }); }); @@ -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)); }); }); diff --git a/src/app/modules/users/components/users-list/users-list.component.ts b/src/app/modules/users/components/users-list/users-list.component.ts index 06540501f..33ef09035 100644 --- a/src/app/modules/users/components/users-list/users-list.component.ts +++ b/src/app/modules/users/components/users-list/users-list.component.ts @@ -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'; @@ -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( @@ -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 { + 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 { + return this.actionsSubject$.pipe( + filter( + (action: Action) => + action.type === UserActionTypes.ADD_USER_TO_GROUP_SUCCESS || + action.type === UserActionTypes.REMOVE_USER_FROM_GROUP_SUCCESS + ) ); } }