]>
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' | |
6 | import { flatMap } 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' | |
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 { | |
20 | videoPlaylists: VideoPlaylist[] = [] | |
21 | ||
22 | pagination: ComponentPagination = { | |
23 | currentPage: 1, | |
ad453580 | 24 | itemsPerPage: 5, |
830b4faf C |
25 | totalItems: null |
26 | } | |
27 | ||
ad453580 C |
28 | onDataSubject = new Subject<any[]>() |
29 | ||
830b4faf C |
30 | private user: User |
31 | ||
32 | constructor ( | |
33 | private authService: AuthService, | |
34 | private notifier: Notifier, | |
35 | private confirmService: ConfirmService, | |
36 | private videoPlaylistService: VideoPlaylistService, | |
37 | private i18n: I18n | |
38 | ) {} | |
39 | ||
40 | ngOnInit () { | |
41 | this.user = this.authService.getUser() | |
42 | ||
43 | this.loadVideoPlaylists() | |
44 | } | |
45 | ||
46 | async deleteVideoPlaylist (videoPlaylist: VideoPlaylist) { | |
47 | const res = await this.confirmService.confirm( | |
48 | this.i18n( | |
49 | 'Do you really want to delete {{playlistDisplayName}}?', | |
50 | { playlistDisplayName: videoPlaylist.displayName } | |
51 | ), | |
52 | this.i18n('Delete') | |
53 | ) | |
54 | if (res === false) return | |
55 | ||
56 | this.videoPlaylistService.removeVideoPlaylist(videoPlaylist) | |
57 | .subscribe( | |
58 | () => { | |
59 | this.videoPlaylists = this.videoPlaylists | |
60 | .filter(p => p.id !== videoPlaylist.id) | |
61 | ||
62 | this.notifier.success( | |
63 | this.i18n('Playlist {{playlistDisplayName}} deleted.', { playlistDisplayName: videoPlaylist.displayName }) | |
64 | ) | |
65 | }, | |
66 | ||
67 | error => this.notifier.error(error.message) | |
68 | ) | |
69 | } | |
70 | ||
71 | isRegularPlaylist (playlist: VideoPlaylist) { | |
72 | return playlist.type.id === VideoPlaylistType.REGULAR | |
73 | } | |
74 | ||
f0a39880 | 75 | onNearOfBottom () { |
830b4faf C |
76 | // Last page |
77 | if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return | |
78 | ||
79 | this.pagination.currentPage += 1 | |
80 | this.loadVideoPlaylists() | |
81 | } | |
f0a39880 C |
82 | |
83 | private loadVideoPlaylists () { | |
84 | this.authService.userInformationLoaded | |
f3628e69 C |
85 | .pipe(flatMap(() => { |
86 | return this.videoPlaylistService.listAccountPlaylists(this.user.account, this.pagination, '-updatedAt') | |
87 | })) | |
f0a39880 C |
88 | .subscribe(res => { |
89 | this.videoPlaylists = this.videoPlaylists.concat(res.data) | |
90 | this.pagination.totalItems = res.total | |
ad453580 C |
91 | |
92 | this.onDataSubject.next(res.data) | |
f0a39880 C |
93 | }) |
94 | } | |
830b4faf | 95 | } |