]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { forkJoin, Subscription } from 'rxjs' |
2 | import { map, switchMap } from 'rxjs/operators' | |
830b4faf C |
3 | import { Component, OnDestroy, OnInit } from '@angular/core' |
4 | import { ActivatedRoute, Router } from '@angular/router' | |
5 | import { AuthService, Notifier, ServerService } from '@app/core' | |
67ed6552 C |
6 | import { populateAsyncUserVideoChannels } from '@app/helpers' |
7 | import { FormValidatorService, VideoPlaylistValidatorsService } from '@app/shared/shared-forms' | |
8 | import { VideoPlaylist, VideoPlaylistService } from '@app/shared/shared-video-playlist' | |
830b4faf | 9 | import { I18n } from '@ngx-translate/i18n-polyfill' |
67ed6552 C |
10 | import { VideoPlaylistUpdate } from '@shared/models' |
11 | import { MyAccountVideoPlaylistEdit } from './my-account-video-playlist-edit' | |
830b4faf C |
12 | |
13 | @Component({ | |
14 | selector: 'my-account-video-playlist-update', | |
15 | templateUrl: './my-account-video-playlist-edit.component.html', | |
16 | styleUrls: [ './my-account-video-playlist-edit.component.scss' ] | |
17 | }) | |
18 | export class MyAccountVideoPlaylistUpdateComponent extends MyAccountVideoPlaylistEdit implements OnInit, OnDestroy { | |
19 | error: string | |
20 | videoPlaylistToUpdate: VideoPlaylist | |
830b4faf C |
21 | |
22 | private paramsSub: Subscription | |
23 | ||
24 | constructor ( | |
25 | protected formValidatorService: FormValidatorService, | |
26 | private authService: AuthService, | |
27 | private videoPlaylistValidatorsService: VideoPlaylistValidatorsService, | |
28 | private notifier: Notifier, | |
29 | private router: Router, | |
30 | private route: ActivatedRoute, | |
31 | private videoPlaylistService: VideoPlaylistService, | |
32 | private i18n: I18n, | |
33 | private serverService: ServerService | |
34 | ) { | |
35 | super() | |
36 | } | |
37 | ||
38 | ngOnInit () { | |
39 | this.buildForm({ | |
978c9d49 | 40 | displayName: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_DISPLAY_NAME, |
830b4faf C |
41 | privacy: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_PRIVACY, |
42 | description: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_DESCRIPTION, | |
43 | videoChannelId: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_CHANNEL_ID, | |
44 | thumbnailfile: null | |
45 | }) | |
46 | ||
978c9d49 C |
47 | this.form.get('privacy').valueChanges.subscribe(privacy => { |
48 | this.videoPlaylistValidatorsService.setChannelValidator(this.form.get('videoChannelId'), privacy) | |
49 | }) | |
50 | ||
830b4faf | 51 | populateAsyncUserVideoChannels(this.authService, this.userVideoChannels) |
851f5daa | 52 | .catch(err => console.error('Cannot populate user video channels.', err)) |
830b4faf | 53 | |
851f5daa C |
54 | this.paramsSub = this.route.params |
55 | .pipe( | |
56 | map(routeParams => routeParams['videoPlaylistId']), | |
ba430d75 C |
57 | switchMap(videoPlaylistId => { |
58 | return forkJoin([ | |
59 | this.videoPlaylistService.getVideoPlaylist(videoPlaylistId), | |
60 | this.serverService.getVideoPlaylistPrivacies() | |
61 | ]) | |
62 | }) | |
851f5daa C |
63 | ) |
64 | .subscribe( | |
ba430d75 | 65 | ([ videoPlaylistToUpdate, videoPlaylistPrivacies]) => { |
851f5daa | 66 | this.videoPlaylistToUpdate = videoPlaylistToUpdate |
ba430d75 | 67 | this.videoPlaylistPrivacies = videoPlaylistPrivacies |
830b4faf | 68 | |
851f5daa C |
69 | this.hydrateFormFromPlaylist() |
70 | }, | |
830b4faf | 71 | |
851f5daa C |
72 | err => this.error = err.message |
73 | ) | |
830b4faf C |
74 | } |
75 | ||
76 | ngOnDestroy () { | |
77 | if (this.paramsSub) this.paramsSub.unsubscribe() | |
78 | } | |
79 | ||
80 | formValidated () { | |
81 | this.error = undefined | |
82 | ||
83 | const body = this.form.value | |
84 | const videoPlaylistUpdate: VideoPlaylistUpdate = { | |
978c9d49 C |
85 | displayName: body.displayName, |
86 | privacy: body.privacy, | |
830b4faf C |
87 | description: body.description || null, |
88 | videoChannelId: body.videoChannelId || null, | |
89 | thumbnailfile: body.thumbnailfile || undefined | |
90 | } | |
91 | ||
92 | this.videoPlaylistService.updateVideoPlaylist(this.videoPlaylistToUpdate, videoPlaylistUpdate).subscribe( | |
93 | () => { | |
94 | this.notifier.success( | |
95 | this.i18n('Playlist {{videoPlaylistName}} updated.', { videoPlaylistName: videoPlaylistUpdate.displayName }) | |
96 | ) | |
97 | ||
98 | this.router.navigate([ '/my-account', 'video-playlists' ]) | |
99 | }, | |
100 | ||
101 | err => this.error = err.message | |
102 | ) | |
103 | } | |
104 | ||
105 | isCreation () { | |
106 | return false | |
107 | } | |
108 | ||
109 | getFormButtonTitle () { | |
110 | return this.i18n('Update') | |
111 | } | |
112 | ||
113 | private hydrateFormFromPlaylist () { | |
114 | this.form.patchValue({ | |
978c9d49 | 115 | displayName: this.videoPlaylistToUpdate.displayName, |
830b4faf C |
116 | privacy: this.videoPlaylistToUpdate.privacy.id, |
117 | description: this.videoPlaylistToUpdate.description, | |
118 | videoChannelId: this.videoPlaylistToUpdate.videoChannel ? this.videoPlaylistToUpdate.videoChannel.id : null | |
119 | }) | |
120 | ||
121 | fetch(this.videoPlaylistToUpdate.thumbnailUrl) | |
122 | .then(response => response.blob()) | |
123 | .then(data => { | |
124 | this.form.patchValue({ | |
125 | thumbnailfile: data | |
126 | }) | |
127 | }) | |
128 | } | |
129 | } |