1 import { Component, OnInit } 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, User } 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'
11 selector: 'my-user-list',
12 templateUrl: './user-list.component.html',
13 styleUrls: [ './user-list.component.scss' ]
15 export class UserListComponent extends RestTable implements OnInit {
19 sort: SortMeta = { field: 'createdAt', order: 1 }
20 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
21 userActions: DropdownAction<User>[] = []
24 private notificationsService: NotificationsService,
25 private confirmService: ConfirmService,
26 private userService: UserService,
34 linkBuilder: this.getRouterUserEditLink
38 handler: user => this.removeUser(user)
47 async removeUser (user: User) {
48 if (user.username === 'root') {
49 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.'))
53 const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this user?'), this.i18n('Delete'))
54 if (res === false) return
56 this.userService.removeUser(user).subscribe(
58 this.notificationsService.success(
60 this.i18n('User {{username}} deleted.', { username: user.username })
65 err => this.notificationsService.error(this.i18n('Error'), err.message)
69 getRouterUserEditLink (user: User) {
70 return [ '/admin', 'users', 'update', user.id ]
73 protected loadData () {
74 this.userService.getUsers(this.pagination, this.sort)
77 this.users = resultList.data
78 this.totalRecords = resultList.total
81 err => this.notificationsService.error(this.i18n('Error'), err.message)