1 import { Component, OnInit } from '@angular/core'
2 import { Notifier } from '@app/core'
3 import { AuthService } from '../../core/auth'
4 import { ConfirmService } from '../../core/confirm'
5 import { User } from '@app/shared'
6 import { flatMap, debounceTime } from 'rxjs/operators'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model'
9 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
10 import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
11 import { VideoPlaylistType } from '@shared/models'
12 import { Subject } from 'rxjs'
15 selector: 'my-account-video-playlists',
16 templateUrl: './my-account-video-playlists.component.html',
17 styleUrls: [ './my-account-video-playlists.component.scss' ]
19 export class MyAccountVideoPlaylistsComponent implements OnInit {
20 videoPlaylistsSearch: string
21 videoPlaylists: VideoPlaylist[] = []
22 videoPlaylistSearchChanged = new Subject<string>()
24 pagination: ComponentPagination = {
30 onDataSubject = new Subject<any[]>()
35 private authService: AuthService,
36 private notifier: Notifier,
37 private confirmService: ConfirmService,
38 private videoPlaylistService: VideoPlaylistService,
43 this.user = this.authService.getUser()
45 this.loadVideoPlaylists()
47 this.videoPlaylistSearchChanged
51 this.loadVideoPlaylists(true)
55 async deleteVideoPlaylist (videoPlaylist: VideoPlaylist) {
56 const res = await this.confirmService.confirm(
58 'Do you really want to delete {{playlistDisplayName}}?',
59 { playlistDisplayName: videoPlaylist.displayName }
63 if (res === false) return
65 this.videoPlaylistService.removeVideoPlaylist(videoPlaylist)
68 this.videoPlaylists = this.videoPlaylists
69 .filter(p => p.id !== videoPlaylist.id)
71 this.notifier.success(
72 this.i18n('Playlist {{playlistDisplayName}} deleted.', { playlistDisplayName: videoPlaylist.displayName })
76 error => this.notifier.error(error.message)
80 isRegularPlaylist (playlist: VideoPlaylist) {
81 return playlist.type.id === VideoPlaylistType.REGULAR
86 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
88 this.pagination.currentPage += 1
89 this.loadVideoPlaylists()
92 onVideoPlaylistSearchChanged () {
93 this.videoPlaylistSearchChanged.next()
96 private loadVideoPlaylists (reset = false) {
97 this.authService.userInformationLoaded
99 return this.videoPlaylistService.listAccountPlaylists(this.user.account, this.pagination, '-updatedAt', this.videoPlaylistsSearch)
102 if (reset) this.videoPlaylists = []
103 this.videoPlaylists = this.videoPlaylists.concat(res.data)
104 this.pagination.totalItems = res.total
106 this.onDataSubject.next(res.data)