]>
Commit | Line | Data |
---|---|---|
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' | |
5 | ||
6 | @Component({ | |
7 | selector: 'my-about-follows', | |
8 | templateUrl: './about-follows.component.html', | |
9 | styleUrls: [ './about-follows.component.scss' ] | |
10 | }) | |
11 | ||
12 | export class AboutFollowsComponent implements OnInit { | |
13 | followers: string[] = [] | |
14 | followings: string[] = [] | |
15 | ||
16 | loadedAllFollowers = false | |
17 | loadedAllFollowings = false | |
18 | ||
19 | followersPagination: ComponentPagination = { | |
20 | currentPage: 1, | |
21 | itemsPerPage: 20, | |
22 | totalItems: 0 | |
23 | } | |
24 | ||
25 | followingsPagination: ComponentPagination = { | |
26 | currentPage: 1, | |
27 | itemsPerPage: 20, | |
28 | totalItems: 0 | |
29 | } | |
30 | ||
31 | sort: SortMeta = { | |
32 | field: 'createdAt', | |
33 | order: -1 | |
34 | } | |
35 | ||
36 | constructor ( | |
37 | private restService: RestService, | |
38 | private notifier: Notifier, | |
39 | private followService: InstanceFollowService | |
40 | ) { } | |
41 | ||
42 | ngOnInit () { | |
43 | this.loadMoreFollowers() | |
44 | ||
45 | this.loadMoreFollowings() | |
46 | } | |
47 | ||
48 | loadAllFollowings () { | |
49 | if (this.loadedAllFollowings) return | |
50 | ||
51 | this.loadedAllFollowings = true | |
52 | this.followingsPagination.itemsPerPage = 100 | |
53 | ||
54 | this.loadMoreFollowings(true) | |
55 | ||
56 | while (hasMoreItems(this.followingsPagination)) { | |
57 | this.followingsPagination.currentPage += 1 | |
58 | ||
59 | this.loadMoreFollowings() | |
60 | } | |
61 | } | |
62 | ||
63 | loadAllFollowers () { | |
64 | if (this.loadedAllFollowers) return | |
65 | ||
66 | this.loadedAllFollowers = true | |
67 | this.followersPagination.itemsPerPage = 100 | |
68 | ||
69 | this.loadMoreFollowers(true) | |
70 | ||
71 | while (hasMoreItems(this.followersPagination)) { | |
72 | this.followersPagination.currentPage += 1 | |
73 | ||
74 | this.loadMoreFollowers() | |
75 | } | |
76 | } | |
77 | ||
78 | buildLink (host: string) { | |
79 | return window.location.protocol + '//' + host | |
80 | } | |
81 | ||
82 | canLoadMoreFollowers () { | |
83 | return this.loadedAllFollowers || this.followersPagination.totalItems > this.followersPagination.itemsPerPage | |
84 | } | |
85 | ||
86 | canLoadMoreFollowings () { | |
87 | return this.loadedAllFollowings || this.followingsPagination.totalItems > this.followingsPagination.itemsPerPage | |
88 | } | |
89 | ||
90 | private loadMoreFollowers (reset = false) { | |
91 | const pagination = this.restService.componentToRestPagination(this.followersPagination) | |
92 | ||
93 | this.followService.getFollowers({ pagination, sort: this.sort, state: 'accepted' }) | |
94 | .subscribe({ | |
95 | next: resultList => { | |
96 | if (reset) this.followers = [] | |
97 | ||
98 | const newFollowers = resultList.data.map(r => r.follower.host) | |
99 | this.followers = this.followers.concat(newFollowers) | |
100 | ||
101 | this.followersPagination.totalItems = resultList.total | |
102 | }, | |
103 | ||
104 | error: err => this.notifier.error(err.message) | |
105 | }) | |
106 | } | |
107 | ||
108 | private loadMoreFollowings (reset = false) { | |
109 | const pagination = this.restService.componentToRestPagination(this.followingsPagination) | |
110 | ||
111 | this.followService.getFollowing({ pagination, sort: this.sort, state: 'accepted' }) | |
112 | .subscribe({ | |
113 | next: resultList => { | |
114 | if (reset) this.followings = [] | |
115 | ||
116 | const newFollowings = resultList.data.map(r => r.following.host) | |
117 | this.followings = this.followings.concat(newFollowings) | |
118 | ||
119 | this.followingsPagination.totalItems = resultList.total | |
120 | }, | |
121 | ||
122 | error: err => this.notifier.error(err.message) | |
123 | }) | |
124 | } | |
125 | ||
126 | } |