]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/users/user-list/user-list.component.ts
Merge branch 'develop' into cli-wrapper
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-list / user-list.component.ts
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 message = this.i18n('If you remove this user, you will not be able to create another with the same username!')
109 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
110 if (res === false) return
111
112 this.userService.removeUser(user).subscribe(
113 () => {
114 this.notificationsService.success(
115 this.i18n('Success'),
116 this.i18n('User {{username}} deleted.', { username: user.username })
117 )
118 this.loadData()
119 },
120
121 err => this.notificationsService.error(this.i18n('Error'), err.message)
122 )
123 }
124
125 getRouterUserEditLink (user: User) {
126 return [ '/admin', 'users', 'update', user.id ]
127 }
128
129 protected loadData () {
130 this.userService.getUsers(this.pagination, this.sort)
131 .subscribe(
132 resultList => {
133 this.users = resultList.data
134 this.totalRecords = resultList.total
135 },
136
137 err => this.notificationsService.error(this.i18n('Error'), err.message)
138 )
139 }
140 }