]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/users/user-list/user-list.component.ts
Add reason when banning a user
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-list / user-list.component.ts
1 import { Component, OnInit } 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, User } 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
10 @Component({
11 selector: 'my-user-list',
12 templateUrl: './user-list.component.html',
13 styleUrls: [ './user-list.component.scss' ]
14 })
15 export class UserListComponent extends RestTable implements OnInit {
16 users: User[] = []
17 totalRecords = 0
18 rowsPerPage = 10
19 sort: SortMeta = { field: 'createdAt', order: 1 }
20 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
21 userActions: DropdownAction<User>[] = []
22
23 constructor (
24 private notificationsService: NotificationsService,
25 private confirmService: ConfirmService,
26 private userService: UserService,
27 private i18n: I18n
28 ) {
29 super()
30
31 this.userActions = [
32 {
33 type: 'edit',
34 linkBuilder: this.getRouterUserEditLink
35 },
36 {
37 type: 'delete',
38 handler: user => this.removeUser(user)
39 }
40 ]
41 }
42
43 ngOnInit () {
44 this.loadSort()
45 }
46
47 async removeUser (user: User) {
48 if (user.username === 'root') {
49 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.'))
50 return
51 }
52
53 const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this user?'), this.i18n('Delete'))
54 if (res === false) return
55
56 this.userService.removeUser(user).subscribe(
57 () => {
58 this.notificationsService.success(
59 this.i18n('Success'),
60 this.i18n('User {{username}} deleted.', { username: user.username })
61 )
62 this.loadData()
63 },
64
65 err => this.notificationsService.error(this.i18n('Error'), err.message)
66 )
67 }
68
69 getRouterUserEditLink (user: User) {
70 return [ '/admin', 'users', 'update', user.id ]
71 }
72
73 protected loadData () {
74 this.userService.getUsers(this.pagination, this.sort)
75 .subscribe(
76 resultList => {
77 this.users = resultList.data
78 this.totalRecords = resultList.total
79 },
80
81 err => this.notificationsService.error(this.i18n('Error'), err.message)
82 )
83 }
84 }