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