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