]>
Commit | Line | Data |
---|---|---|
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 | ||
9 | @Component({ | |
10 | selector: 'my-user-list', | |
11 | templateUrl: './user-list.component.html', | |
12 | styleUrls: [ './user-list.component.scss' ] | |
13 | }) | |
14 | export class UserListComponent extends RestTable implements OnInit { | |
15 | users: User[] = [] | |
16 | totalRecords = 0 | |
17 | rowsPerPage = 10 | |
18 | sort: SortMeta = { field: 'createdAt', order: 1 } | |
19 | pagination: RestPagination = { count: this.rowsPerPage, start: 0 } | |
20 | ||
21 | constructor ( | |
22 | private notificationsService: NotificationsService, | |
23 | private confirmService: ConfirmService, | |
24 | private userService: UserService, | |
25 | private i18n: I18n | |
26 | ) { | |
27 | super() | |
28 | } | |
29 | ||
30 | ngOnInit () { | |
31 | this.loadSort() | |
32 | } | |
33 | ||
34 | async removeUser (user: User) { | |
35 | if (user.username === 'root') { | |
36 | this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.')) | |
37 | return | |
38 | } | |
39 | ||
40 | const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this user?'), this.i18n('Delete')) | |
41 | if (res === false) return | |
42 | ||
43 | this.userService.removeUser(user).subscribe( | |
44 | () => { | |
45 | this.notificationsService.success( | |
46 | this.i18n('Success'), | |
47 | this.i18n('User {{ username }} deleted.', { username: user.username }) | |
48 | ) | |
49 | this.loadData() | |
50 | }, | |
51 | ||
52 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
53 | ) | |
54 | } | |
55 | ||
56 | getRouterUserEditLink (user: User) { | |
57 | return [ '/admin', 'users', 'update', user.id ] | |
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 | ||
68 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
69 | ) | |
70 | } | |
71 | } |