]>
Commit | Line | Data |
---|---|---|
1 | import { Subject } from 'rxjs' | |
2 | import { Component, OnInit } from '@angular/core' | |
3 | import { ComponentPagination, Notifier } from '@app/core' | |
4 | import { VideoChannel } from '@app/shared/shared-main' | |
5 | import { UserSubscriptionService } from '@app/shared/shared-user-subscription' | |
6 | import { debounceTime } from 'rxjs/operators' | |
7 | ||
8 | @Component({ | |
9 | selector: 'my-account-subscriptions', | |
10 | templateUrl: './my-account-subscriptions.component.html', | |
11 | styleUrls: [ './my-account-subscriptions.component.scss' ] | |
12 | }) | |
13 | export class MyAccountSubscriptionsComponent implements OnInit { | |
14 | videoChannels: VideoChannel[] = [] | |
15 | ||
16 | pagination: ComponentPagination = { | |
17 | currentPage: 1, | |
18 | itemsPerPage: 10, | |
19 | totalItems: null | |
20 | } | |
21 | ||
22 | onDataSubject = new Subject<any[]>() | |
23 | ||
24 | subscriptionsSearch: string | |
25 | subscriptionsSearchChanged = new Subject<string>() | |
26 | ||
27 | constructor ( | |
28 | private userSubscriptionService: UserSubscriptionService, | |
29 | private notifier: Notifier | |
30 | ) {} | |
31 | ||
32 | ngOnInit () { | |
33 | this.loadSubscriptions() | |
34 | ||
35 | this.subscriptionsSearchChanged | |
36 | .pipe(debounceTime(500)) | |
37 | .subscribe(() => { | |
38 | this.pagination.currentPage = 1 | |
39 | this.loadSubscriptions(false) | |
40 | }) | |
41 | } | |
42 | ||
43 | resetSearch () { | |
44 | this.subscriptionsSearch = '' | |
45 | this.onSubscriptionsSearchChanged() | |
46 | } | |
47 | ||
48 | onSubscriptionsSearchChanged () { | |
49 | this.subscriptionsSearchChanged.next() | |
50 | } | |
51 | ||
52 | onNearOfBottom () { | |
53 | // Last page | |
54 | if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return | |
55 | ||
56 | this.pagination.currentPage += 1 | |
57 | this.loadSubscriptions() | |
58 | } | |
59 | ||
60 | private loadSubscriptions (more = true) { | |
61 | this.userSubscriptionService.listSubscriptions({ pagination: this.pagination, search: this.subscriptionsSearch }) | |
62 | .subscribe( | |
63 | res => { | |
64 | this.videoChannels = more | |
65 | ? this.videoChannels.concat(res.data) | |
66 | : res.data | |
67 | this.pagination.totalItems = res.total | |
68 | ||
69 | this.onDataSubject.next(res.data) | |
70 | }, | |
71 | ||
72 | error => this.notifier.error(error.message) | |
73 | ) | |
74 | } | |
75 | } |