]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+my-account/my-account-video-playlists/my-account-video-playlists.component.ts
Lazy load static objects
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-video-playlists / my-account-video-playlists.component.ts
CommitLineData
830b4faf
C
1import { Component, OnInit } from '@angular/core'
2import { Notifier } from '@app/core'
3import { AuthService } from '../../core/auth'
4import { ConfirmService } from '../../core/confirm'
5import { User } from '@app/shared'
6import { flatMap } from 'rxjs/operators'
7import { I18n } from '@ngx-translate/i18n-polyfill'
8import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model'
9import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
10import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
11import { VideoPlaylistType } from '@shared/models'
ad453580 12import { Subject } from 'rxjs'
830b4faf
C
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})
19export class MyAccountVideoPlaylistsComponent implements OnInit {
20 videoPlaylists: VideoPlaylist[] = []
21
22 pagination: ComponentPagination = {
23 currentPage: 1,
ad453580 24 itemsPerPage: 5,
830b4faf
C
25 totalItems: null
26 }
27
ad453580
C
28 onDataSubject = new Subject<any[]>()
29
830b4faf
C
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
f0a39880 75 onNearOfBottom () {
830b4faf
C
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 }
f0a39880
C
82
83 private loadVideoPlaylists () {
84 this.authService.userInformationLoaded
f3628e69
C
85 .pipe(flatMap(() => {
86 return this.videoPlaylistService.listAccountPlaylists(this.user.account, this.pagination, '-updatedAt')
87 }))
f0a39880
C
88 .subscribe(res => {
89 this.videoPlaylists = this.videoPlaylists.concat(res.data)
90 this.pagination.totalItems = res.total
ad453580
C
91
92 this.onDataSubject.next(res.data)
f0a39880
C
93 })
94 }
830b4faf 95}