]>
Commit | Line | Data |
---|---|---|
791645e6 | 1 | import { Component, OnInit, ViewChild } 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' |
e724fa93 | 5 | import { RestPagination, RestTable, UserService } from '../../../shared' |
b1d40cff | 6 | import { I18n } from '@ngx-translate/i18n-polyfill' |
8569a870 | 7 | import { User } from '../../../../../../shared' |
791645e6 C |
8 | import { UserBanModalComponent } from '@app/shared/moderation' |
9 | import { DropdownAction } from '@app/shared/buttons/action-dropdown.component' | |
7da18e44 C |
10 | |
11 | @Component({ | |
12 | selector: 'my-user-list', | |
ec8d8440 C |
13 | templateUrl: './user-list.component.html', |
14 | styleUrls: [ './user-list.component.scss' ] | |
7da18e44 | 15 | }) |
ab998f7b | 16 | export class UserListComponent extends RestTable implements OnInit { |
791645e6 C |
17 | @ViewChild('userBanModal') userBanModal: UserBanModalComponent |
18 | ||
d592e0a9 C |
19 | users: User[] = [] |
20 | totalRecords = 0 | |
21 | rowsPerPage = 10 | |
ab998f7b | 22 | sort: SortMeta = { field: 'createdAt', order: 1 } |
d592e0a9 | 23 | pagination: RestPagination = { count: this.rowsPerPage, start: 0 } |
141b177d | 24 | |
791645e6 C |
25 | selectedUsers: User[] = [] |
26 | bulkUserActions: DropdownAction<User>[] = [] | |
27 | ||
df98563e | 28 | constructor ( |
7ddd02c9 | 29 | private notificationsService: NotificationsService, |
5769e1db | 30 | private confirmService: ConfirmService, |
b1d40cff C |
31 | private userService: UserService, |
32 | private i18n: I18n | |
28798b5d | 33 | ) { |
d592e0a9 | 34 | super() |
7da18e44 C |
35 | } |
36 | ||
ab998f7b C |
37 | ngOnInit () { |
38 | this.loadSort() | |
ab998f7b | 39 | |
791645e6 C |
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 | ] | |
141b177d C |
56 | } |
57 | ||
d592e0a9 | 58 | protected loadData () { |
791645e6 C |
59 | this.selectedUsers = [] |
60 | ||
d592e0a9 C |
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 | } | |
791645e6 C |
71 | |
72 | openBanUserModal (users: User[]) { | |
73 | for (const user of users) { | |
74 | if (user.username === 'root') { | |
75 | this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.')) | |
76 | return | |
77 | } | |
78 | } | |
79 | ||
80 | this.userBanModal.openModal(users) | |
81 | } | |
82 | ||
83 | onUsersBanned () { | |
84 | this.loadData() | |
85 | } | |
86 | ||
87 | async unbanUsers (users: User[]) { | |
88 | const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length }) | |
89 | ||
90 | const res = await this.confirmService.confirm(message, this.i18n('Unban')) | |
91 | if (res === false) return | |
92 | ||
93 | this.userService.unbanUsers(users) | |
94 | .subscribe( | |
95 | () => { | |
96 | const message = this.i18n('{{num}} users unbanned.', { num: users.length }) | |
97 | ||
98 | this.notificationsService.success(this.i18n('Success'), message) | |
99 | this.loadData() | |
100 | }, | |
101 | ||
102 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
103 | ) | |
104 | } | |
105 | ||
106 | async removeUsers (users: User[]) { | |
107 | for (const user of users) { | |
108 | if (user.username === 'root') { | |
109 | this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.')) | |
110 | return | |
111 | } | |
112 | } | |
113 | ||
114 | const message = this.i18n('If you remove these users, you will not be able to create others with the same username!') | |
115 | const res = await this.confirmService.confirm(message, this.i18n('Delete')) | |
116 | if (res === false) return | |
117 | ||
118 | this.userService.removeUser(users).subscribe( | |
119 | () => { | |
120 | this.notificationsService.success( | |
121 | this.i18n('Success'), | |
122 | this.i18n('{{num}} users deleted.', { num: users.length }) | |
123 | ) | |
124 | this.loadData() | |
125 | }, | |
126 | ||
127 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
128 | ) | |
129 | } | |
130 | ||
131 | isInSelectionMode () { | |
132 | return this.selectedUsers.length !== 0 | |
133 | } | |
7da18e44 | 134 | } |