]>
Commit | Line | Data |
---|---|---|
a6dbbf03 | 1 | import { SortMeta } from 'primeng/api' |
67ed6552 C |
2 | import { Component, OnInit } from '@angular/core' |
3 | import { ComponentPagination, hasMoreItems, Notifier, RestService } from '@app/core' | |
4 | import { InstanceFollowService } from '@app/shared/shared-instance' | |
a6dbbf03 C |
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 | ||
0d7c7314 C |
16 | loadedAllFollowers = false |
17 | loadedAllFollowings = false | |
49c4dd7e | 18 | |
a6dbbf03 C |
19 | followersPagination: ComponentPagination = { |
20 | currentPage: 1, | |
ad453580 | 21 | itemsPerPage: 20, |
ed8b4014 | 22 | totalItems: 0 |
a6dbbf03 C |
23 | } |
24 | ||
25 | followingsPagination: ComponentPagination = { | |
26 | currentPage: 1, | |
ad453580 | 27 | itemsPerPage: 20, |
ed8b4014 | 28 | totalItems: 0 |
a6dbbf03 C |
29 | } |
30 | ||
31 | sort: SortMeta = { | |
32 | field: 'createdAt', | |
33 | order: -1 | |
34 | } | |
35 | ||
36 | constructor ( | |
37 | private restService: RestService, | |
38 | private notifier: Notifier, | |
67ed6552 | 39 | private followService: InstanceFollowService |
a6dbbf03 C |
40 | ) { } |
41 | ||
42 | ngOnInit () { | |
43 | this.loadMoreFollowers() | |
44 | ||
45 | this.loadMoreFollowings() | |
46 | } | |
47 | ||
49c4dd7e | 48 | loadAllFollowings () { |
0d7c7314 C |
49 | if (this.loadedAllFollowings) return |
50 | ||
51 | this.loadedAllFollowings = true | |
52 | this.followingsPagination.itemsPerPage = 100 | |
53 | ||
54 | this.loadMoreFollowings(true) | |
55 | ||
bd49f8e9 | 56 | while (hasMoreItems(this.followingsPagination)) { |
49c4dd7e | 57 | this.followingsPagination.currentPage += 1 |
a6dbbf03 | 58 | |
49c4dd7e M |
59 | this.loadMoreFollowings() |
60 | } | |
a6dbbf03 C |
61 | } |
62 | ||
49c4dd7e | 63 | loadAllFollowers () { |
0d7c7314 C |
64 | if (this.loadedAllFollowers) return |
65 | ||
66 | this.loadedAllFollowers = true | |
67 | this.followersPagination.itemsPerPage = 100 | |
68 | ||
69 | this.loadMoreFollowers(true) | |
70 | ||
bd49f8e9 | 71 | while (hasMoreItems(this.followersPagination)) { |
49c4dd7e | 72 | this.followersPagination.currentPage += 1 |
bd49f8e9 M |
73 | |
74 | this.loadMoreFollowers() | |
49c4dd7e | 75 | } |
a6dbbf03 C |
76 | } |
77 | ||
78 | buildLink (host: string) { | |
79 | return window.location.protocol + '//' + host | |
80 | } | |
81 | ||
0d7c7314 C |
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) { | |
a6dbbf03 C |
91 | const pagination = this.restService.componentPaginationToRestPagination(this.followersPagination) |
92 | ||
b8f4167f | 93 | this.followService.getFollowers({ pagination: pagination, sort: this.sort, state: 'accepted' }) |
a6dbbf03 C |
94 | .subscribe( |
95 | resultList => { | |
0d7c7314 | 96 | if (reset) this.followers = [] |
2ce94945 | 97 | |
0d7c7314 C |
98 | const newFollowers = resultList.data.map(r => r.follower.host) |
99 | this.followers = this.followers.concat(newFollowers) | |
a6dbbf03 C |
100 | |
101 | this.followersPagination.totalItems = resultList.total | |
102 | }, | |
103 | ||
104 | err => this.notifier.error(err.message) | |
105 | ) | |
106 | } | |
107 | ||
0d7c7314 | 108 | private loadMoreFollowings (reset = false) { |
a6dbbf03 C |
109 | const pagination = this.restService.componentPaginationToRestPagination(this.followingsPagination) |
110 | ||
b8f4167f | 111 | this.followService.getFollowing({ pagination, sort: this.sort, state: 'accepted' }) |
a6dbbf03 C |
112 | .subscribe( |
113 | resultList => { | |
0d7c7314 | 114 | if (reset) this.followings = [] |
2ce94945 | 115 | |
0d7c7314 C |
116 | const newFollowings = resultList.data.map(r => r.following.host) |
117 | this.followings = this.followings.concat(newFollowings) | |
a6dbbf03 C |
118 | |
119 | this.followingsPagination.totalItems = resultList.total | |
120 | }, | |
121 | ||
122 | err => this.notifier.error(err.message) | |
123 | ) | |
124 | } | |
125 | ||
126 | } |