]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/users/user-list/user-list.component.ts
Add quota used in users list
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-list / user-list.component.ts
CommitLineData
141b177d 1import { Component, OnInit, ViewChild } from '@angular/core'
df98563e 2import { NotificationsService } from 'angular2-notifications'
1f30a185 3import { SortMeta } from 'primeng/components/common/sortmeta'
df98563e 4import { ConfirmService } from '../../../core'
1f30a185 5import { RestPagination, RestTable, User } from '../../../shared'
df98563e 6import { UserService } from '../shared'
b1d40cff 7import { I18n } from '@ngx-translate/i18n-polyfill'
eacb25c4 8import { DropdownAction } from '@app/shared/buttons/action-dropdown.component'
141b177d
C
9import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
10import { UserBanModalComponent } from '@app/+admin/users/user-list/user-ban-modal.component'
7da18e44
C
11
12@Component({
13 selector: 'my-user-list',
ec8d8440
C
14 templateUrl: './user-list.component.html',
15 styleUrls: [ './user-list.component.scss' ]
7da18e44 16})
ab998f7b 17export class UserListComponent extends RestTable implements OnInit {
141b177d
C
18 @ViewChild('userBanModal') userBanModal: UserBanModalComponent
19
d592e0a9
C
20 users: User[] = []
21 totalRecords = 0
22 rowsPerPage = 10
ab998f7b 23 sort: SortMeta = { field: 'createdAt', order: 1 }
d592e0a9 24 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
eacb25c4 25 userActions: DropdownAction<User>[] = []
7da18e44 26
141b177d
C
27 private userToBan: User
28 private openedModal: NgbModalRef
29
df98563e 30 constructor (
7ddd02c9 31 private notificationsService: NotificationsService,
5769e1db 32 private confirmService: ConfirmService,
b1d40cff
C
33 private userService: UserService,
34 private i18n: I18n
28798b5d 35 ) {
d592e0a9 36 super()
eacb25c4
C
37
38 this.userActions = [
39 {
141b177d 40 label: this.i18n('Edit'),
eacb25c4
C
41 linkBuilder: this.getRouterUserEditLink
42 },
43 {
141b177d 44 label: this.i18n('Delete'),
eacb25c4 45 handler: user => this.removeUser(user)
141b177d
C
46 },
47 {
48 label: this.i18n('Ban'),
49 handler: user => this.openBanUserModal(user),
50 isDisplayed: user => !user.blocked
51 },
52 {
53 label: this.i18n('Unban'),
54 handler: user => this.unbanUser(user),
55 isDisplayed: user => user.blocked
eacb25c4
C
56 }
57 ]
7da18e44
C
58 }
59
ab998f7b
C
60 ngOnInit () {
61 this.loadSort()
62 }
63
141b177d
C
64 hideBanUserModal () {
65 this.userToBan = undefined
66 this.openedModal.close()
67 }
68
69 openBanUserModal (user: User) {
70 if (user.username === 'root') {
71 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.'))
72 return
73 }
74
75 this.userBanModal.openModal(user)
76 }
77
78 onUserBanned () {
79 this.loadData()
80 }
81
82 async unbanUser (user: User) {
83 const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username })
84 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
85 if (res === false) return
86
87 this.userService.unbanUser(user)
88 .subscribe(
89 () => {
90 this.notificationsService.success(
91 this.i18n('Success'),
92 this.i18n('User {{username}} unbanned.', { username: user.username })
93 )
94 this.loadData()
95 },
96
97 err => this.notificationsService.error(this.i18n('Error'), err.message)
98 )
99 }
100
1f30a185 101 async removeUser (user: User) {
28798b5d 102 if (user.username === 'root') {
b1d40cff 103 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.'))
df98563e 104 return
28798b5d 105 }
7da18e44 106
b1d40cff 107 const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this user?'), this.i18n('Delete'))
1f30a185 108 if (res === false) return
5769e1db 109
1f30a185
C
110 this.userService.removeUser(user).subscribe(
111 () => {
b1d40cff
C
112 this.notificationsService.success(
113 this.i18n('Success'),
25acef90 114 this.i18n('User {{username}} deleted.', { username: user.username })
b1d40cff 115 )
1f30a185
C
116 this.loadData()
117 },
5769e1db 118
b1d40cff 119 err => this.notificationsService.error(this.i18n('Error'), err.message)
df98563e 120 )
7da18e44 121 }
d592e0a9
C
122
123 getRouterUserEditLink (user: User) {
9b9b1805 124 return [ '/admin', 'users', 'update', user.id ]
d592e0a9
C
125 }
126
127 protected loadData () {
128 this.userService.getUsers(this.pagination, this.sort)
129 .subscribe(
130 resultList => {
131 this.users = resultList.data
132 this.totalRecords = resultList.total
133 },
134
b1d40cff 135 err => this.notificationsService.error(this.i18n('Error'), err.message)
d592e0a9
C
136 )
137 }
7da18e44 138}