Skip to content

Commit c085ab1

Browse files
committed
refactor: TT-310 Improve role implementation
1 parent 38d0201 commit c085ab1

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
<td class="col-4 text-break">{{ user.email }}</td>
1515
<td class="col-5 text-break">{{ user.name }}</td>
1616
<td class="col-3 text-center">
17-
<ui-switch size="small" (change)="switchGroup('time-tracker-admin', user); updateRole(ROLES.admin, user);"
17+
<ui-switch size="small"
18+
(change)="switchGroup('time-tracker-admin', user); updateRole(ROLES.admin, user, $event);"
1819
[checked]="user.groups.includes('time-tracker-admin')"></ui-switch>
1920
admin
2021
<ui-switch size="small" (change)="switchGroup('time-tracker-tester', user)"
@@ -24,4 +25,4 @@
2425
</tr>
2526
</tbody>
2627
</table>
27-
</div>
28+
</div>

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ describe('UsersListComponent', () => {
168168
expect(component.dtElement.dtInstance.then).toHaveBeenCalled();
169169
});
170170

171-
it('When the user does not have the role, this role must be added', () => {
171+
it('When the toggle is enabled, the role must be added to the user ', () => {
172172
const availableRoles = [
173173
{
174174
name: 'admin',
@@ -190,12 +190,13 @@ describe('UsersListComponent', () => {
190190
};
191191

192192
availableRoles.forEach((role) => {
193-
component.updateRole(role, user);
193+
const isToggleEnabled = true;
194+
component.updateRole(role, user, isToggleEnabled);
194195
expect(store.dispatch).toHaveBeenCalledWith(new GrantUserRole(user.id, role.name));
195196
});
196197
});
197198

198-
it('When the user has the role, this role must be removed', () => {
199+
it('When the toggle is disabled, the role must be removed from the user', () => {
199200
const availableRoles = [
200201
{
201202
name: 'admin',
@@ -217,7 +218,8 @@ describe('UsersListComponent', () => {
217218
};
218219

219220
availableRoles.forEach((role) => {
220-
component.updateRole(role, user);
221+
const isToggleEnabled = false;
222+
component.updateRole(role, user, isToggleEnabled);
221223
expect(store.dispatch).toHaveBeenCalledWith(new RevokeUserRole(user.id, role.name));
222224
});
223225
});

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,8 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
7575
);
7676
}
7777

78-
updateRole(role: { name: string; value: string }, user: User) {
79-
const userHasRole = user.roles.includes(role.value);
80-
const action = userHasRole ? new RevokeUserRole(user.id, role.name) : new GrantUserRole(user.id, role.name);
81-
78+
updateRole(role: { name: string; value: string }, user: User, isToggleEnabled: boolean) {
79+
const action = isToggleEnabled ? new GrantUserRole(user.id, role.name) : new RevokeUserRole(user.id, role.name);
8280
this.store.dispatch(action);
8381
}
8482

0 commit comments

Comments
 (0)