1 import { Subject } from 'rxjs'
2 import { debounceTime, flatMap } 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 { I18n } from '@ngx-translate/i18n-polyfill'
7 import { VideoPlaylistType } from '@shared/models'
10 selector: 'my-account-video-playlists',
11 templateUrl: './my-account-video-playlists.component.html',
12 styleUrls: [ './my-account-video-playlists.component.scss' ]
14 export class MyAccountVideoPlaylistsComponent implements OnInit {
15 videoPlaylistsSearch: string
16 videoPlaylists: VideoPlaylist[] = []
17 videoPlaylistSearchChanged = new Subject<string>()
19 pagination: ComponentPagination = {
25 onDataSubject = new Subject<any[]>()
30 private authService: AuthService,
31 private notifier: Notifier,
32 private confirmService: ConfirmService,
33 private videoPlaylistService: VideoPlaylistService,
38 this.user = this.authService.getUser()
40 this.loadVideoPlaylists()
42 this.videoPlaylistSearchChanged
46 this.loadVideoPlaylists(true)
50 async deleteVideoPlaylist (videoPlaylist: VideoPlaylist) {
51 const res = await this.confirmService.confirm(
53 'Do you really want to delete {{playlistDisplayName}}?',
54 { playlistDisplayName: videoPlaylist.displayName }
58 if (res === false) return
60 this.videoPlaylistService.removeVideoPlaylist(videoPlaylist)
63 this.videoPlaylists = this.videoPlaylists
64 .filter(p => p.id !== videoPlaylist.id)
66 this.notifier.success(
67 this.i18n('Playlist {{playlistDisplayName}} deleted.', { playlistDisplayName: videoPlaylist.displayName })
71 error => this.notifier.error(error.message)
75 isRegularPlaylist (playlist: VideoPlaylist) {
76 return playlist.type.id === VideoPlaylistType.REGULAR
81 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
83 this.pagination.currentPage += 1
84 this.loadVideoPlaylists()
87 onVideoPlaylistSearchChanged () {
88 this.videoPlaylistSearchChanged.next()
91 private loadVideoPlaylists (reset = false) {
92 this.authService.userInformationLoaded
94 return this.videoPlaylistService.listAccountPlaylists(this.user.account, this.pagination, '-updatedAt', this.videoPlaylistsSearch)
97 if (reset) this.videoPlaylists = []
98 this.videoPlaylists = this.videoPlaylists.concat(res.data)
99 this.pagination.totalItems = res.total
101 this.onDataSubject.next(res.data)