]>
Commit | Line | Data |
---|---|---|
a6dbbf03 C |
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' | |
ad453580 | 7 | import { Subject } from 'rxjs' |
a6dbbf03 C |
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, | |
ad453580 | 21 | itemsPerPage: 20, |
a6dbbf03 C |
22 | totalItems: null |
23 | } | |
24 | ||
25 | followingsPagination: ComponentPagination = { | |
26 | currentPage: 1, | |
ad453580 | 27 | itemsPerPage: 20, |
a6dbbf03 C |
28 | totalItems: null |
29 | } | |
30 | ||
31 | sort: SortMeta = { | |
32 | field: 'createdAt', | |
33 | order: -1 | |
34 | } | |
35 | ||
ad453580 C |
36 | onDataSubject = new Subject<any[]>() |
37 | ||
a6dbbf03 C |
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 | ||
b8f4167f | 77 | this.followService.getFollowers({ pagination: pagination, sort: this.sort, state: 'accepted' }) |
a6dbbf03 C |
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 | |
ad453580 C |
84 | |
85 | this.onDataSubject.next(newFollowers) | |
a6dbbf03 C |
86 | }, |
87 | ||
88 | err => this.notifier.error(err.message) | |
89 | ) | |
90 | } | |
91 | ||
92 | private loadMoreFollowings () { | |
93 | const pagination = this.restService.componentPaginationToRestPagination(this.followingsPagination) | |
94 | ||
b8f4167f | 95 | this.followService.getFollowing({ pagination, sort: this.sort, state: 'accepted' }) |
a6dbbf03 C |
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 | |
ad453580 C |
102 | |
103 | this.onDataSubject.next(newFollowings) | |
a6dbbf03 C |
104 | }, |
105 | ||
106 | err => this.notifier.error(err.message) | |
107 | ) | |
108 | } | |
109 | ||
110 | } |