Reorganize client shared modules
[oweals/peertube.git] / client / src / app / +admin / follows / following-list / following-list.component.ts
1 import { SortMeta } from 'primeng/api'
2 import { Component, OnInit, ViewChild } from '@angular/core'
3 import { ConfirmService, Notifier, RestPagination, RestTable } from '@app/core'
4 import { InstanceFollowService } from '@app/shared/shared-instance'
5 import { BatchDomainsModalComponent } from '@app/shared/shared-moderation'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { ActorFollow } from '@shared/models'
8
9 @Component({
10   selector: 'my-followers-list',
11   templateUrl: './following-list.component.html',
12   styleUrls: [ '../follows.component.scss', './following-list.component.scss' ]
13 })
14 export class FollowingListComponent extends RestTable implements OnInit {
15   @ViewChild('batchDomainsModal') batchDomainsModal: BatchDomainsModalComponent
16
17   following: ActorFollow[] = []
18   totalRecords = 0
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: InstanceFollowService,
26     private i18n: I18n
27   ) {
28     super()
29   }
30
31   ngOnInit () {
32     this.initialize()
33   }
34
35   getIdentifier () {
36     return 'FollowingListComponent'
37   }
38
39   addDomainsToFollow () {
40     this.batchDomainsModal.openModal()
41   }
42
43   httpEnabled () {
44     return window.location.protocol === 'https:'
45   }
46
47   async addFollowing (hosts: string[]) {
48     this.followService.follow(hosts).subscribe(
49       () => {
50         this.notifier.success(this.i18n('Follow request(s) sent!'))
51         this.loadData()
52       },
53
54       err => this.notifier.error(err.message)
55     )
56   }
57
58   async removeFollowing (follow: ActorFollow) {
59     const res = await this.confirmService.confirm(
60       this.i18n('Do you really want to unfollow {{host}}?', { host: follow.following.host }),
61       this.i18n('Unfollow')
62     )
63     if (res === false) return
64
65     this.followService.unfollow(follow).subscribe(
66       () => {
67         this.notifier.success(this.i18n('You are not following {{host}} anymore.', { host: follow.following.host }))
68         this.loadData()
69       },
70
71       err => this.notifier.error(err.message)
72     )
73   }
74
75   protected loadData () {
76     this.followService.getFollowing({ pagination: this.pagination, sort: this.sort, search: this.search })
77                       .subscribe(
78                         resultList => {
79                           this.following = resultList.data
80                           this.totalRecords = resultList.total
81                         },
82
83                         err => this.notifier.error(err.message)
84                       )
85   }
86 }