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