1 import { Component, OnInit, ViewChild } from '@angular/core'
2 import { NotificationsService } from 'angular2-notifications'
3 import { SortMeta } from 'primeng/components/common/sortmeta'
4 import { ConfirmService } from '../../../core'
5 import { RestPagination, RestTable, UserService } from '../../../shared'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { User } from '../../../../../../shared'
8 import { UserBanModalComponent } from '@app/shared/moderation'
9 import { DropdownAction } from '@app/shared/buttons/action-dropdown.component'
12 selector: 'my-user-list',
13 templateUrl: './user-list.component.html',
14 styleUrls: [ './user-list.component.scss' ]
16 export class UserListComponent extends RestTable implements OnInit {
17 @ViewChild('userBanModal') userBanModal: UserBanModalComponent
22 sort: SortMeta = { field: 'createdAt', order: 1 }
23 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
25 selectedUsers: User[] = []
26 bulkUserActions: DropdownAction<User>[] = []
29 private notificationsService: NotificationsService,
30 private confirmService: ConfirmService,
31 private userService: UserService,
40 this.bulkUserActions = [
42 label: this.i18n('Delete'),
43 handler: users => this.removeUsers(users)
46 label: this.i18n('Ban'),
47 handler: users => this.openBanUserModal(users),
48 isDisplayed: users => users.every(u => u.blocked === false)
51 label: this.i18n('Unban'),
52 handler: users => this.unbanUsers(users),
53 isDisplayed: users => users.every(u => u.blocked === true)
58 protected loadData () {
59 this.selectedUsers = []
61 this.userService.getUsers(this.pagination, this.sort)
64 this.users = resultList.data
65 this.totalRecords = resultList.total
68 err => this.notificationsService.error(this.i18n('Error'), err.message)
72 openBanUserModal (users: User[]) {
73 for (const user of users) {
74 if (user.username === 'root') {
75 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.'))
80 this.userBanModal.openModal(users)
87 async unbanUsers (users: User[]) {
88 const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length })
90 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
91 if (res === false) return
93 this.userService.unbanUsers(users)
96 const message = this.i18n('{{num}} users unbanned.', { num: users.length })
98 this.notificationsService.success(this.i18n('Success'), message)
102 err => this.notificationsService.error(this.i18n('Error'), err.message)
106 async removeUsers (users: User[]) {
107 for (const user of users) {
108 if (user.username === 'root') {
109 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.'))
114 const message = this.i18n('If you remove these users, you will not be able to create others with the same username!')
115 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
116 if (res === false) return
118 this.userService.removeUser(users).subscribe(
120 this.notificationsService.success(
121 this.i18n('Success'),
122 this.i18n('{{num}} users deleted.', { num: users.length })
127 err => this.notificationsService.error(this.i18n('Error'), err.message)
131 isInSelectionMode () {
132 return this.selectedUsers.length !== 0