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 openedModal: NgbModalRef
31 private notificationsService: NotificationsService,
32 private confirmService: ConfirmService,
33 private userService: UserService,
40 label: this.i18n('Edit'),
41 linkBuilder: this.getRouterUserEditLink
44 label: this.i18n('Delete'),
45 handler: user => this.removeUser(user)
48 label: this.i18n('Ban'),
49 handler: user => this.openBanUserModal(user),
50 isDisplayed: user => !user.blocked
53 label: this.i18n('Unban'),
54 handler: user => this.unbanUser(user),
55 isDisplayed: user => user.blocked
65 this.openedModal.close()
68 openBanUserModal (user: User) {
69 if (user.username === 'root') {
70 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.'))
74 this.userBanModal.openModal(user)
81 async unbanUser (user: User) {
82 const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username })
83 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
84 if (res === false) return
86 this.userService.unbanUser(user)
89 this.notificationsService.success(
91 this.i18n('User {{username}} unbanned.', { username: user.username })
96 err => this.notificationsService.error(this.i18n('Error'), err.message)
100 async removeUser (user: User) {
101 if (user.username === 'root') {
102 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.'))
106 const message = this.i18n('If you remove this user, you will not be able to create another with the same username!')
107 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
108 if (res === false) return
110 this.userService.removeUser(user).subscribe(
112 this.notificationsService.success(
113 this.i18n('Success'),
114 this.i18n('User {{username}} deleted.', { username: user.username })
119 err => this.notificationsService.error(this.i18n('Error'), err.message)
123 getRouterUserEditLink (user: User) {
124 return [ '/admin', 'users', 'update', user.id ]
127 protected loadData () {
128 this.userService.getUsers(this.pagination, this.sort)
131 this.users = resultList.data
132 this.totalRecords = resultList.total
135 err => this.notificationsService.error(this.i18n('Error'), err.message)