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 selector: 'my-account-video-playlists',
10 templateUrl: './my-account-video-playlists.component.html',
11 styleUrls: [ './my-account-video-playlists.component.scss' ]
13 export class MyAccountVideoPlaylistsComponent implements OnInit {
14 videoPlaylistsSearch: string
15 videoPlaylists: VideoPlaylist[] = []
16 videoPlaylistSearchChanged = new Subject<string>()
18 pagination: ComponentPagination = {
24 onDataSubject = new Subject<any[]>()
29 private authService: AuthService,
30 private notifier: Notifier,
31 private confirmService: ConfirmService,
32 private videoPlaylistService: VideoPlaylistService
36 this.user = this.authService.getUser()
38 this.loadVideoPlaylists()
40 this.videoPlaylistSearchChanged
44 this.loadVideoPlaylists(true)
48 async deleteVideoPlaylist (videoPlaylist: VideoPlaylist) {
49 const res = await this.confirmService.confirm(
50 $localize`Do you really want to delete ${videoPlaylist.displayName}?`,
53 if (res === false) return
55 this.videoPlaylistService.removeVideoPlaylist(videoPlaylist)
58 this.videoPlaylists = this.videoPlaylists
59 .filter(p => p.id !== videoPlaylist.id)
61 this.notifier.success($localize`Playlist ${videoPlaylist.displayName}} deleted.`)
64 error => this.notifier.error(error.message)
68 isRegularPlaylist (playlist: VideoPlaylist) {
69 return playlist.type.id === VideoPlaylistType.REGULAR
74 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
76 this.pagination.currentPage += 1
77 this.loadVideoPlaylists()
81 this.videoPlaylistsSearch = ''
82 this.onVideoPlaylistSearchChanged()
85 onVideoPlaylistSearchChanged () {
86 this.videoPlaylistSearchChanged.next()
89 private loadVideoPlaylists (reset = false) {
90 this.authService.userInformationLoaded
91 .pipe(mergeMap(() => {
92 return this.videoPlaylistService.listAccountPlaylists(this.user.account, this.pagination, '-updatedAt', this.videoPlaylistsSearch)
95 if (reset) this.videoPlaylists = []
96 this.videoPlaylists = this.videoPlaylists.concat(res.data)
97 this.pagination.totalItems = res.total
99 this.onDataSubject.next(res.data)