]>
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, UserService } from '../../../shared' | |
6 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
7 | import { User } from '../../../../../../shared' | |
8 | import { UserBanModalComponent } from '@app/shared/moderation' | |
9 | import { DropdownAction } from '@app/shared/buttons/action-dropdown.component' | |
10 | ||
11 | @Component({ | |
12 | selector: 'my-user-list', | |
13 | templateUrl: './user-list.component.html', | |
14 | styleUrls: [ './user-list.component.scss' ] | |
15 | }) | |
16 | export class UserListComponent extends RestTable implements OnInit { | |
17 | @ViewChild('userBanModal') userBanModal: UserBanModalComponent | |
18 | ||
19 | users: User[] = [] | |
20 | totalRecords = 0 | |
21 | rowsPerPage = 10 | |
22 | sort: SortMeta = { field: 'createdAt', order: 1 } | |
23 | pagination: RestPagination = { count: this.rowsPerPage, start: 0 } | |
24 | ||
25 | selectedUsers: User[] = [] | |
26 | bulkUserActions: DropdownAction<User[]>[] = [] | |
27 | ||
28 | constructor ( | |
29 | private notificationsService: NotificationsService, | |
30 | private confirmService: ConfirmService, | |
31 | private userService: UserService, | |
32 | private i18n: I18n | |
33 | ) { | |
34 | super() | |
35 | } | |
36 | ||
37 | ngOnInit () { | |
38 | this.initialize() | |
39 | ||
40 | this.bulkUserActions = [ | |
41 | { | |
42 | label: this.i18n('Delete'), | |
43 | handler: users => this.removeUsers(users) | |
44 | }, | |
45 | { | |
46 | label: this.i18n('Ban'), | |
47 | handler: users => this.openBanUserModal(users), | |
48 | isDisplayed: users => users.every(u => u.blocked === false) | |
49 | }, | |
50 | { | |
51 | label: this.i18n('Unban'), | |
52 | handler: users => this.unbanUsers(users), | |
53 | isDisplayed: users => users.every(u => u.blocked === true) | |
54 | } | |
55 | ] | |
56 | } | |
57 | ||
58 | openBanUserModal (users: User[]) { | |
59 | for (const user of users) { | |
60 | if (user.username === 'root') { | |
61 | this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.')) | |
62 | return | |
63 | } | |
64 | } | |
65 | ||
66 | this.userBanModal.openModal(users) | |
67 | } | |
68 | ||
69 | onUsersBanned () { | |
70 | this.loadData() | |
71 | } | |
72 | ||
73 | async unbanUsers (users: User[]) { | |
74 | const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length }) | |
75 | ||
76 | const res = await this.confirmService.confirm(message, this.i18n('Unban')) | |
77 | if (res === false) return | |
78 | ||
79 | this.userService.unbanUsers(users) | |
80 | .subscribe( | |
81 | () => { | |
82 | const message = this.i18n('{{num}} users unbanned.', { num: users.length }) | |
83 | ||
84 | this.notificationsService.success(this.i18n('Success'), message) | |
85 | this.loadData() | |
86 | }, | |
87 | ||
88 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
89 | ) | |
90 | } | |
91 | ||
92 | async removeUsers (users: User[]) { | |
93 | for (const user of users) { | |
94 | if (user.username === 'root') { | |
95 | this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.')) | |
96 | return | |
97 | } | |
98 | } | |
99 | ||
100 | const message = this.i18n('If you remove these users, you will not be able to create others with the same username!') | |
101 | const res = await this.confirmService.confirm(message, this.i18n('Delete')) | |
102 | if (res === false) return | |
103 | ||
104 | this.userService.removeUser(users).subscribe( | |
105 | () => { | |
106 | this.notificationsService.success( | |
107 | this.i18n('Success'), | |
108 | this.i18n('{{num}} users deleted.', { num: users.length }) | |
109 | ) | |
110 | this.loadData() | |
111 | }, | |
112 | ||
113 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
114 | ) | |
115 | } | |
116 | ||
117 | isInSelectionMode () { | |
118 | return this.selectedUsers.length !== 0 | |
119 | } | |
120 | ||
121 | protected loadData () { | |
122 | this.selectedUsers = [] | |
123 | ||
124 | this.userService.getUsers(this.pagination, this.sort, this.search) | |
125 | .subscribe( | |
126 | resultList => { | |
127 | this.users = resultList.data | |
128 | this.totalRecords = resultList.total | |
129 | }, | |
130 | ||
131 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
132 | ) | |
133 | } | |
134 | } |