]>
Commit | Line | Data |
---|---|---|
141b177d | 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' |
8569a870 | 5 | import { RestPagination, RestTable } from '../../../shared' |
df98563e | 6 | import { UserService } from '../shared' |
b1d40cff | 7 | import { I18n } from '@ngx-translate/i18n-polyfill' |
eacb25c4 | 8 | import { DropdownAction } from '@app/shared/buttons/action-dropdown.component' |
141b177d C |
9 | import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref' |
10 | import { UserBanModalComponent } from '@app/+admin/users/user-list/user-ban-modal.component' | |
8569a870 | 11 | import { User } from '../../../../../../shared' |
7da18e44 C |
12 | |
13 | @Component({ | |
14 | selector: 'my-user-list', | |
ec8d8440 C |
15 | templateUrl: './user-list.component.html', |
16 | styleUrls: [ './user-list.component.scss' ] | |
7da18e44 | 17 | }) |
ab998f7b | 18 | export class UserListComponent extends RestTable implements OnInit { |
141b177d C |
19 | @ViewChild('userBanModal') userBanModal: UserBanModalComponent |
20 | ||
d592e0a9 C |
21 | users: User[] = [] |
22 | totalRecords = 0 | |
23 | rowsPerPage = 10 | |
ab998f7b | 24 | sort: SortMeta = { field: 'createdAt', order: 1 } |
d592e0a9 | 25 | pagination: RestPagination = { count: this.rowsPerPage, start: 0 } |
eacb25c4 | 26 | userActions: DropdownAction<User>[] = [] |
7da18e44 | 27 | |
141b177d C |
28 | private userToBan: User |
29 | private openedModal: NgbModalRef | |
30 | ||
df98563e | 31 | constructor ( |
7ddd02c9 | 32 | private notificationsService: NotificationsService, |
5769e1db | 33 | private confirmService: ConfirmService, |
b1d40cff C |
34 | private userService: UserService, |
35 | private i18n: I18n | |
28798b5d | 36 | ) { |
d592e0a9 | 37 | super() |
eacb25c4 C |
38 | |
39 | this.userActions = [ | |
40 | { | |
141b177d | 41 | label: this.i18n('Edit'), |
eacb25c4 C |
42 | linkBuilder: this.getRouterUserEditLink |
43 | }, | |
44 | { | |
141b177d | 45 | label: this.i18n('Delete'), |
eacb25c4 | 46 | handler: user => this.removeUser(user) |
141b177d C |
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 | |
eacb25c4 C |
57 | } |
58 | ] | |
7da18e44 C |
59 | } |
60 | ||
ab998f7b C |
61 | ngOnInit () { |
62 | this.loadSort() | |
63 | } | |
64 | ||
141b177d C |
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 | ||
1f30a185 | 102 | async removeUser (user: User) { |
28798b5d | 103 | if (user.username === 'root') { |
b1d40cff | 104 | this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.')) |
df98563e | 105 | return |
28798b5d | 106 | } |
7da18e44 | 107 | |
b1d40cff | 108 | const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this user?'), this.i18n('Delete')) |
1f30a185 | 109 | if (res === false) return |
5769e1db | 110 | |
1f30a185 C |
111 | this.userService.removeUser(user).subscribe( |
112 | () => { | |
b1d40cff C |
113 | this.notificationsService.success( |
114 | this.i18n('Success'), | |
25acef90 | 115 | this.i18n('User {{username}} deleted.', { username: user.username }) |
b1d40cff | 116 | ) |
1f30a185 C |
117 | this.loadData() |
118 | }, | |
5769e1db | 119 | |
b1d40cff | 120 | err => this.notificationsService.error(this.i18n('Error'), err.message) |
df98563e | 121 | ) |
7da18e44 | 122 | } |
d592e0a9 C |
123 | |
124 | getRouterUserEditLink (user: User) { | |
9b9b1805 | 125 | return [ '/admin', 'users', 'update', user.id ] |
d592e0a9 C |
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 | ||
b1d40cff | 136 | err => this.notificationsService.error(this.i18n('Error'), err.message) |
d592e0a9 C |
137 | ) |
138 | } | |
7da18e44 | 139 | } |