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 = {
31 videosPagination: ComponentPagination = {
35 videosSort: VideoSortField = '-publishedAt'
37 onChannelDataSubject = new Subject<any>()
39 private accountSub: Subscription
42 private route: ActivatedRoute,
43 private authService: AuthService,
44 private accountService: AccountService,
45 private videoChannelService: VideoChannelService,
46 private videoService: VideoService,
47 private screenService: ScreenService
51 return this.authService.getUser()
55 // Parent get the account for us
56 this.accountSub = this.accountService.accountLoaded
57 .subscribe(account => {
58 this.account = account
60 this.loadMoreChannels()
65 if (this.accountSub) this.accountSub.unsubscribe()
69 this.videoChannelService.listAccountVideoChannels(this.account, this.channelPagination)
71 tap(res => this.channelPagination.totalItems = res.total),
72 switchMap(res => from(res.data)),
73 concatMap(videoChannel => {
74 return this.videoService.getVideoChannelVideos(videoChannel, this.videosPagination, this.videosSort)
75 .pipe(map(data => ({ videoChannel, videos: data.data })))
78 .subscribe(({ videoChannel, videos }) => {
79 this.videoChannels.push(videoChannel)
81 this.videos[videoChannel.id] = videos
83 this.onChannelDataSubject.next([ videoChannel ])
87 getVideosOf (videoChannel: VideoChannel) {
88 const numberOfVideos = this.screenService.getNumberOfAvailableMiniatures()
91 return this.videos[ videoChannel.id ].slice(0, numberOfVideos * 2)
95 if (!hasMoreItems(this.channelPagination)) return
97 this.channelPagination.currentPage += 1
99 this.loadMoreChannels()
102 getVideoChannelLink (videoChannel: VideoChannel) {
103 return [ '/video-channels', videoChannel.nameWithHost ]