1 import { SortMeta } from 'primeng/api'
2 import { Component, OnInit } from '@angular/core'
3 import { ComponentPagination, hasMoreItems, Notifier, RestService } from '@app/core'
4 import { InstanceFollowService } from '@app/shared/shared-instance'
7 selector: 'my-about-follows',
8 templateUrl: './about-follows.component.html',
9 styleUrls: [ './about-follows.component.scss' ]
12 export class AboutFollowsComponent implements OnInit {
13 followers: string[] = []
14 followings: string[] = []
16 loadedAllFollowers = false
17 loadedAllFollowings = false
19 followersPagination: ComponentPagination = {
25 followingsPagination: ComponentPagination = {
37 private restService: RestService,
38 private notifier: Notifier,
39 private followService: InstanceFollowService
43 this.loadMoreFollowers()
45 this.loadMoreFollowings()
48 loadAllFollowings () {
49 if (this.loadedAllFollowings) return
51 this.loadedAllFollowings = true
52 this.followingsPagination.itemsPerPage = 100
54 this.loadMoreFollowings(true)
56 while (hasMoreItems(this.followingsPagination)) {
57 this.followingsPagination.currentPage += 1
59 this.loadMoreFollowings()
64 if (this.loadedAllFollowers) return
66 this.loadedAllFollowers = true
67 this.followersPagination.itemsPerPage = 100
69 this.loadMoreFollowers(true)
71 while (hasMoreItems(this.followersPagination)) {
72 this.followersPagination.currentPage += 1
74 this.loadMoreFollowers()
78 buildLink (host: string) {
79 return window.location.protocol + '//' + host
82 canLoadMoreFollowers () {
83 return this.loadedAllFollowers || this.followersPagination.totalItems > this.followersPagination.itemsPerPage
86 canLoadMoreFollowings () {
87 return this.loadedAllFollowings || this.followingsPagination.totalItems > this.followingsPagination.itemsPerPage
90 private loadMoreFollowers (reset = false) {
91 const pagination = this.restService.componentToRestPagination(this.followersPagination)
93 this.followService.getFollowers({ pagination, sort: this.sort, state: 'accepted' })
96 if (reset) this.followers = []
98 const newFollowers = resultList.data.map(r => r.follower.host)
99 this.followers = this.followers.concat(newFollowers)
101 this.followersPagination.totalItems = resultList.total
104 error: err => this.notifier.error(err.message)
108 private loadMoreFollowings (reset = false) {
109 const pagination = this.restService.componentToRestPagination(this.followingsPagination)
111 this.followService.getFollowing({ pagination, sort: this.sort, state: 'accepted' })
113 next: resultList => {
114 if (reset) this.followings = []
116 const newFollowings = resultList.data.map(r => r.following.host)
117 this.followings = this.followings.concat(newFollowings)
119 this.followingsPagination.totalItems = resultList.total
122 error: err => this.notifier.error(err.message)