X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2F%2Baccounts%2Faccount-video-channels%2Faccount-video-channels.component.ts;h=2e5c5aae216f33f3a8adfd1368607f0c028dd6a4;hb=5c20a45518c3afc40c9494cad4a78def92e5e288;hp=a6e6dd656bbf73ccbbce65efee233e67eb8d6fff;hpb=ad9e39fb815d85e5e718c40540fa75471474fa17;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/+accounts/account-video-channels/account-video-channels.component.ts b/client/src/app/+accounts/account-video-channels/account-video-channels.component.ts index a6e6dd656..2e5c5aae2 100644 --- a/client/src/app/+accounts/account-video-channels/account-video-channels.component.ts +++ b/client/src/app/+accounts/account-video-channels/account-video-channels.component.ts @@ -1,34 +1,107 @@ -import { Component, OnInit } from '@angular/core' -import { ActivatedRoute } from '@angular/router' +import { from, Subject, Subscription } from 'rxjs' +import { concatMap, map, switchMap, tap } from 'rxjs/operators' +import { Component, OnDestroy, OnInit } from '@angular/core' +import { UserService } from '@app/shared' import { Account } from '@app/shared/account/account.model' import { AccountService } from '@app/shared/account/account.service' -import { VideoChannel } from '../../../../../shared/models/videos' +import { ScreenService } from '@app/shared/misc/screen.service' +import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model' +import { VideoChannel } from '@app/shared/video-channel/video-channel.model' import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' -import { flatMap, map, tap } from 'rxjs/operators' +import { VideoSortField } from '@app/shared/video/sort-field.type' +import { Video } from '@app/shared/video/video.model' +import { VideoService } from '@app/shared/video/video.service' +import { User } from '@shared/models' @Component({ selector: 'my-account-video-channels', templateUrl: './account-video-channels.component.html', styleUrls: [ './account-video-channels.component.scss' ] }) -export class AccountVideoChannelsComponent implements OnInit { +export class AccountVideoChannelsComponent implements OnInit, OnDestroy { account: Account videoChannels: VideoChannel[] = [] + videos: { [id: number]: Video[] } = {} + + channelPagination: ComponentPagination = { + currentPage: 1, + itemsPerPage: 2, + totalItems: null + } + + videosPagination: ComponentPagination = { + currentPage: 1, + itemsPerPage: 12, + totalItems: null + } + videosSort: VideoSortField = '-publishedAt' + + onChannelDataSubject = new Subject() + + userMiniature: User + + private accountSub: Subscription constructor ( - protected route: ActivatedRoute, private accountService: AccountService, - private videoChannelService: VideoChannelService + private videoChannelService: VideoChannelService, + private videoService: VideoService, + private screenService: ScreenService, + private userService: UserService ) { } ngOnInit () { // Parent get the account for us - this.accountService.accountLoaded - .pipe( - tap(account => this.account = account), - flatMap(account => this.videoChannelService.listAccountVideoChannels(account)), - map(res => res.data) - ) - .subscribe(videoChannels => this.videoChannels = videoChannels) + this.accountSub = this.accountService.accountLoaded + .subscribe(account => { + this.account = account + + this.loadMoreChannels() + }) + + this.userService.getAnonymousOrLoggedUser() + .subscribe(user => this.userMiniature = user) + } + + ngOnDestroy () { + if (this.accountSub) this.accountSub.unsubscribe() + } + + loadMoreChannels () { + this.videoChannelService.listAccountVideoChannels(this.account, this.channelPagination) + .pipe( + tap(res => this.channelPagination.totalItems = res.total), + switchMap(res => from(res.data)), + concatMap(videoChannel => { + return this.videoService.getVideoChannelVideos(videoChannel, this.videosPagination, this.videosSort) + .pipe(map(data => ({ videoChannel, videos: data.data }))) + }) + ) + .subscribe(({ videoChannel, videos }) => { + this.videoChannels.push(videoChannel) + + this.videos[videoChannel.id] = videos + + this.onChannelDataSubject.next([ videoChannel ]) + }) + } + + getVideosOf (videoChannel: VideoChannel) { + const numberOfVideos = this.screenService.getNumberOfAvailableMiniatures() + + // 2 rows + return this.videos[ videoChannel.id ].slice(0, numberOfVideos * 2) + } + + onNearOfBottom () { + if (!hasMoreItems(this.channelPagination)) return + + this.channelPagination.currentPage += 1 + + this.loadMoreChannels() + } + + getVideoChannelLink (videoChannel: VideoChannel) { + return [ '/video-channels', videoChannel.nameWithHost ] } }