]>
Commit | Line | Data |
---|---|---|
734a5ceb | 1 | import { Component, OnDestroy, OnInit } from '@angular/core' |
d3e91a5f | 2 | import { ActivatedRoute } from '@angular/router' |
d3e91a5f C |
3 | import { Account } from '@app/shared/account/account.model' |
4 | import { AccountService } from '@app/shared/account/account.service' | |
d3e91a5f | 5 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' |
c8487f3f | 6 | import { concatMap, map, switchMap, tap } from 'rxjs/operators' |
ad453580 | 7 | import { from, Subject, Subscription } from 'rxjs' |
8a19bee1 | 8 | import { VideoChannel } from '@app/shared/video-channel/video-channel.model' |
c8487f3f C |
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' | |
6eb62c33 | 14 | import { ScreenService } from '@app/shared/misc/screen.service' |
d3e91a5f C |
15 | |
16 | @Component({ | |
17 | selector: 'my-account-video-channels', | |
18 | templateUrl: './account-video-channels.component.html', | |
19 | styleUrls: [ './account-video-channels.component.scss' ] | |
20 | }) | |
734a5ceb | 21 | export class AccountVideoChannelsComponent implements OnInit, OnDestroy { |
d3e91a5f C |
22 | account: Account |
23 | videoChannels: VideoChannel[] = [] | |
c8487f3f C |
24 | videos: { [id: number]: Video[] } = {} |
25 | ||
26 | channelPagination: ComponentPagination = { | |
27 | currentPage: 1, | |
440d39c5 C |
28 | itemsPerPage: 2, |
29 | totalItems: null | |
c8487f3f C |
30 | } |
31 | ||
32 | videosPagination: ComponentPagination = { | |
33 | currentPage: 1, | |
440d39c5 C |
34 | itemsPerPage: 12, |
35 | totalItems: null | |
c8487f3f C |
36 | } |
37 | videosSort: VideoSortField = '-publishedAt' | |
d3e91a5f | 38 | |
ad453580 C |
39 | onChannelDataSubject = new Subject<any>() |
40 | ||
734a5ceb C |
41 | private accountSub: Subscription |
42 | ||
d3e91a5f | 43 | constructor ( |
c8487f3f C |
44 | private route: ActivatedRoute, |
45 | private authService: AuthService, | |
d3e91a5f | 46 | private accountService: AccountService, |
c8487f3f | 47 | private videoChannelService: VideoChannelService, |
6eb62c33 C |
48 | private videoService: VideoService, |
49 | private screenService: ScreenService | |
d3e91a5f C |
50 | ) { } |
51 | ||
c8487f3f C |
52 | get user () { |
53 | return this.authService.getUser() | |
54 | } | |
55 | ||
d3e91a5f C |
56 | ngOnInit () { |
57 | // Parent get the account for us | |
734a5ceb | 58 | this.accountSub = this.accountService.accountLoaded |
c8487f3f C |
59 | .subscribe(account => { |
60 | this.account = account | |
61 | ||
62 | this.loadMoreChannels() | |
63 | }) | |
d3e91a5f | 64 | } |
734a5ceb C |
65 | |
66 | ngOnDestroy () { | |
67 | if (this.accountSub) this.accountSub.unsubscribe() | |
68 | } | |
c8487f3f C |
69 | |
70 | loadMoreChannels () { | |
71 | this.videoChannelService.listAccountVideoChannels(this.account, this.channelPagination) | |
72 | .pipe( | |
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) | |
93cae479 | 77 | .pipe(map(data => ({ videoChannel, videos: data.data }))) |
c8487f3f C |
78 | }) |
79 | ) | |
80 | .subscribe(({ videoChannel, videos }) => { | |
81 | this.videoChannels.push(videoChannel) | |
82 | ||
83 | this.videos[videoChannel.id] = videos | |
ad453580 C |
84 | |
85 | this.onChannelDataSubject.next([ videoChannel ]) | |
c8487f3f C |
86 | }) |
87 | } | |
88 | ||
89 | getVideosOf (videoChannel: VideoChannel) { | |
6eb62c33 C |
90 | const numberOfVideos = this.screenService.getNumberOfAvailableMiniatures() |
91 | ||
92 | // 2 rows | |
93 | return this.videos[ videoChannel.id ].slice(0, numberOfVideos * 2) | |
c8487f3f C |
94 | } |
95 | ||
96 | onNearOfBottom () { | |
97 | if (!hasMoreItems(this.channelPagination)) return | |
98 | ||
99 | this.channelPagination.currentPage += 1 | |
100 | ||
101 | this.loadMoreChannels() | |
102 | } | |
dc890263 C |
103 | |
104 | getVideoChannelLink (videoChannel: VideoChannel) { | |
105 | return [ '/video-channels', videoChannel.nameWithHost ] | |
106 | } | |
d3e91a5f | 107 | } |