]>
Commit | Line | Data |
---|---|---|
4beda9e1 C |
1 | import { Subject } from 'rxjs' |
2 | import { Component, OnInit } from '@angular/core' | |
3 | import { ActivatedRoute } from '@angular/router' | |
4 | import { AuthService, ComponentPagination, Notifier } from '@app/core' | |
5 | import { UserSubscriptionService } from '@app/shared/shared-user-subscription' | |
6 | import { ActorFollow } from '@shared/models' | |
7 | ||
8 | @Component({ | |
9 | templateUrl: './my-followers.component.html', | |
10 | styleUrls: [ './my-followers.component.scss' ] | |
11 | }) | |
12 | export class MyFollowersComponent implements OnInit { | |
13 | follows: ActorFollow[] = [] | |
14 | ||
15 | pagination: ComponentPagination = { | |
16 | currentPage: 1, | |
17 | itemsPerPage: 10, | |
18 | totalItems: null | |
19 | } | |
20 | ||
21 | onDataSubject = new Subject<any[]>() | |
22 | search: string | |
23 | ||
24 | constructor ( | |
25 | private route: ActivatedRoute, | |
26 | private auth: AuthService, | |
27 | private userSubscriptionService: UserSubscriptionService, | |
28 | private notifier: Notifier | |
29 | ) {} | |
30 | ||
31 | ngOnInit () { | |
32 | if (this.route.snapshot.queryParams['search']) { | |
33 | this.search = this.route.snapshot.queryParams['search'] | |
34 | } | |
35 | } | |
36 | ||
37 | onNearOfBottom () { | |
38 | // Last page | |
39 | if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return | |
40 | ||
41 | this.pagination.currentPage += 1 | |
42 | this.loadFollowers() | |
43 | } | |
44 | ||
45 | onSearch (search: string) { | |
46 | this.search = search | |
47 | this.loadFollowers(false) | |
48 | } | |
49 | ||
50 | isFollowingAccount (follow: ActorFollow) { | |
51 | return follow.following.name === this.getUsername() | |
52 | } | |
53 | ||
54 | private loadFollowers (more = true) { | |
55 | this.userSubscriptionService.listFollowers({ | |
56 | pagination: this.pagination, | |
57 | nameWithHost: this.getUsername(), | |
58 | search: this.search | |
59 | }).subscribe({ | |
60 | next: res => { | |
61 | this.follows = more | |
62 | ? this.follows.concat(res.data) | |
63 | : res.data | |
64 | this.pagination.totalItems = res.total | |
65 | ||
66 | this.onDataSubject.next(res.data) | |
67 | }, | |
68 | ||
69 | error: err => this.notifier.error(err.message) | |
70 | }) | |
71 | } | |
72 | ||
73 | private getUsername () { | |
74 | return this.auth.getUser().username | |
75 | } | |
76 | } |