diff options
Diffstat (limited to 'client/src/app/+accounts/account-video-channels/account-video-channels.component.ts')
-rw-r--r-- | client/src/app/+accounts/account-video-channels/account-video-channels.component.ts | 71 |
1 files changed, 61 insertions, 10 deletions
diff --git a/client/src/app/+accounts/account-video-channels/account-video-channels.component.ts b/client/src/app/+accounts/account-video-channels/account-video-channels.component.ts index 44f5626bb..ee3b5f8e4 100644 --- a/client/src/app/+accounts/account-video-channels/account-video-channels.component.ts +++ b/client/src/app/+accounts/account-video-channels/account-video-channels.component.ts | |||
@@ -3,9 +3,14 @@ import { ActivatedRoute } from '@angular/router' | |||
3 | import { Account } from '@app/shared/account/account.model' | 3 | import { Account } from '@app/shared/account/account.model' |
4 | import { AccountService } from '@app/shared/account/account.service' | 4 | import { AccountService } from '@app/shared/account/account.service' |
5 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' | 5 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' |
6 | import { flatMap, map, tap } from 'rxjs/operators' | 6 | import { concatMap, map, switchMap, tap } from 'rxjs/operators' |
7 | import { Subscription } from 'rxjs' | 7 | import { from, Subscription } from 'rxjs' |
8 | import { VideoChannel } from '@app/shared/video-channel/video-channel.model' | 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' | ||
9 | 14 | ||
10 | @Component({ | 15 | @Component({ |
11 | selector: 'my-account-video-channels', | 16 | selector: 'my-account-video-channels', |
@@ -15,27 +20,73 @@ import { VideoChannel } from '@app/shared/video-channel/video-channel.model' | |||
15 | export class AccountVideoChannelsComponent implements OnInit, OnDestroy { | 20 | export class AccountVideoChannelsComponent implements OnInit, OnDestroy { |
16 | account: Account | 21 | account: Account |
17 | videoChannels: VideoChannel[] = [] | 22 | videoChannels: VideoChannel[] = [] |
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' | ||
18 | 35 | ||
19 | private accountSub: Subscription | 36 | private accountSub: Subscription |
20 | 37 | ||
21 | constructor ( | 38 | constructor ( |
22 | protected route: ActivatedRoute, | 39 | private route: ActivatedRoute, |
40 | private authService: AuthService, | ||
23 | private accountService: AccountService, | 41 | private accountService: AccountService, |
24 | private videoChannelService: VideoChannelService | 42 | private videoChannelService: VideoChannelService, |
43 | private videoService: VideoService | ||
25 | ) { } | 44 | ) { } |
26 | 45 | ||
46 | get user () { | ||
47 | return this.authService.getUser() | ||
48 | } | ||
49 | |||
27 | ngOnInit () { | 50 | ngOnInit () { |
28 | // Parent get the account for us | 51 | // Parent get the account for us |
29 | this.accountSub = this.accountService.accountLoaded | 52 | this.accountSub = this.accountService.accountLoaded |
30 | .pipe( | 53 | .subscribe(account => { |
31 | tap(account => this.account = account), | 54 | this.account = account |
32 | flatMap(account => this.videoChannelService.listAccountVideoChannels(account)), | 55 | |
33 | map(res => res.data) | 56 | this.loadMoreChannels() |
34 | ) | 57 | }) |
35 | .subscribe(videoChannels => this.videoChannels = videoChannels) | ||
36 | } | 58 | } |
37 | 59 | ||
38 | ngOnDestroy () { | 60 | ngOnDestroy () { |
39 | if (this.accountSub) this.accountSub.unsubscribe() | 61 | if (this.accountSub) this.accountSub.unsubscribe() |
40 | } | 62 | } |
63 | |||
64 | loadMoreChannels () { | ||
65 | this.videoChannelService.listAccountVideoChannels(this.account, this.channelPagination) | ||
66 | .pipe( | ||
67 | tap(res => this.channelPagination.totalItems = res.total), | ||
68 | switchMap(res => from(res.data)), | ||
69 | concatMap(videoChannel => { | ||
70 | return this.videoService.getVideoChannelVideos(videoChannel, this.videosPagination, this.videosSort) | ||
71 | .pipe(map(data => ({ videoChannel, videos: data.videos }))) | ||
72 | }) | ||
73 | ) | ||
74 | .subscribe(({ videoChannel, videos }) => { | ||
75 | this.videoChannels.push(videoChannel) | ||
76 | |||
77 | this.videos[videoChannel.id] = videos | ||
78 | }) | ||
79 | } | ||
80 | |||
81 | getVideosOf (videoChannel: VideoChannel) { | ||
82 | return this.videos[ videoChannel.id ] || [] | ||
83 | } | ||
84 | |||
85 | onNearOfBottom () { | ||
86 | if (!hasMoreItems(this.channelPagination)) return | ||
87 | |||
88 | this.channelPagination.currentPage += 1 | ||
89 | |||
90 | this.loadMoreChannels() | ||
91 | } | ||
41 | } | 92 | } |