Move to HttpClient and PrimeNG data table
[oweals/peertube.git] / client / src / app / +admin / users / user-list / user-list.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { SortMeta } from 'primeng/primeng'
3
4 import { NotificationsService } from 'angular2-notifications'
5
6 import { ConfirmService } from '../../../core'
7 import { RestTable, RestPagination, User } from '../../../shared'
8 import { UserService } from '../shared'
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: 'id', order: 1 }
20   pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
21
22   constructor (
23     private notificationsService: NotificationsService,
24     private confirmService: ConfirmService,
25     private userService: UserService
26   ) {
27     super()
28   }
29
30   ngOnInit () {
31     this.loadData()
32   }
33
34   removeUser (user: User) {
35     if (user.username === 'root') {
36       this.notificationsService.error('Error', 'You cannot delete root.')
37       return
38     }
39
40     this.confirmService.confirm('Do you really want to delete this user?', 'Delete').subscribe(
41       res => {
42         if (res === false) return
43
44         this.userService.removeUser(user).subscribe(
45           () => {
46             this.notificationsService.success('Success', `User ${user.username} deleted.`)
47             this.loadData()
48           },
49
50           err => this.notificationsService.error('Error', err)
51         )
52       }
53     )
54   }
55
56   getRouterUserEditLink (user: User) {
57     return [ '/admin', 'users', user.id, 'update' ]
58   }
59
60   protected loadData () {
61     this.userService.getUsers(this.pagination, this.sort)
62                     .subscribe(
63                       resultList => {
64                         this.users = resultList.data
65                         this.totalRecords = resultList.total
66                       },
67
68                       err => this.notificationsService.error('Error', err)
69                     )
70   }
71 }