]>
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' |
af75e2d8 | 4 | import { User, UserService } from '@app/shared' |
d3e91a5f C |
5 | import { Account } from '@app/shared/account/account.model' |
6 | import { AccountService } from '@app/shared/account/account.service' | |
5c20a455 C |
7 | import { ScreenService } from '@app/shared/misc/screen.service' |
8 | import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model' | |
8a19bee1 | 9 | import { VideoChannel } from '@app/shared/video-channel/video-channel.model' |
5c20a455 C |
10 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' |
11 | import { VideoSortField } from '@app/shared/video/sort-field.type' | |
c8487f3f | 12 | import { Video } from '@app/shared/video/video.model' |
c8487f3f | 13 | import { VideoService } from '@app/shared/video/video.service' |
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 | 20 | export 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, | |
440d39c5 C |
27 | itemsPerPage: 2, |
28 | totalItems: null | |
c8487f3f C |
29 | } |
30 | ||
31 | videosPagination: ComponentPagination = { | |
32 | currentPage: 1, | |
440d39c5 C |
33 | itemsPerPage: 12, |
34 | totalItems: null | |
c8487f3f C |
35 | } |
36 | videosSort: VideoSortField = '-publishedAt' | |
d3e91a5f | 37 | |
ad453580 C |
38 | onChannelDataSubject = new Subject<any>() |
39 | ||
5c20a455 C |
40 | userMiniature: User |
41 | ||
734a5ceb C |
42 | private accountSub: Subscription |
43 | ||
d3e91a5f | 44 | constructor ( |
d3e91a5f | 45 | private accountService: AccountService, |
c8487f3f | 46 | private videoChannelService: VideoChannelService, |
6eb62c33 | 47 | private videoService: VideoService, |
5c20a455 C |
48 | private screenService: ScreenService, |
49 | private userService: UserService | |
d3e91a5f C |
50 | ) { } |
51 | ||
52 | ngOnInit () { | |
53 | // Parent get the account for us | |
734a5ceb | 54 | this.accountSub = this.accountService.accountLoaded |
c8487f3f C |
55 | .subscribe(account => { |
56 | this.account = account | |
57 | ||
58 | this.loadMoreChannels() | |
59 | }) | |
5c20a455 C |
60 | |
61 | this.userService.getAnonymousOrLoggedUser() | |
62 | .subscribe(user => this.userMiniature = user) | |
d3e91a5f | 63 | } |
734a5ceb C |
64 | |
65 | ngOnDestroy () { | |
66 | if (this.accountSub) this.accountSub.unsubscribe() | |
67 | } | |
c8487f3f C |
68 | |
69 | loadMoreChannels () { | |
70 | this.videoChannelService.listAccountVideoChannels(this.account, this.channelPagination) | |
71 | .pipe( | |
72 | tap(res => this.channelPagination.totalItems = res.total), | |
73 | switchMap(res => from(res.data)), | |
74 | concatMap(videoChannel => { | |
75 | return this.videoService.getVideoChannelVideos(videoChannel, this.videosPagination, this.videosSort) | |
93cae479 | 76 | .pipe(map(data => ({ videoChannel, videos: data.data }))) |
c8487f3f C |
77 | }) |
78 | ) | |
79 | .subscribe(({ videoChannel, videos }) => { | |
80 | this.videoChannels.push(videoChannel) | |
81 | ||
82 | this.videos[videoChannel.id] = videos | |
ad453580 C |
83 | |
84 | this.onChannelDataSubject.next([ videoChannel ]) | |
c8487f3f C |
85 | }) |
86 | } | |
87 | ||
88 | getVideosOf (videoChannel: VideoChannel) { | |
6eb62c33 C |
89 | const numberOfVideos = this.screenService.getNumberOfAvailableMiniatures() |
90 | ||
91 | // 2 rows | |
92 | return this.videos[ videoChannel.id ].slice(0, numberOfVideos * 2) | |
c8487f3f C |
93 | } |
94 | ||
95 | onNearOfBottom () { | |
96 | if (!hasMoreItems(this.channelPagination)) return | |
97 | ||
98 | this.channelPagination.currentPage += 1 | |
99 | ||
100 | this.loadMoreChannels() | |
101 | } | |
dc890263 C |
102 | |
103 | getVideoChannelLink (videoChannel: VideoChannel) { | |
104 | return [ '/video-channels', videoChannel.nameWithHost ] | |
105 | } | |
d3e91a5f | 106 | } |