Merge branch 'release/v1.3.0' 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 { Notifier } from '@app/core'
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 '@app/shared/instance/follow.service'
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 notifier: Notifier,
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.notifier.success(this.i18n('You are not following {{host}} anymore.', { host: follow.following.host }))
45         this.loadData()
46       },
47
48       err => this.notifier.error(err.message)
49     )
50   }
51
52   protected loadData () {
53     this.followService.getFollowing(this.pagination, this.sort, this.search)
54                       .subscribe(
55                         resultList => {
56                           this.following = resultList.data
57                           this.totalRecords = resultList.total
58                         },
59
60                         err => this.notifier.error(err.message)
61                       )
62   }
63 }