]>
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' | |
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, | |
27 | itemsPerPage: 2 | |
28 | } | |
29 | ||
30 | videosPagination: ComponentPagination = { | |
31 | currentPage: 1, | |
32 | itemsPerPage: 12 | |
33 | } | |
34 | videosSort: VideoSortField = '-publishedAt' | |
d3e91a5f | 35 | |
ad453580 C |
36 | onChannelDataSubject = new Subject<any>() |
37 | ||
734a5ceb C |
38 | private accountSub: Subscription |
39 | ||
d3e91a5f | 40 | constructor ( |
c8487f3f C |
41 | private route: ActivatedRoute, |
42 | private authService: AuthService, | |
d3e91a5f | 43 | private accountService: AccountService, |
c8487f3f C |
44 | private videoChannelService: VideoChannelService, |
45 | private videoService: VideoService | |
d3e91a5f C |
46 | ) { } |
47 | ||
c8487f3f C |
48 | get user () { |
49 | return this.authService.getUser() | |
50 | } | |
51 | ||
d3e91a5f C |
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 | }) | |
d3e91a5f | 60 | } |
734a5ceb C |
61 | |
62 | ngOnDestroy () { | |
63 | if (this.accountSub) this.accountSub.unsubscribe() | |
64 | } | |
c8487f3f C |
65 | |
66 | loadMoreChannels () { | |
67 | this.videoChannelService.listAccountVideoChannels(this.account, this.channelPagination) | |
68 | .pipe( | |
69 | tap(res => this.channelPagination.totalItems = res.total), | |
70 | switchMap(res => from(res.data)), | |
71 | concatMap(videoChannel => { | |
72 | return this.videoService.getVideoChannelVideos(videoChannel, this.videosPagination, this.videosSort) | |
93cae479 | 73 | .pipe(map(data => ({ videoChannel, videos: data.data }))) |
c8487f3f C |
74 | }) |
75 | ) | |
76 | .subscribe(({ videoChannel, videos }) => { | |
77 | this.videoChannels.push(videoChannel) | |
78 | ||
79 | this.videos[videoChannel.id] = videos | |
ad453580 C |
80 | |
81 | this.onChannelDataSubject.next([ videoChannel ]) | |
c8487f3f C |
82 | }) |
83 | } | |
84 | ||
85 | getVideosOf (videoChannel: VideoChannel) { | |
fff77ba2 | 86 | return this.videos[ videoChannel.id ] |
c8487f3f C |
87 | } |
88 | ||
89 | onNearOfBottom () { | |
90 | if (!hasMoreItems(this.channelPagination)) return | |
91 | ||
92 | this.channelPagination.currentPage += 1 | |
93 | ||
94 | this.loadMoreChannels() | |
95 | } | |
dc890263 C |
96 | |
97 | getVideoChannelLink (videoChannel: VideoChannel) { | |
98 | return [ '/video-channels', videoChannel.nameWithHost ] | |
99 | } | |
d3e91a5f | 100 | } |