1 import { Component, OnInit, ViewChild } from '@angular/core'
2 import { Notifier } from '@app/core'
3 import { SortMeta } from 'primeng/components/common/sortmeta'
4 import { ConfirmService, ServerService } 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 notifier: Notifier,
30 private confirmService: ConfirmService,
31 private serverService: ServerService,
32 private userService: UserService,
38 get requiresEmailVerification () {
39 return this.serverService.getConfig().signup.requiresEmailVerification
45 this.bulkUserActions = [
47 label: this.i18n('Delete'),
48 handler: users => this.removeUsers(users)
51 label: this.i18n('Ban'),
52 handler: users => this.openBanUserModal(users),
53 isDisplayed: users => users.every(u => u.blocked === false)
56 label: this.i18n('Unban'),
57 handler: users => this.unbanUsers(users),
58 isDisplayed: users => users.every(u => u.blocked === true)
61 label: this.i18n('Set Email as Verified'),
62 handler: users => this.setEmailsAsVerified(users),
63 isDisplayed: users => this.requiresEmailVerification && users.every(u => !u.blocked && u.emailVerified === false)
68 openBanUserModal (users: User[]) {
69 for (const user of users) {
70 if (user.username === 'root') {
71 this.notifier.error(this.i18n('You cannot ban root.'))
76 this.userBanModal.openModal(users)
83 async unbanUsers (users: User[]) {
84 const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length })
86 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
87 if (res === false) return
89 this.userService.unbanUsers(users)
92 const message = this.i18n('{{num}} users unbanned.', { num: users.length })
94 this.notifier.success(message)
98 err => this.notifier.error(err.message)
102 async removeUsers (users: User[]) {
103 for (const user of users) {
104 if (user.username === 'root') {
105 this.notifier.error(this.i18n('You cannot delete root.'))
110 const message = this.i18n('If you remove these users, you will not be able to create others with the same username!')
111 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
112 if (res === false) return
114 this.userService.removeUser(users).subscribe(
116 this.notifier.success(this.i18n('{{num}} users deleted.', { num: users.length }))
120 err => this.notifier.error(err.message)
124 async setEmailsAsVerified (users: User[]) {
125 this.userService.updateUsers(users, { emailVerified: true }).subscribe(
127 this.notifier.success(this.i18n('{{num}} users email set as verified.', { num: users.length }))
131 err => this.notifier.error(err.message)
135 isInSelectionMode () {
136 return this.selectedUsers.length !== 0
139 protected loadData () {
140 this.selectedUsers = []
142 this.userService.getUsers(this.pagination, this.sort, this.search)
145 this.users = resultList.data
146 this.totalRecords = resultList.total
149 err => this.notifier.error(err.message)