Merge branch 'master' into develop
[oweals/peertube.git] / client / src / app / +admin / follows / following-list / following-list.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { NotificationsService } from 'angular2-notifications'
3 import { SortMeta } from 'primeng/primeng'
4 import { ActorFollow } from '../../../../../../shared/models/actors/follow.model'
5 import { ConfirmService } from '../../../core/confirm/confirm.service'
6 import { RestPagination, RestTable } from '../../../shared'
7 import { FollowService } from '../shared'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9
10 @Component({
11   selector: 'my-followers-list',
12   templateUrl: './following-list.component.html',
13   styleUrls: [ './following-list.component.scss' ]
14 })
15 export class FollowingListComponent extends RestTable implements OnInit {
16   following: ActorFollow[] = []
17   totalRecords = 0
18   rowsPerPage = 10
19   sort: SortMeta = { field: 'createdAt', order: 1 }
20   pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
21
22   constructor (
23     private notificationsService: NotificationsService,
24     private confirmService: ConfirmService,
25     private followService: FollowService,
26     private i18n: I18n
27   ) {
28     super()
29   }
30
31   ngOnInit () {
32     this.initialize()
33   }
34
35   async removeFollowing (follow: ActorFollow) {
36     const res = await this.confirmService.confirm(
37       this.i18n('Do you really want to unfollow {{host}}?', { host: follow.following.host }),
38       this.i18n('Unfollow')
39     )
40     if (res === false) return
41
42     this.followService.unfollow(follow).subscribe(
43       () => {
44         this.notificationsService.success(
45           this.i18n('Success'),
46           this.i18n('You are not following {{host}} anymore.', { host: follow.following.host })
47         )
48         this.loadData()
49       },
50
51       err => this.notificationsService.error(this.i18n('Error'), err.message)
52     )
53   }
54
55   protected loadData () {
56     this.followService.getFollowing(this.pagination, this.sort, this.search)
57                       .subscribe(
58                         resultList => {
59                           this.following = resultList.data
60                           this.totalRecords = resultList.total
61                         },
62
63                         err => this.notificationsService.error(this.i18n('Error'), err.message)
64                       )
65   }
66 }