]>
Commit | Line | Data |
---|---|---|
830b4faf C |
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' | |
bf64ed41 | 6 | import { flatMap, debounceTime } from 'rxjs/operators' |
830b4faf C |
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' | |
ad453580 | 12 | import { Subject } from 'rxjs' |
830b4faf C |
13 | |
14 | @Component({ | |
15 | selector: 'my-account-video-playlists', | |
16 | templateUrl: './my-account-video-playlists.component.html', | |
17 | styleUrls: [ './my-account-video-playlists.component.scss' ] | |
18 | }) | |
19 | export class MyAccountVideoPlaylistsComponent implements OnInit { | |
bf64ed41 | 20 | videoPlaylistsSearch: string |
830b4faf | 21 | videoPlaylists: VideoPlaylist[] = [] |
bf64ed41 | 22 | videoPlaylistSearchChanged = new Subject<string>() |
830b4faf C |
23 | |
24 | pagination: ComponentPagination = { | |
25 | currentPage: 1, | |
ad453580 | 26 | itemsPerPage: 5, |
830b4faf C |
27 | totalItems: null |
28 | } | |
29 | ||
ad453580 C |
30 | onDataSubject = new Subject<any[]>() |
31 | ||
830b4faf C |
32 | private user: User |
33 | ||
34 | constructor ( | |
35 | private authService: AuthService, | |
36 | private notifier: Notifier, | |
37 | private confirmService: ConfirmService, | |
38 | private videoPlaylistService: VideoPlaylistService, | |
39 | private i18n: I18n | |
40 | ) {} | |
41 | ||
42 | ngOnInit () { | |
43 | this.user = this.authService.getUser() | |
44 | ||
45 | this.loadVideoPlaylists() | |
bf64ed41 RK |
46 | |
47 | this.videoPlaylistSearchChanged | |
48 | .pipe( | |
49 | debounceTime(500)) | |
50 | .subscribe(() => { | |
9d45db29 | 51 | this.loadVideoPlaylists(true) |
bf64ed41 | 52 | }) |
830b4faf C |
53 | } |
54 | ||
55 | async deleteVideoPlaylist (videoPlaylist: VideoPlaylist) { | |
56 | const res = await this.confirmService.confirm( | |
57 | this.i18n( | |
58 | 'Do you really want to delete {{playlistDisplayName}}?', | |
59 | { playlistDisplayName: videoPlaylist.displayName } | |
60 | ), | |
61 | this.i18n('Delete') | |
62 | ) | |
63 | if (res === false) return | |
64 | ||
65 | this.videoPlaylistService.removeVideoPlaylist(videoPlaylist) | |
66 | .subscribe( | |
67 | () => { | |
68 | this.videoPlaylists = this.videoPlaylists | |
69 | .filter(p => p.id !== videoPlaylist.id) | |
70 | ||
71 | this.notifier.success( | |
72 | this.i18n('Playlist {{playlistDisplayName}} deleted.', { playlistDisplayName: videoPlaylist.displayName }) | |
73 | ) | |
74 | }, | |
75 | ||
76 | error => this.notifier.error(error.message) | |
77 | ) | |
78 | } | |
79 | ||
80 | isRegularPlaylist (playlist: VideoPlaylist) { | |
81 | return playlist.type.id === VideoPlaylistType.REGULAR | |
82 | } | |
83 | ||
f0a39880 | 84 | onNearOfBottom () { |
830b4faf C |
85 | // Last page |
86 | if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return | |
87 | ||
88 | this.pagination.currentPage += 1 | |
89 | this.loadVideoPlaylists() | |
90 | } | |
f0a39880 | 91 | |
bf64ed41 RK |
92 | onVideoPlaylistSearchChanged () { |
93 | this.videoPlaylistSearchChanged.next() | |
94 | } | |
95 | ||
9d45db29 | 96 | private loadVideoPlaylists (reset = false) { |
f0a39880 | 97 | this.authService.userInformationLoaded |
f3628e69 | 98 | .pipe(flatMap(() => { |
bf64ed41 | 99 | return this.videoPlaylistService.listAccountPlaylists(this.user.account, this.pagination, '-updatedAt', this.videoPlaylistsSearch) |
f3628e69 | 100 | })) |
f0a39880 | 101 | .subscribe(res => { |
9d45db29 | 102 | if (reset) this.videoPlaylists = [] |
f0a39880 C |
103 | this.videoPlaylists = this.videoPlaylists.concat(res.data) |
104 | this.pagination.totalItems = res.total | |
ad453580 C |
105 | |
106 | this.onDataSubject.next(res.data) | |
f0a39880 C |
107 | }) |
108 | } | |
830b4faf | 109 | } |