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