]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+accounts/account-video-channels/account-video-channels.component.ts
Add ability to subscribe from the channel account page
[github/Chocobozzz/PeerTube.git] / client / src / app / +accounts / account-video-channels / account-video-channels.component.ts
CommitLineData
734a5ceb 1import { Component, OnDestroy, OnInit } from '@angular/core'
d3e91a5f 2import { ActivatedRoute } from '@angular/router'
d3e91a5f
C
3import { Account } from '@app/shared/account/account.model'
4import { AccountService } from '@app/shared/account/account.service'
d3e91a5f 5import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
c8487f3f
C
6import { concatMap, map, switchMap, tap } from 'rxjs/operators'
7import { from, Subscription } from 'rxjs'
8a19bee1 8import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
c8487f3f
C
9import { Video } from '@app/shared/video/video.model'
10import { AuthService } from '@app/core'
11import { VideoService } from '@app/shared/video/video.service'
12import { VideoSortField } from '@app/shared/video/sort-field.type'
13import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model'
d3e91a5f
C
14
15@Component({
16 selector: 'my-account-video-channels',
17 templateUrl: './account-video-channels.component.html',
18 styleUrls: [ './account-video-channels.component.scss' ]
19})
734a5ceb 20export class AccountVideoChannelsComponent implements OnInit, OnDestroy {
d3e91a5f
C
21 account: Account
22 videoChannels: VideoChannel[] = []
c8487f3f
C
23 videos: { [id: number]: Video[] } = {}
24
25 channelPagination: ComponentPagination = {
26 currentPage: 1,
27 itemsPerPage: 2
28 }
29
30 videosPagination: ComponentPagination = {
31 currentPage: 1,
32 itemsPerPage: 12
33 }
34 videosSort: VideoSortField = '-publishedAt'
d3e91a5f 35
734a5ceb
C
36 private accountSub: Subscription
37
d3e91a5f 38 constructor (
c8487f3f
C
39 private route: ActivatedRoute,
40 private authService: AuthService,
d3e91a5f 41 private accountService: AccountService,
c8487f3f
C
42 private videoChannelService: VideoChannelService,
43 private videoService: VideoService
d3e91a5f
C
44 ) { }
45
c8487f3f
C
46 get user () {
47 return this.authService.getUser()
48 }
49
d3e91a5f
C
50 ngOnInit () {
51 // Parent get the account for us
734a5ceb 52 this.accountSub = this.accountService.accountLoaded
c8487f3f
C
53 .subscribe(account => {
54 this.account = account
55
56 this.loadMoreChannels()
57 })
d3e91a5f 58 }
734a5ceb
C
59
60 ngOnDestroy () {
61 if (this.accountSub) this.accountSub.unsubscribe()
62 }
c8487f3f
C
63
64 loadMoreChannels () {
65 this.videoChannelService.listAccountVideoChannels(this.account, this.channelPagination)
66 .pipe(
67 tap(res => this.channelPagination.totalItems = res.total),
68 switchMap(res => from(res.data)),
69 concatMap(videoChannel => {
70 return this.videoService.getVideoChannelVideos(videoChannel, this.videosPagination, this.videosSort)
71 .pipe(map(data => ({ videoChannel, videos: data.videos })))
72 })
73 )
74 .subscribe(({ videoChannel, videos }) => {
75 this.videoChannels.push(videoChannel)
76
77 this.videos[videoChannel.id] = videos
78 })
79 }
80
81 getVideosOf (videoChannel: VideoChannel) {
82 return this.videos[ videoChannel.id ] || []
83 }
84
85 onNearOfBottom () {
86 if (!hasMoreItems(this.channelPagination)) return
87
88 this.channelPagination.currentPage += 1
89
90 this.loadMoreChannels()
91 }
d3e91a5f 92}