8b70a9b1abbe94582aaf043816be68398ae77e3e
[oweals/peertube.git] /
1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { Notifier } from '@app/core'
3 import { AuthService } from '../../core/auth'
4 import { ConfirmService } from '../../core/confirm'
5 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
6 import { Video } from '@app/shared/video/video.model'
7 import { Subscription } from 'rxjs'
8 import { ActivatedRoute } from '@angular/router'
9 import { VideoService } from '@app/shared/video/video.service'
10
11 @Component({
12   selector: 'my-account-video-playlist-elements',
13   templateUrl: './my-account-video-playlist-elements.component.html',
14   styleUrls: [ './my-account-video-playlist-elements.component.scss' ]
15 })
16 export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestroy {
17   videos: Video[] = []
18
19   pagination: ComponentPagination = {
20     currentPage: 1,
21     itemsPerPage: 10,
22     totalItems: null
23   }
24
25   private videoPlaylistId: string | number
26   private paramsSub: Subscription
27
28   constructor (
29     private authService: AuthService,
30     private notifier: Notifier,
31     private confirmService: ConfirmService,
32     private route: ActivatedRoute,
33     private videoService: VideoService
34   ) {}
35
36   ngOnInit () {
37     this.paramsSub = this.route.params.subscribe(routeParams => {
38       this.videoPlaylistId = routeParams[ 'videoPlaylistId' ]
39       this.loadElements()
40     })
41   }
42
43   ngOnDestroy () {
44     if (this.paramsSub) this.paramsSub.unsubscribe()
45   }
46
47   onNearOfBottom () {
48     // Last page
49     if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
50
51     this.pagination.currentPage += 1
52     this.loadElements()
53   }
54
55   private loadElements () {
56     this.videoService.getPlaylistVideos(this.videoPlaylistId, this.pagination)
57         .subscribe(({ totalVideos, videos }) => {
58           this.videos = this.videos.concat(videos)
59           this.pagination.totalItems = totalVideos
60         })
61   }
62 }