1 import { from, Subject, Subscription } from 'rxjs'
2 import { concatMap, map, switchMap, tap } from 'rxjs/operators'
3 import { Component, OnDestroy, OnInit } from '@angular/core'
4 import { ComponentPagination, hasMoreItems, ScreenService, User, UserService } from '@app/core'
5 import { Account, AccountService, Video, VideoChannel, VideoChannelService, VideoService } from '@app/shared/shared-main'
6 import { VideoSortField } from '@shared/models'
9 selector: 'my-account-video-channels',
10 templateUrl: './account-video-channels.component.html',
11 styleUrls: [ './account-video-channels.component.scss' ]
13 export class AccountVideoChannelsComponent implements OnInit, OnDestroy {
15 videoChannels: VideoChannel[] = []
16 videos: { [id: number]: Video[] } = {}
18 channelPagination: ComponentPagination = {
24 videosPagination: ComponentPagination = {
29 videosSort: VideoSortField = '-publishedAt'
31 onChannelDataSubject = new Subject<any>()
35 private accountSub: Subscription
38 private accountService: AccountService,
39 private videoChannelService: VideoChannelService,
40 private videoService: VideoService,
41 private screenService: ScreenService,
42 private userService: UserService
46 // Parent get the account for us
47 this.accountSub = this.accountService.accountLoaded
48 .subscribe(account => {
49 this.account = account
51 this.loadMoreChannels()
54 this.userService.getAnonymousOrLoggedUser()
55 .subscribe(user => this.userMiniature = user)
59 if (this.accountSub) this.accountSub.unsubscribe()
63 this.videoChannelService.listAccountVideoChannels(this.account, this.channelPagination)
65 tap(res => this.channelPagination.totalItems = res.total),
66 switchMap(res => from(res.data)),
67 concatMap(videoChannel => {
68 return this.videoService.getVideoChannelVideos(videoChannel, this.videosPagination, this.videosSort)
69 .pipe(map(data => ({ videoChannel, videos: data.data })))
72 .subscribe(({ videoChannel, videos }) => {
73 this.videoChannels.push(videoChannel)
75 this.videos[videoChannel.id] = videos
77 this.onChannelDataSubject.next([ videoChannel ])
81 getVideosOf (videoChannel: VideoChannel) {
82 const numberOfVideos = this.screenService.getNumberOfAvailableMiniatures()
85 return this.videos[ videoChannel.id ].slice(0, numberOfVideos * 2)
89 if (!hasMoreItems(this.channelPagination)) return
91 this.channelPagination.currentPage += 1
93 this.loadMoreChannels()
96 getVideoChannelLink (videoChannel: VideoChannel) {
97 return [ '/video-channels', videoChannel.nameWithHost ]