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, Subject, 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'
14 import { ScreenService } from '@app/shared/misc/screen.service'
17 selector: 'my-account-video-channels',
18 templateUrl: './account-video-channels.component.html',
19 styleUrls: [ './account-video-channels.component.scss' ]
21 export class AccountVideoChannelsComponent implements OnInit, OnDestroy {
23 videoChannels: VideoChannel[] = []
24 videos: { [id: number]: Video[] } = {}
26 channelPagination: ComponentPagination = {
32 videosPagination: ComponentPagination = {
37 videosSort: VideoSortField = '-publishedAt'
39 onChannelDataSubject = new Subject<any>()
41 private accountSub: Subscription
44 private route: ActivatedRoute,
45 private authService: AuthService,
46 private accountService: AccountService,
47 private videoChannelService: VideoChannelService,
48 private videoService: VideoService,
49 private screenService: ScreenService
53 return this.authService.getUser()
57 // Parent get the account for us
58 this.accountSub = this.accountService.accountLoaded
59 .subscribe(account => {
60 this.account = account
62 this.loadMoreChannels()
67 if (this.accountSub) this.accountSub.unsubscribe()
71 this.videoChannelService.listAccountVideoChannels(this.account, this.channelPagination)
73 tap(res => this.channelPagination.totalItems = res.total),
74 switchMap(res => from(res.data)),
75 concatMap(videoChannel => {
76 return this.videoService.getVideoChannelVideos(videoChannel, this.videosPagination, this.videosSort)
77 .pipe(map(data => ({ videoChannel, videos: data.data })))
80 .subscribe(({ videoChannel, videos }) => {
81 this.videoChannels.push(videoChannel)
83 this.videos[videoChannel.id] = videos
85 this.onChannelDataSubject.next([ videoChannel ])
89 getVideosOf (videoChannel: VideoChannel) {
90 const numberOfVideos = this.screenService.getNumberOfAvailableMiniatures()
93 return this.videos[ videoChannel.id ].slice(0, numberOfVideos * 2)
97 if (!hasMoreItems(this.channelPagination)) return
99 this.channelPagination.currentPage += 1
101 this.loadMoreChannels()
104 getVideoChannelLink (videoChannel: VideoChannel) {
105 return [ '/video-channels', videoChannel.nameWithHost ]