1 import { Subject } from 'rxjs'
2 import { mergeMap } from 'rxjs/operators'
3 import { Component } from '@angular/core'
4 import { AuthService, ComponentPagination, ConfirmService, Notifier } from '@app/core'
5 import { VideoPlaylist, VideoPlaylistService } from '@app/shared/shared-video-playlist'
6 import { VideoPlaylistType } from '@shared/models'
9 templateUrl: './my-video-playlists.component.html',
10 styleUrls: [ './my-video-playlists.component.scss' ]
12 export class MyVideoPlaylistsComponent {
13 videoPlaylists: VideoPlaylist[] = []
15 pagination: ComponentPagination = {
21 onDataSubject = new Subject<any[]>()
26 private authService: AuthService,
27 private notifier: Notifier,
28 private confirmService: ConfirmService,
29 private videoPlaylistService: VideoPlaylistService
32 async deleteVideoPlaylist (videoPlaylist: VideoPlaylist) {
33 const res = await this.confirmService.confirm(
34 $localize`Do you really want to delete ${videoPlaylist.displayName}?`,
37 if (res === false) return
39 this.videoPlaylistService.removeVideoPlaylist(videoPlaylist)
42 this.videoPlaylists = this.videoPlaylists
43 .filter(p => p.id !== videoPlaylist.id)
45 this.notifier.success($localize`Playlist ${videoPlaylist.displayName} deleted.`)
48 error: err => this.notifier.error(err.message)
52 isRegularPlaylist (playlist: VideoPlaylist) {
53 return playlist.type.id === VideoPlaylistType.REGULAR
58 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
60 this.pagination.currentPage += 1
61 this.loadVideoPlaylists()
64 onSearch (search: string) {
66 this.loadVideoPlaylists(true)
69 private loadVideoPlaylists (reset = false) {
70 this.authService.userInformationLoaded
71 .pipe(mergeMap(() => {
72 const user = this.authService.getUser()
74 return this.videoPlaylistService.listAccountPlaylists(user.account, this.pagination, '-updatedAt', this.search)
75 })).subscribe(res => {
76 if (reset) this.videoPlaylists = []
78 this.videoPlaylists = this.videoPlaylists.concat(res.data)
79 this.pagination.totalItems = res.total
81 this.onDataSubject.next(res.data)