2019-06-06 11:39:22 +02:00
|
|
|
import { SortMeta } from 'primeng/api'
|
2019-08-02 14:49:25 +02:00
|
|
|
import { Subject } from 'rxjs'
|
2020-06-23 14:10:17 +02:00
|
|
|
import { Component, OnInit } from '@angular/core'
|
|
|
|
import { ComponentPagination, hasMoreItems, Notifier, RestService } from '@app/core'
|
|
|
|
import { InstanceFollowService } from '@app/shared/shared-instance'
|
2019-06-06 11:39:22 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-about-follows',
|
|
|
|
templateUrl: './about-follows.component.html',
|
|
|
|
styleUrls: [ './about-follows.component.scss' ]
|
|
|
|
})
|
|
|
|
|
|
|
|
export class AboutFollowsComponent implements OnInit {
|
|
|
|
followers: string[] = []
|
|
|
|
followings: string[] = []
|
2020-04-27 21:14:40 +02:00
|
|
|
moreFollowers: string[] = []
|
|
|
|
moreFollowings: string[] = []
|
2019-06-06 11:39:22 +02:00
|
|
|
|
2020-04-27 13:37:18 +02:00
|
|
|
showMoreFollowers = false
|
|
|
|
showMoreFollowings = false
|
2020-04-27 12:18:24 +02:00
|
|
|
|
2019-06-06 11:39:22 +02:00
|
|
|
followersPagination: ComponentPagination = {
|
|
|
|
currentPage: 1,
|
2019-08-02 14:49:25 +02:00
|
|
|
itemsPerPage: 20,
|
2019-06-06 11:39:22 +02:00
|
|
|
totalItems: null
|
|
|
|
}
|
|
|
|
|
|
|
|
followingsPagination: ComponentPagination = {
|
|
|
|
currentPage: 1,
|
2019-08-02 14:49:25 +02:00
|
|
|
itemsPerPage: 20,
|
2019-06-06 11:39:22 +02:00
|
|
|
totalItems: null
|
|
|
|
}
|
|
|
|
|
|
|
|
sort: SortMeta = {
|
|
|
|
field: 'createdAt',
|
|
|
|
order: -1
|
|
|
|
}
|
|
|
|
|
2019-08-02 14:49:25 +02:00
|
|
|
onDataSubject = new Subject<any[]>()
|
|
|
|
|
2019-06-06 11:39:22 +02:00
|
|
|
constructor (
|
|
|
|
private restService: RestService,
|
|
|
|
private notifier: Notifier,
|
2020-06-23 14:10:17 +02:00
|
|
|
private followService: InstanceFollowService
|
2019-06-06 11:39:22 +02:00
|
|
|
) { }
|
|
|
|
|
|
|
|
ngOnInit () {
|
|
|
|
this.loadMoreFollowers()
|
|
|
|
|
|
|
|
this.loadMoreFollowings()
|
|
|
|
}
|
|
|
|
|
2020-04-27 12:18:24 +02:00
|
|
|
loadAllFollowings () {
|
2020-04-27 13:37:18 +02:00
|
|
|
while (hasMoreItems(this.followingsPagination)) {
|
2020-04-27 12:18:24 +02:00
|
|
|
this.followingsPagination.currentPage += 1
|
2019-06-06 11:39:22 +02:00
|
|
|
|
2020-04-27 12:18:24 +02:00
|
|
|
this.loadMoreFollowings()
|
|
|
|
}
|
2019-06-06 11:39:22 +02:00
|
|
|
}
|
|
|
|
|
2020-04-27 12:18:24 +02:00
|
|
|
loadAllFollowers () {
|
2020-04-27 13:37:18 +02:00
|
|
|
while (hasMoreItems(this.followersPagination)) {
|
2020-04-27 12:18:24 +02:00
|
|
|
this.followersPagination.currentPage += 1
|
2020-04-27 13:37:18 +02:00
|
|
|
|
|
|
|
this.loadMoreFollowers()
|
2020-04-27 12:18:24 +02:00
|
|
|
}
|
2019-06-06 11:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
buildLink (host: string) {
|
|
|
|
return window.location.protocol + '//' + host
|
|
|
|
}
|
|
|
|
|
|
|
|
private loadMoreFollowers () {
|
|
|
|
const pagination = this.restService.componentPaginationToRestPagination(this.followersPagination)
|
|
|
|
|
2019-11-28 11:37:32 +01:00
|
|
|
this.followService.getFollowers({ pagination: pagination, sort: this.sort, state: 'accepted' })
|
2019-06-06 11:39:22 +02:00
|
|
|
.subscribe(
|
|
|
|
resultList => {
|
|
|
|
const newFollowers = resultList.data.map(r => r.follower.host)
|
2020-04-27 21:14:40 +02:00
|
|
|
if (this.followers.length === 0) this.followers = this.followers.concat(newFollowers)
|
|
|
|
|
|
|
|
else this.moreFollowers = this.moreFollowers.concat(newFollowers)
|
2019-06-06 11:39:22 +02:00
|
|
|
|
|
|
|
this.followersPagination.totalItems = resultList.total
|
2019-08-02 14:49:25 +02:00
|
|
|
|
|
|
|
this.onDataSubject.next(newFollowers)
|
2019-06-06 11:39:22 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
err => this.notifier.error(err.message)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
private loadMoreFollowings () {
|
|
|
|
const pagination = this.restService.componentPaginationToRestPagination(this.followingsPagination)
|
|
|
|
|
2019-11-28 11:37:32 +01:00
|
|
|
this.followService.getFollowing({ pagination, sort: this.sort, state: 'accepted' })
|
2019-06-06 11:39:22 +02:00
|
|
|
.subscribe(
|
|
|
|
resultList => {
|
|
|
|
const newFollowings = resultList.data.map(r => r.following.host)
|
2020-04-27 21:37:56 +02:00
|
|
|
if (this.followings.length === 0) this.followings = this.followings.concat(newFollowings)
|
2020-04-27 21:14:40 +02:00
|
|
|
|
|
|
|
else this.moreFollowings = this.moreFollowings.concat(newFollowings)
|
2019-06-06 11:39:22 +02:00
|
|
|
|
|
|
|
this.followingsPagination.totalItems = resultList.total
|
2019-08-02 14:49:25 +02:00
|
|
|
|
|
|
|
this.onDataSubject.next(newFollowings)
|
2019-06-06 11:39:22 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
err => this.notifier.error(err.message)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|