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