]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+about/about-follows/about-follows.component.ts
Reorganize client shared modules
[github/Chocobozzz/PeerTube.git] / client / src / app / +about / about-follows / about-follows.component.ts
CommitLineData
a6dbbf03 1import { SortMeta } from 'primeng/api'
ad453580 2import { Subject } from 'rxjs'
67ed6552
C
3import { Component, OnInit } from '@angular/core'
4import { ComponentPagination, hasMoreItems, Notifier, RestService } from '@app/core'
5import { InstanceFollowService } from '@app/shared/shared-instance'
a6dbbf03
C
6
7@Component({
8 selector: 'my-about-follows',
9 templateUrl: './about-follows.component.html',
10 styleUrls: [ './about-follows.component.scss' ]
11})
12
13export class AboutFollowsComponent implements OnInit {
14 followers: string[] = []
15 followings: string[] = []
16
17 followersPagination: ComponentPagination = {
18 currentPage: 1,
ad453580 19 itemsPerPage: 20,
a6dbbf03
C
20 totalItems: null
21 }
22
23 followingsPagination: ComponentPagination = {
24 currentPage: 1,
ad453580 25 itemsPerPage: 20,
a6dbbf03
C
26 totalItems: null
27 }
28
29 sort: SortMeta = {
30 field: 'createdAt',
31 order: -1
32 }
33
ad453580
C
34 onDataSubject = new Subject<any[]>()
35
a6dbbf03
C
36 constructor (
37 private restService: RestService,
38 private notifier: Notifier,
67ed6552 39 private followService: InstanceFollowService
a6dbbf03
C
40 ) { }
41
42 ngOnInit () {
43 this.loadMoreFollowers()
44
45 this.loadMoreFollowings()
46 }
47
48 onNearOfBottom () {
49 this.onNearOfFollowersBottom()
50
51 this.onNearOfFollowingsBottom()
52 }
53
54 onNearOfFollowersBottom () {
55 if (!hasMoreItems(this.followersPagination)) return
56
57 this.followersPagination.currentPage += 1
58 this.loadMoreFollowers()
59 }
60
61 onNearOfFollowingsBottom () {
62 if (!hasMoreItems(this.followingsPagination)) return
63
64 this.followingsPagination.currentPage += 1
65 this.loadMoreFollowings()
66 }
67
68 buildLink (host: string) {
69 return window.location.protocol + '//' + host
70 }
71
72 private loadMoreFollowers () {
73 const pagination = this.restService.componentPaginationToRestPagination(this.followersPagination)
74
b8f4167f 75 this.followService.getFollowers({ pagination: pagination, sort: this.sort, state: 'accepted' })
a6dbbf03
C
76 .subscribe(
77 resultList => {
78 const newFollowers = resultList.data.map(r => r.follower.host)
79 this.followers = this.followers.concat(newFollowers)
80
81 this.followersPagination.totalItems = resultList.total
ad453580
C
82
83 this.onDataSubject.next(newFollowers)
a6dbbf03
C
84 },
85
86 err => this.notifier.error(err.message)
87 )
88 }
89
90 private loadMoreFollowings () {
91 const pagination = this.restService.componentPaginationToRestPagination(this.followingsPagination)
92
b8f4167f 93 this.followService.getFollowing({ pagination, sort: this.sort, state: 'accepted' })
a6dbbf03
C
94 .subscribe(
95 resultList => {
96 const newFollowings = resultList.data.map(r => r.following.host)
97 this.followings = this.followings.concat(newFollowings)
98
99 this.followingsPagination.totalItems = resultList.total
ad453580
C
100
101 this.onDataSubject.next(newFollowings)
a6dbbf03
C
102 },
103
104 err => this.notifier.error(err.message)
105 )
106 }
107
108}