1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { ActivatedRoute } from '@angular/router'
3 import { Account } from '@app/shared/account/account.model'
4 import { AccountService } from '@app/shared/account/account.service'
5 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
6 import { concatMap, map, switchMap, tap } from 'rxjs/operators'
7 import { from, Subscription } from 'rxjs'
8 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
9 import { Video } from '@app/shared/video/video.model'
10 import { AuthService } from '@app/core'
11 import { VideoService } from '@app/shared/video/video.service'
12 import { VideoSortField } from '@app/shared/video/sort-field.type'
13 import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model'
16 selector: 'my-account-video-channels',
17 templateUrl: './account-video-channels.component.html',
18 styleUrls: [ './account-video-channels.component.scss' ]
20 export class AccountVideoChannelsComponent implements OnInit, OnDestroy {
22 videoChannels: VideoChannel[] = []
23 videos: { [id: number]: Video[] } = {}
25 channelPagination: ComponentPagination = {
30 videosPagination: ComponentPagination = {
34 videosSort: VideoSortField = '-publishedAt'
36 private accountSub: Subscription
39 private route: ActivatedRoute,
40 private authService: AuthService,
41 private accountService: AccountService,
42 private videoChannelService: VideoChannelService,
43 private videoService: VideoService
47 return this.authService.getUser()
51 // Parent get the account for us
52 this.accountSub = this.accountService.accountLoaded
53 .subscribe(account => {
54 this.account = account
56 this.loadMoreChannels()
61 if (this.accountSub) this.accountSub.unsubscribe()
65 this.videoChannelService.listAccountVideoChannels(this.account, this.channelPagination)
67 tap(res => this.channelPagination.totalItems = res.total),
68 switchMap(res => from(res.data)),
69 concatMap(videoChannel => {
70 return this.videoService.getVideoChannelVideos(videoChannel, this.videosPagination, this.videosSort)
71 .pipe(map(data => ({ videoChannel, videos: data.videos })))
74 .subscribe(({ videoChannel, videos }) => {
75 this.videoChannels.push(videoChannel)
77 this.videos[videoChannel.id] = videos
81 getVideosOf (videoChannel: VideoChannel) {
82 return this.videos[ videoChannel.id ] || []
86 if (!hasMoreItems(this.channelPagination)) return
88 this.channelPagination.currentPage += 1
90 this.loadMoreChannels()