From 0c0d77e22006077742e901ddfcfe74f5939c130a Mon Sep 17 00:00:00 2001 From: Lebeau Elise Date: Fri, 10 Dec 2021 11:28:12 +0000 Subject: [PATCH] user management, add multiple user selection --- .../delete-user-dialog.component.html | 5 ++- .../delete-user-dialog.component.scss | 4 ++ .../delete-user-dialog.component.ts | 14 ++++++- .../user-management.component.html | 18 +++++++++ .../user-management.component.scss | 4 ++ .../user-management.component.ts | 40 +++++++++++++++++-- src/app/models/users/user.ts | 1 + src/app/services/user.service.ts | 1 - 8 files changed, 80 insertions(+), 7 deletions(-) diff --git a/src/app/components/user-management/delete-user-dialog/delete-user-dialog.component.html b/src/app/components/user-management/delete-user-dialog/delete-user-dialog.component.html index dc1287c1..d15b5b40 100644 --- a/src/app/components/user-management/delete-user-dialog/delete-user-dialog.component.html +++ b/src/app/components/user-management/delete-user-dialog/delete-user-dialog.component.html @@ -1,4 +1,7 @@ -

Are you sure to delete user named: '{{data.user.username}}' ?

+

Are you sure you want to delete the following users ?

+
@@ -17,6 +20,21 @@
+ + + + + + + + + + + Name diff --git a/src/app/components/user-management/user-management.component.scss b/src/app/components/user-management/user-management.component.scss index b264352a..03241487 100644 --- a/src/app/components/user-management/user-management.component.scss +++ b/src/app/components/user-management/user-management.component.scss @@ -9,3 +9,7 @@ margin-left: -470px; left: 50%; } + +.small-col { + flex-grow: 0.5; +} diff --git a/src/app/components/user-management/user-management.component.ts b/src/app/components/user-management/user-management.component.ts index ce3c2ae3..6c82fce0 100644 --- a/src/app/components/user-management/user-management.component.ts +++ b/src/app/components/user-management/user-management.component.ts @@ -18,7 +18,7 @@ import {UserService} from "@services/user.service"; import {ProgressService} from "../../common/progress/progress.service"; import {User} from "@models/users/user"; import {BehaviorSubject, merge, Observable} from "rxjs"; -import {DataSource} from "@angular/cdk/collections"; +import {DataSource, SelectionModel} from "@angular/cdk/collections"; import {map} from "rxjs/operators"; import {AddUserDialogComponent} from "@components/user-management/add-user-dialog/add-user-dialog.component"; import {MatDialog} from "@angular/material/dialog"; @@ -34,8 +34,8 @@ export class UserManagementComponent implements OnInit { server: Server; dataSource: UserDataSource; userDatabase = new UserDatabase(); - displayedColumns = ['name', 'email', 'is_active', 'is_superadmin', 'updated_at', 'delete']; - + displayedColumns = ['select', 'name', 'email', 'is_active', 'is_superadmin', 'updated_at', 'delete']; + selection = new SelectionModel(true, []); searchText: string = ''; @ViewChild(MatSort, { static: true }) sort: MatSort; @@ -84,7 +84,7 @@ export class UserManagementComponent implements OnInit { onDelete(user: User) { this.dialog - .open(DeleteUserDialogComponent, {width: '500px', data: {user: user}}) + .open(DeleteUserDialogComponent, {width: '500px', data: {users: [user]}}) .afterClosed() .subscribe((isDeletedConfirm) => { if (isDeletedConfirm) { @@ -97,6 +97,38 @@ export class UserManagementComponent implements OnInit { } }); } + + isAllSelected() { + const numSelected = this.selection.selected.length; + const numRows = this.userDatabase.data.length; + return numSelected === numRows; + } + + masterToggle() { + this.isAllSelected() ? + this.selection.clear() : + this.userDatabase.data.forEach(row => this.selection.select(row)); + } + + deleteMultiple() { + this.dialog + .open(DeleteUserDialogComponent, {width: '500px', data: {users: this.selection.selected}}) + .afterClosed() + .subscribe((isDeletedConfirm) => { + if (isDeletedConfirm) { + this.selection.selected.forEach((user: User) => { + this.userService.delete(this.server, user.user_id) + .subscribe(() => { + this.refresh() + }, (error) => { + this.toasterService.error(`An error occur while trying to delete user ${user.username}`); + }); + }) + this.selection.clear(); + } + }); + + } } export class UserDatabase { diff --git a/src/app/models/users/user.ts b/src/app/models/users/user.ts index 880734dd..a385cc85 100644 --- a/src/app/models/users/user.ts +++ b/src/app/models/users/user.ts @@ -2,6 +2,7 @@ export interface User { created_at: string; email: string; full_name: string; + last_login: string; is_active: boolean; is_superadmin: boolean; updated_at: string; diff --git a/src/app/services/user.service.ts b/src/app/services/user.service.ts index e49d76fd..8cd4c0b4 100644 --- a/src/app/services/user.service.ts +++ b/src/app/services/user.service.ts @@ -20,7 +20,6 @@ export class UserService { } add(server: Server, user: any): Observable { - console.log(user) return this.httpServer.post(server, `/users`, { username: user.username, is_active: user.is_active,