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'
9 selector: 'my-account-subscriptions',
10 templateUrl: './my-account-subscriptions.component.html',
11 styleUrls: [ './my-account-subscriptions.component.scss' ]
13 export class MyAccountSubscriptionsComponent implements OnInit {
14 videoChannels: VideoChannel[] = []
16 pagination: ComponentPagination = {
22 onDataSubject = new Subject<any[]>()
24 subscriptionsSearch: string
25 subscriptionsSearchChanged = new Subject<string>()
28 private userSubscriptionService: UserSubscriptionService,
29 private notifier: Notifier
33 this.loadSubscriptions()
35 this.subscriptionsSearchChanged
36 .pipe(debounceTime(500))
38 this.pagination.currentPage = 1
39 this.loadSubscriptions(false)
44 this.subscriptionsSearch = ''
45 this.onSubscriptionsSearchChanged()
48 onSubscriptionsSearchChanged () {
49 this.subscriptionsSearchChanged.next()
54 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
56 this.pagination.currentPage += 1
57 this.loadSubscriptions()
60 private loadSubscriptions (more = true) {
61 this.userSubscriptionService.listSubscriptions({ pagination: this.pagination, search: this.subscriptionsSearch })
64 this.videoChannels = more
65 ? this.videoChannels.concat(res.data)
67 this.pagination.totalItems = res.total
69 this.onDataSubject.next(res.data)
72 error => this.notifier.error(error.message)