]>
Commit | Line | Data |
---|---|---|
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' | |
12 | ||
13 | @Component({ | |
14 | selector: 'my-user-list', | |
15 | templateUrl: './user-list.component.html', | |
16 | styleUrls: [ './user-list.component.scss' ] | |
17 | }) | |
18 | export class UserListComponent extends RestTable implements OnInit { | |
19 | @ViewChild('userBanModal') userBanModal: UserBanModalComponent | |
20 | ||
21 | users: User[] = [] | |
22 | totalRecords = 0 | |
23 | rowsPerPage = 10 | |
24 | sort: SortMeta = { field: 'createdAt', order: 1 } | |
25 | pagination: RestPagination = { count: this.rowsPerPage, start: 0 } | |
26 | userActions: DropdownAction<User>[] = [] | |
27 | ||
28 | private userToBan: User | |
29 | private openedModal: NgbModalRef | |
30 | ||
31 | constructor ( | |
32 | private notificationsService: NotificationsService, | |
33 | private confirmService: ConfirmService, | |
34 | private userService: UserService, | |
35 | private i18n: I18n | |
36 | ) { | |
37 | super() | |
38 | ||
39 | this.userActions = [ | |
40 | { | |
41 | label: this.i18n('Edit'), | |
42 | linkBuilder: this.getRouterUserEditLink | |
43 | }, | |
44 | { | |
45 | label: this.i18n('Delete'), | |
46 | handler: user => this.removeUser(user) | |
47 | }, | |
48 | { | |
49 | label: this.i18n('Ban'), | |
50 | handler: user => this.openBanUserModal(user), | |
51 | isDisplayed: user => !user.blocked | |
52 | }, | |
53 | { | |
54 | label: this.i18n('Unban'), | |
55 | handler: user => this.unbanUser(user), | |
56 | isDisplayed: user => user.blocked | |
57 | } | |
58 | ] | |
59 | } | |
60 | ||
61 | ngOnInit () { | |
62 | this.loadSort() | |
63 | } | |
64 | ||
65 | hideBanUserModal () { | |
66 | this.userToBan = undefined | |
67 | this.openedModal.close() | |
68 | } | |
69 | ||
70 | openBanUserModal (user: User) { | |
71 | if (user.username === 'root') { | |
72 | this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.')) | |
73 | return | |
74 | } | |
75 | ||
76 | this.userBanModal.openModal(user) | |
77 | } | |
78 | ||
79 | onUserBanned () { | |
80 | this.loadData() | |
81 | } | |
82 | ||
83 | async unbanUser (user: User) { | |
84 | const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username }) | |
85 | const res = await this.confirmService.confirm(message, this.i18n('Unban')) | |
86 | if (res === false) return | |
87 | ||
88 | this.userService.unbanUser(user) | |
89 | .subscribe( | |
90 | () => { | |
91 | this.notificationsService.success( | |
92 | this.i18n('Success'), | |
93 | this.i18n('User {{username}} unbanned.', { username: user.username }) | |
94 | ) | |
95 | this.loadData() | |
96 | }, | |
97 | ||
98 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
99 | ) | |
100 | } | |
101 | ||
102 | async removeUser (user: User) { | |
103 | if (user.username === 'root') { | |
104 | this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.')) | |
105 | return | |
106 | } | |
107 | ||
108 | const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this user?'), this.i18n('Delete')) | |
109 | if (res === false) return | |
110 | ||
111 | this.userService.removeUser(user).subscribe( | |
112 | () => { | |
113 | this.notificationsService.success( | |
114 | this.i18n('Success'), | |
115 | this.i18n('User {{username}} deleted.', { username: user.username }) | |
116 | ) | |
117 | this.loadData() | |
118 | }, | |
119 | ||
120 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
121 | ) | |
122 | } | |
123 | ||
124 | getRouterUserEditLink (user: User) { | |
125 | return [ '/admin', 'users', 'update', user.id ] | |
126 | } | |
127 | ||
128 | protected loadData () { | |
129 | this.userService.getUsers(this.pagination, this.sort) | |
130 | .subscribe( | |
131 | resultList => { | |
132 | this.users = resultList.data | |
133 | this.totalRecords = resultList.total | |
134 | }, | |
135 | ||
136 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
137 | ) | |
138 | } | |
139 | } |