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 } from '../../../shared'
6 import { UserService } from '../shared'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { DropdownAction } from '@app/shared/buttons/action-dropdown.component'
9 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
10 import { UserBanModalComponent } from '@app/+admin/users/user-list/user-ban-modal.component'
11 import { User } from '../../../../../../shared'
14 selector: 'my-user-list',
15 templateUrl: './user-list.component.html',
16 styleUrls: [ './user-list.component.scss' ]
18 export class UserListComponent extends RestTable implements OnInit {
19 @ViewChild('userBanModal') userBanModal: UserBanModalComponent
24 sort: SortMeta = { field: 'createdAt', order: 1 }
25 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
26 userActions: DropdownAction<User>[] = []
28 private userToBan: User
29 private openedModal: NgbModalRef
32 private notificationsService: NotificationsService,
33 private confirmService: ConfirmService,
34 private userService: UserService,
41 label: this.i18n('Edit'),
42 linkBuilder: this.getRouterUserEditLink
45 label: this.i18n('Delete'),
46 handler: user => this.removeUser(user)
49 label: this.i18n('Ban'),
50 handler: user => this.openBanUserModal(user),
51 isDisplayed: user => !user.blocked
54 label: this.i18n('Unban'),
55 handler: user => this.unbanUser(user),
56 isDisplayed: user => user.blocked
66 this.userToBan = undefined
67 this.openedModal.close()
70 openBanUserModal (user: User) {
71 if (user.username === 'root') {
72 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.'))
76 this.userBanModal.openModal(user)
83 async unbanUser (user: User) {
84 const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username })
85 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
86 if (res === false) return
88 this.userService.unbanUser(user)
91 this.notificationsService.success(
93 this.i18n('User {{username}} unbanned.', { username: user.username })
98 err => this.notificationsService.error(this.i18n('Error'), err.message)
102 async removeUser (user: User) {
103 if (user.username === 'root') {
104 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.'))
108 const message = this.i18n('If you remove this user, you will not be able to create another with the same username!')
109 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
110 if (res === false) return
112 this.userService.removeUser(user).subscribe(
114 this.notificationsService.success(
115 this.i18n('Success'),
116 this.i18n('User {{username}} deleted.', { username: user.username })
121 err => this.notificationsService.error(this.i18n('Error'), err.message)
125 getRouterUserEditLink (user: User) {
126 return [ '/admin', 'users', 'update', user.id ]
129 protected loadData () {
130 this.userService.getUsers(this.pagination, this.sort)
133 this.users = resultList.data
134 this.totalRecords = resultList.total
137 err => this.notificationsService.error(this.i18n('Error'), err.message)