1 import { Subject } from 'rxjs'
2 import { debounceTime, mergeMap } from 'rxjs/operators'
3 import { Component, OnInit } from '@angular/core'
4 import { AuthService, ComponentPagination, ConfirmService, Notifier, User } 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 implements OnInit {
13 videoPlaylistsSearch: string
14 videoPlaylists: VideoPlaylist[] = []
15 videoPlaylistSearchChanged = new Subject<string>()
17 pagination: ComponentPagination = {
23 onDataSubject = new Subject<any[]>()
28 private authService: AuthService,
29 private notifier: Notifier,
30 private confirmService: ConfirmService,
31 private videoPlaylistService: VideoPlaylistService
35 this.user = this.authService.getUser()
37 this.loadVideoPlaylists()
39 this.videoPlaylistSearchChanged
43 this.loadVideoPlaylists(true)
47 async deleteVideoPlaylist (videoPlaylist: VideoPlaylist) {
48 const res = await this.confirmService.confirm(
49 $localize`Do you really want to delete ${videoPlaylist.displayName}?`,
52 if (res === false) return
54 this.videoPlaylistService.removeVideoPlaylist(videoPlaylist)
57 this.videoPlaylists = this.videoPlaylists
58 .filter(p => p.id !== videoPlaylist.id)
60 this.notifier.success($localize`Playlist ${videoPlaylist.displayName}} deleted.`)
63 error => this.notifier.error(error.message)
67 isRegularPlaylist (playlist: VideoPlaylist) {
68 return playlist.type.id === VideoPlaylistType.REGULAR
73 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
75 this.pagination.currentPage += 1
76 this.loadVideoPlaylists()
80 this.videoPlaylistsSearch = ''
81 this.onVideoPlaylistSearchChanged()
84 onVideoPlaylistSearchChanged () {
85 this.videoPlaylistSearchChanged.next()
88 private loadVideoPlaylists (reset = false) {
89 this.authService.userInformationLoaded
90 .pipe(mergeMap(() => {
91 return this.videoPlaylistService.listAccountPlaylists(this.user.account, this.pagination, '-updatedAt', this.videoPlaylistsSearch)
94 if (reset) this.videoPlaylists = []
95 this.videoPlaylists = this.videoPlaylists.concat(res.data)
96 this.pagination.totalItems = res.total
98 this.onDataSubject.next(res.data)