Fix infinite scroll on big screens
[oweals/peertube.git] / client / src / app / +my-account / my-account-video-playlists / my-account-video-playlists.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { Notifier } from '@app/core'
3 import { AuthService } from '../../core/auth'
4 import { ConfirmService } from '../../core/confirm'
5 import { User } from '@app/shared'
6 import { flatMap } from 'rxjs/operators'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model'
9 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
10 import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
11 import { VideoPlaylistType } from '@shared/models'
12 import { Subject } from 'rxjs'
13
14 @Component({
15   selector: 'my-account-video-playlists',
16   templateUrl: './my-account-video-playlists.component.html',
17   styleUrls: [ './my-account-video-playlists.component.scss' ]
18 })
19 export class MyAccountVideoPlaylistsComponent implements OnInit {
20   videoPlaylists: VideoPlaylist[] = []
21
22   pagination: ComponentPagination = {
23     currentPage: 1,
24     itemsPerPage: 5,
25     totalItems: null
26   }
27
28   onDataSubject = new Subject<any[]>()
29
30   private user: User
31
32   constructor (
33     private authService: AuthService,
34     private notifier: Notifier,
35     private confirmService: ConfirmService,
36     private videoPlaylistService: VideoPlaylistService,
37     private i18n: I18n
38   ) {}
39
40   ngOnInit () {
41     this.user = this.authService.getUser()
42
43     this.loadVideoPlaylists()
44   }
45
46   async deleteVideoPlaylist (videoPlaylist: VideoPlaylist) {
47     const res = await this.confirmService.confirm(
48       this.i18n(
49         'Do you really want to delete {{playlistDisplayName}}?',
50         { playlistDisplayName: videoPlaylist.displayName }
51       ),
52       this.i18n('Delete')
53     )
54     if (res === false) return
55
56     this.videoPlaylistService.removeVideoPlaylist(videoPlaylist)
57       .subscribe(
58         () => {
59           this.videoPlaylists = this.videoPlaylists
60                                     .filter(p => p.id !== videoPlaylist.id)
61
62           this.notifier.success(
63             this.i18n('Playlist {{playlistDisplayName}} deleted.', { playlistDisplayName: videoPlaylist.displayName })
64           )
65         },
66
67         error => this.notifier.error(error.message)
68       )
69   }
70
71   isRegularPlaylist (playlist: VideoPlaylist) {
72     return playlist.type.id === VideoPlaylistType.REGULAR
73   }
74
75   onNearOfBottom () {
76     // Last page
77     if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
78
79     this.pagination.currentPage += 1
80     this.loadVideoPlaylists()
81   }
82
83   private loadVideoPlaylists () {
84     const playlistsObservable = this.videoPlaylistService.listAccountPlaylists(this.user.account, this.pagination, '-updatedAt')
85
86     this.authService.userInformationLoaded
87         .pipe(flatMap(() => playlistsObservable))
88         .subscribe(res => {
89           this.videoPlaylists = this.videoPlaylists.concat(res.data)
90           this.pagination.totalItems = res.total
91
92           this.onDataSubject.next(res.data)
93         })
94   }
95 }