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 { UserService } from '@app/shared'
5 import { Account } from '@app/shared/account/account.model'
6 import { AccountService } from '@app/shared/account/account.service'
7 import { ScreenService } from '@app/shared/misc/screen.service'
8 import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model'
9 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
10 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
11 import { VideoSortField } from '@app/shared/video/sort-field.type'
12 import { Video } from '@app/shared/video/video.model'
13 import { VideoService } from '@app/shared/video/video.service'
14 import { User } from '@shared/models'
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>()
43 private accountSub: Subscription
46 private accountService: AccountService,
47 private videoChannelService: VideoChannelService,
48 private videoService: VideoService,
49 private screenService: ScreenService,
50 private userService: UserService
54 // Parent get the account for us
55 this.accountSub = this.accountService.accountLoaded
56 .subscribe(account => {
57 this.account = account
59 this.loadMoreChannels()
62 this.userService.getAnonymousOrLoggedUser()
63 .subscribe(user => this.userMiniature = user)
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 ]