Unify paginator disabling when no result is displayable, fix batch domain add for...
[oweals/peertube.git] / client / src / app / +admin / follows / following-list / following-list.component.ts
1 import { Component, OnInit, ViewChild } from '@angular/core'
2 import { Notifier } from '@app/core'
3 import { SortMeta } from 'primeng/api'
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 import { BatchDomainsModalComponent } from '@app/+admin/config/shared/batch-domains-modal.component'
10
11 @Component({
12   selector: 'my-followers-list',
13   templateUrl: './following-list.component.html',
14   styleUrls: [ '../follows.component.scss', './following-list.component.scss' ]
15 })
16 export class FollowingListComponent extends RestTable implements OnInit {
17   @ViewChild('batchDomainsModal') batchDomainsModal: BatchDomainsModalComponent
18
19   following: ActorFollow[] = []
20   totalRecords = 0
21   rowsPerPageOptions = [ 20, 50, 100 ]
22   rowsPerPage = this.rowsPerPageOptions[0]
23   sort: SortMeta = { field: 'createdAt', order: -1 }
24   pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
25
26   constructor (
27     private notifier: Notifier,
28     private confirmService: ConfirmService,
29     private followService: FollowService,
30     private i18n: I18n
31   ) {
32     super()
33   }
34
35   ngOnInit () {
36     this.initialize()
37   }
38
39   getIdentifier () {
40     return 'FollowingListComponent'
41   }
42
43   addDomainsToFollow () {
44     this.batchDomainsModal.openModal()
45   }
46
47   httpEnabled () {
48     return window.location.protocol === 'https:'
49   }
50
51   async addFollowing (hosts: string[]) {
52     this.followService.follow(hosts).subscribe(
53       () => {
54         this.notifier.success(this.i18n('Follow request(s) sent!'))
55         this.loadData()
56       },
57
58       err => this.notifier.error(err.message)
59     )
60   }
61
62   async removeFollowing (follow: ActorFollow) {
63     const res = await this.confirmService.confirm(
64       this.i18n('Do you really want to unfollow {{host}}?', { host: follow.following.host }),
65       this.i18n('Unfollow')
66     )
67     if (res === false) return
68
69     this.followService.unfollow(follow).subscribe(
70       () => {
71         this.notifier.success(this.i18n('You are not following {{host}} anymore.', { host: follow.following.host }))
72         this.loadData()
73       },
74
75       err => this.notifier.error(err.message)
76     )
77   }
78
79   protected loadData () {
80     this.followService.getFollowing({ pagination: this.pagination, sort: this.sort, search: this.search })
81                       .subscribe(
82                         resultList => {
83                           this.following = resultList.data
84                           this.totalRecords = resultList.total
85                         },
86
87                         err => this.notifier.error(err.message)
88                       )
89   }
90 }