]>
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, | |
28 | itemsPerPage: 2 | |
29 | } | |
30 | ||
31 | videosPagination: ComponentPagination = { | |
32 | currentPage: 1, | |
33 | itemsPerPage: 12 | |
34 | } | |
35 | videosSort: VideoSortField = '-publishedAt' | |
d3e91a5f | 36 | |
ad453580 C |
37 | onChannelDataSubject = new Subject<any>() |
38 | ||
734a5ceb C |
39 | private accountSub: Subscription |
40 | ||
d3e91a5f | 41 | constructor ( |
c8487f3f C |
42 | private route: ActivatedRoute, |
43 | private authService: AuthService, | |
d3e91a5f | 44 | private accountService: AccountService, |
c8487f3f | 45 | private videoChannelService: VideoChannelService, |
6eb62c33 C |
46 | private videoService: VideoService, |
47 | private screenService: ScreenService | |
d3e91a5f C |
48 | ) { } |
49 | ||
c8487f3f C |
50 | get user () { |
51 | return this.authService.getUser() | |
52 | } | |
53 | ||
d3e91a5f C |
54 | ngOnInit () { |
55 | // Parent get the account for us | |
734a5ceb | 56 | this.accountSub = this.accountService.accountLoaded |
c8487f3f C |
57 | .subscribe(account => { |
58 | this.account = account | |
59 | ||
60 | this.loadMoreChannels() | |
61 | }) | |
d3e91a5f | 62 | } |
734a5ceb C |
63 | |
64 | ngOnDestroy () { | |
65 | if (this.accountSub) this.accountSub.unsubscribe() | |
66 | } | |
c8487f3f C |
67 | |
68 | loadMoreChannels () { | |
69 | this.videoChannelService.listAccountVideoChannels(this.account, this.channelPagination) | |
70 | .pipe( | |
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) | |
93cae479 | 75 | .pipe(map(data => ({ videoChannel, videos: data.data }))) |
c8487f3f C |
76 | }) |
77 | ) | |
78 | .subscribe(({ videoChannel, videos }) => { | |
79 | this.videoChannels.push(videoChannel) | |
80 | ||
81 | this.videos[videoChannel.id] = videos | |
ad453580 C |
82 | |
83 | this.onChannelDataSubject.next([ videoChannel ]) | |
c8487f3f C |
84 | }) |
85 | } | |
86 | ||
87 | getVideosOf (videoChannel: VideoChannel) { | |
6eb62c33 C |
88 | const numberOfVideos = this.screenService.getNumberOfAvailableMiniatures() |
89 | ||
90 | // 2 rows | |
91 | return this.videos[ videoChannel.id ].slice(0, numberOfVideos * 2) | |
c8487f3f C |
92 | } |
93 | ||
94 | onNearOfBottom () { | |
95 | if (!hasMoreItems(this.channelPagination)) return | |
96 | ||
97 | this.channelPagination.currentPage += 1 | |
98 | ||
99 | this.loadMoreChannels() | |
100 | } | |
dc890263 C |
101 | |
102 | getVideoChannelLink (videoChannel: VideoChannel) { | |
103 | return [ '/video-channels', videoChannel.nameWithHost ] | |
104 | } | |
d3e91a5f | 105 | } |