]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+my-account/my-account-subscriptions/my-account-subscriptions.component.ts
fix CONTRIBUTING.md command order (#3305)
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-subscriptions / my-account-subscriptions.component.ts
CommitLineData
ad453580 1import { Subject } from 'rxjs'
67ed6552
C
2import { Component, OnInit } from '@angular/core'
3import { ComponentPagination, Notifier } from '@app/core'
4import { VideoChannel } from '@app/shared/shared-main'
5import { UserSubscriptionService } from '@app/shared/shared-user-subscription'
4f5d0459 6import { debounceTime } from 'rxjs/operators'
22a16e36
C
7
8@Component({
9 selector: 'my-account-subscriptions',
10 templateUrl: './my-account-subscriptions.component.html',
11 styleUrls: [ './my-account-subscriptions.component.scss' ]
12})
13export class MyAccountSubscriptionsComponent implements OnInit {
14 videoChannels: VideoChannel[] = []
15
aa55a4da
C
16 pagination: ComponentPagination = {
17 currentPage: 1,
18 itemsPerPage: 10,
19 totalItems: null
20 }
21
ad453580
C
22 onDataSubject = new Subject<any[]>()
23
4f5d0459
RK
24 subscriptionsSearch: string
25 subscriptionsSearchChanged = new Subject<string>()
26
22a16e36
C
27 constructor (
28 private userSubscriptionService: UserSubscriptionService,
830b4faf 29 private notifier: Notifier
22a16e36
C
30 ) {}
31
32 ngOnInit () {
aa55a4da 33 this.loadSubscriptions()
aa55a4da 34
4f5d0459
RK
35 this.subscriptionsSearchChanged
36 .pipe(debounceTime(500))
37 .subscribe(() => {
38 this.pagination.currentPage = 1
39 this.loadSubscriptions(false)
40 })
41 }
ad453580 42
4f5d0459
RK
43 resetSearch () {
44 this.subscriptionsSearch = ''
45 this.onSubscriptionsSearchChanged()
46 }
aa55a4da 47
4f5d0459
RK
48 onSubscriptionsSearchChanged () {
49 this.subscriptionsSearchChanged.next()
aa55a4da
C
50 }
51
52 onNearOfBottom () {
53 // Last page
54 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
22a16e36 55
aa55a4da
C
56 this.pagination.currentPage += 1
57 this.loadSubscriptions()
22a16e36
C
58 }
59
4f5d0459
RK
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 }
22a16e36 75}