]>
Commit | Line | Data |
---|---|---|
1 | import { SortMeta } from 'primeng/api' | |
2 | import { Subject } from 'rxjs' | |
3 | import { Component, OnInit } from '@angular/core' | |
4 | import { ComponentPagination, hasMoreItems, Notifier, RestService } from '@app/core' | |
5 | import { InstanceFollowService } from '@app/shared/shared-instance' | |
6 | ||
7 | @Component({ | |
8 | selector: 'my-about-follows', | |
9 | templateUrl: './about-follows.component.html', | |
10 | styleUrls: [ './about-follows.component.scss' ] | |
11 | }) | |
12 | ||
13 | export class AboutFollowsComponent implements OnInit { | |
14 | followers: string[] = [] | |
15 | followings: string[] = [] | |
16 | ||
17 | showMoreFollowers = false; | |
18 | showMoreFollowings = false; | |
19 | ||
20 | followersPagination: ComponentPagination = { | |
21 | currentPage: 1, | |
22 | itemsPerPage: 20, | |
23 | totalItems: null | |
24 | } | |
25 | ||
26 | followingsPagination: ComponentPagination = { | |
27 | currentPage: 1, | |
28 | itemsPerPage: 20, | |
29 | totalItems: null | |
30 | } | |
31 | ||
32 | sort: SortMeta = { | |
33 | field: 'createdAt', | |
34 | order: -1 | |
35 | } | |
36 | ||
37 | onDataSubject = new Subject<any[]>() | |
38 | ||
39 | constructor ( | |
40 | private restService: RestService, | |
41 | private notifier: Notifier, | |
42 | private followService: InstanceFollowService | |
43 | ) { } | |
44 | ||
45 | ngOnInit () { | |
46 | this.loadMoreFollowers() | |
47 | ||
48 | this.loadMoreFollowings() | |
49 | } | |
50 | ||
51 | loadAllFollowings () { | |
52 | while(hasMoreItems(this.followingsPagination)) { | |
53 | this.followingsPagination.currentPage += 1 | |
54 | ||
55 | this.loadMoreFollowings() | |
56 | } | |
57 | } | |
58 | ||
59 | loadAllFollowers () { | |
60 | while(hasMoreItems(this.followersPagination)) { | |
61 | this.followersPagination.currentPage += 1 | |
62 | ||
63 | this.loadMoreFollowers(); | |
64 | } | |
65 | } | |
66 | ||
67 | buildLink (host: string) { | |
68 | return window.location.protocol + '//' + host | |
69 | } | |
70 | ||
71 | private loadMoreFollowers () { | |
72 | const pagination = this.restService.componentPaginationToRestPagination(this.followersPagination) | |
73 | ||
74 | this.followService.getFollowers({ pagination: pagination, sort: this.sort, state: 'accepted' }) | |
75 | .subscribe( | |
76 | resultList => { | |
77 | const newFollowers = resultList.data.map(r => r.follower.host) | |
78 | this.followers = this.followers.concat(newFollowers) | |
79 | ||
80 | this.followersPagination.totalItems = resultList.total | |
81 | ||
82 | this.onDataSubject.next(newFollowers) | |
83 | }, | |
84 | ||
85 | err => this.notifier.error(err.message) | |
86 | ) | |
87 | } | |
88 | ||
89 | private loadMoreFollowings () { | |
90 | const pagination = this.restService.componentPaginationToRestPagination(this.followingsPagination) | |
91 | ||
92 | this.followService.getFollowing({ pagination, sort: this.sort, state: 'accepted' }) | |
93 | .subscribe( | |
94 | resultList => { | |
95 | const newFollowings = resultList.data.map(r => r.following.host) | |
96 | this.followings = this.followings.concat(newFollowings) | |
97 | ||
98 | this.followingsPagination.totalItems = resultList.total | |
99 | ||
100 | this.onDataSubject.next(newFollowings) | |
101 | }, | |
102 | ||
103 | err => this.notifier.error(err.message) | |
104 | ) | |
105 | } | |
106 | ||
107 | } |