]>
Commit | Line | Data |
---|---|---|
ab998f7b | 1 | import { Component, OnInit } from '@angular/core' |
df98563e | 2 | import { NotificationsService } from 'angular2-notifications' |
1f30a185 | 3 | import { SortMeta } from 'primeng/components/common/sortmeta' |
df98563e | 4 | import { ConfirmService } from '../../../core' |
1f30a185 | 5 | import { RestPagination, RestTable, User } from '../../../shared' |
df98563e | 6 | import { UserService } from '../shared' |
b1d40cff | 7 | import { I18n } from '@ngx-translate/i18n-polyfill' |
7da18e44 C |
8 | |
9 | @Component({ | |
10 | selector: 'my-user-list', | |
ec8d8440 C |
11 | templateUrl: './user-list.component.html', |
12 | styleUrls: [ './user-list.component.scss' ] | |
7da18e44 | 13 | }) |
ab998f7b | 14 | export class UserListComponent extends RestTable implements OnInit { |
d592e0a9 C |
15 | users: User[] = [] |
16 | totalRecords = 0 | |
17 | rowsPerPage = 10 | |
ab998f7b | 18 | sort: SortMeta = { field: 'createdAt', order: 1 } |
d592e0a9 | 19 | pagination: RestPagination = { count: this.rowsPerPage, start: 0 } |
7da18e44 | 20 | |
df98563e | 21 | constructor ( |
7ddd02c9 | 22 | private notificationsService: NotificationsService, |
5769e1db | 23 | private confirmService: ConfirmService, |
b1d40cff C |
24 | private userService: UserService, |
25 | private i18n: I18n | |
28798b5d | 26 | ) { |
d592e0a9 | 27 | super() |
7da18e44 C |
28 | } |
29 | ||
ab998f7b C |
30 | ngOnInit () { |
31 | this.loadSort() | |
32 | } | |
33 | ||
1f30a185 | 34 | async removeUser (user: User) { |
28798b5d | 35 | if (user.username === 'root') { |
b1d40cff | 36 | this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.')) |
df98563e | 37 | return |
28798b5d | 38 | } |
7da18e44 | 39 | |
b1d40cff | 40 | const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this user?'), this.i18n('Delete')) |
1f30a185 | 41 | if (res === false) return |
5769e1db | 42 | |
1f30a185 C |
43 | this.userService.removeUser(user).subscribe( |
44 | () => { | |
b1d40cff C |
45 | this.notificationsService.success( |
46 | this.i18n('Success'), | |
47 | this.i18n('User {{ username }} deleted.', { username: user.username }) | |
48 | ) | |
1f30a185 C |
49 | this.loadData() |
50 | }, | |
5769e1db | 51 | |
b1d40cff | 52 | err => this.notificationsService.error(this.i18n('Error'), err.message) |
df98563e | 53 | ) |
7da18e44 | 54 | } |
d592e0a9 C |
55 | |
56 | getRouterUserEditLink (user: User) { | |
9b9b1805 | 57 | return [ '/admin', 'users', 'update', user.id ] |
d592e0a9 C |
58 | } |
59 | ||
60 | protected loadData () { | |
61 | this.userService.getUsers(this.pagination, this.sort) | |
62 | .subscribe( | |
63 | resultList => { | |
64 | this.users = resultList.data | |
65 | this.totalRecords = resultList.total | |
66 | }, | |
67 | ||
b1d40cff | 68 | err => this.notificationsService.error(this.i18n('Error'), err.message) |
d592e0a9 C |
69 | ) |
70 | } | |
7da18e44 | 71 | } |