aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+accounts/account-video-channels/account-video-channels.component.ts
blob: 7144f4b5f501260d94c6a9ba1953e59700495517 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { Component, OnDestroy, OnInit } from '@angular/core'
import { ActivatedRoute } from '@angular/router'
import { Account } from '@app/shared/account/account.model'
import { AccountService } from '@app/shared/account/account.service'
import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
import { concatMap, map, switchMap, tap } from 'rxjs/operators'
import { from, Subscription } from 'rxjs'
import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
import { Video } from '@app/shared/video/video.model'
import { AuthService } from '@app/core'
import { VideoService } from '@app/shared/video/video.service'
import { VideoSortField } from '@app/shared/video/sort-field.type'
import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model'

@Component({
  selector: 'my-account-video-channels',
  templateUrl: './account-video-channels.component.html',
  styleUrls: [ './account-video-channels.component.scss' ]
})
export class AccountVideoChannelsComponent implements OnInit, OnDestroy {
  account: Account
  videoChannels: VideoChannel[] = []
  videos: { [id: number]: Video[] } = {}

  channelPagination: ComponentPagination = {
    currentPage: 1,
    itemsPerPage: 2
  }

  videosPagination: ComponentPagination = {
    currentPage: 1,
    itemsPerPage: 12
  }
  videosSort: VideoSortField = '-publishedAt'

  private accountSub: Subscription

  constructor (
    private route: ActivatedRoute,
    private authService: AuthService,
    private accountService: AccountService,
    private videoChannelService: VideoChannelService,
    private videoService: VideoService
  ) { }

  get user () {
    return this.authService.getUser()
  }

  ngOnInit () {
    // Parent get the account for us
    this.accountSub = this.accountService.accountLoaded
        .subscribe(account => {
          this.account = account

          this.loadMoreChannels()
        })
  }

  ngOnDestroy () {
    if (this.accountSub) this.accountSub.unsubscribe()
  }

  loadMoreChannels () {
    this.videoChannelService.listAccountVideoChannels(this.account, this.channelPagination)
      .pipe(
        tap(res => this.channelPagination.totalItems = res.total),
        switchMap(res => from(res.data)),
        concatMap(videoChannel => {
          return this.videoService.getVideoChannelVideos(videoChannel, this.videosPagination, this.videosSort)
            .pipe(map(data => ({ videoChannel, videos: data.data })))
        })
      )
      .subscribe(({ videoChannel, videos }) => {
        this.videoChannels.push(videoChannel)

        this.videos[videoChannel.id] = videos
      })
  }

  getVideosOf (videoChannel: VideoChannel) {
    return this.videos[ videoChannel.id ]
  }

  onNearOfBottom () {
    if (!hasMoreItems(this.channelPagination)) return

    this.channelPagination.currentPage += 1

    this.loadMoreChannels()
  }

  getVideoChannelLink (videoChannel: VideoChannel) {
    return [ '/video-channels', videoChannel.nameWithHost ]
  }
}