aboutsummaryrefslogblamecommitdiffhomepage
path: root/client/src/app/+my-account/my-account-video-playlists/my-account-video-playlists.component.ts
blob: 1e569c0b68789852c6016063416c3a3a7e33b49c (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
                              
                                                       
                                                 

                                                                                            







                                                                 
                              
                                      
                                                    


                                     
                    


                    

                                      





                                           

                                                      




                                          




                                   
                                     
        



                                                            

                                                                            








                                                                           
                                                                                           









                                                         
                     





                                                                                                          
 




                                       



                                          
                                              
                                          
                              
                                                                                                                                            
           
                           
                                             

                                                                    

                                           

          
 
import { Subject } from 'rxjs'
import { debounceTime, mergeMap } from 'rxjs/operators'
import { Component, OnInit } from '@angular/core'
import { AuthService, ComponentPagination, ConfirmService, Notifier, User } from '@app/core'
import { VideoPlaylist, VideoPlaylistService } from '@app/shared/shared-video-playlist'
import { VideoPlaylistType } from '@shared/models'

@Component({
  selector: 'my-account-video-playlists',
  templateUrl: './my-account-video-playlists.component.html',
  styleUrls: [ './my-account-video-playlists.component.scss' ]
})
export class MyAccountVideoPlaylistsComponent implements OnInit {
  videoPlaylistsSearch: string
  videoPlaylists: VideoPlaylist[] = []
  videoPlaylistSearchChanged = new Subject<string>()

  pagination: ComponentPagination = {
    currentPage: 1,
    itemsPerPage: 5,
    totalItems: null
  }

  onDataSubject = new Subject<any[]>()

  private user: User

  constructor (
    private authService: AuthService,
    private notifier: Notifier,
    private confirmService: ConfirmService,
    private videoPlaylistService: VideoPlaylistService
    ) {}

  ngOnInit () {
    this.user = this.authService.getUser()

    this.loadVideoPlaylists()

    this.videoPlaylistSearchChanged
      .pipe(
        debounceTime(500))
      .subscribe(() => {
        this.loadVideoPlaylists(true)
      })
  }

  async deleteVideoPlaylist (videoPlaylist: VideoPlaylist) {
    const res = await this.confirmService.confirm(
      $localize`Do you really want to delete ${videoPlaylist.displayName}?`,
      $localize`Delete`
    )
    if (res === false) return

    this.videoPlaylistService.removeVideoPlaylist(videoPlaylist)
      .subscribe(
        () => {
          this.videoPlaylists = this.videoPlaylists
                                    .filter(p => p.id !== videoPlaylist.id)

          this.notifier.success($localize`Playlist ${videoPlaylist.displayName}} deleted.`)
        },

        error => this.notifier.error(error.message)
      )
  }

  isRegularPlaylist (playlist: VideoPlaylist) {
    return playlist.type.id === VideoPlaylistType.REGULAR
  }

  onNearOfBottom () {
    // Last page
    if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return

    this.pagination.currentPage += 1
    this.loadVideoPlaylists()
  }

  resetSearch () {
    this.videoPlaylistsSearch = ''
    this.onVideoPlaylistSearchChanged()
  }

  onVideoPlaylistSearchChanged () {
    this.videoPlaylistSearchChanged.next()
  }

  private loadVideoPlaylists (reset = false) {
    this.authService.userInformationLoaded
        .pipe(mergeMap(() => {
          return this.videoPlaylistService.listAccountPlaylists(this.user.account, this.pagination, '-updatedAt', this.videoPlaylistsSearch)
        }))
        .subscribe(res => {
          if (reset) this.videoPlaylists = []
          this.videoPlaylists = this.videoPlaylists.concat(res.data)
          this.pagination.totalItems = res.total

          this.onDataSubject.next(res.data)
        })
  }
}