]>
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' | |
d0800f76 | 6 | import { listUserChannelsForSelect } from '@app/helpers' |
7ed1edbb C |
7 | import { |
8 | setPlaylistChannelValidator, | |
9 | VIDEO_PLAYLIST_CHANNEL_ID_VALIDATOR, | |
10 | VIDEO_PLAYLIST_DESCRIPTION_VALIDATOR, | |
11 | VIDEO_PLAYLIST_DISPLAY_NAME_VALIDATOR, | |
12 | VIDEO_PLAYLIST_PRIVACY_VALIDATOR | |
13 | } from '@app/shared/form-validators/video-playlist-validators' | |
14 | import { FormValidatorService } from '@app/shared/shared-forms' | |
67ed6552 | 15 | import { VideoPlaylist, VideoPlaylistService } from '@app/shared/shared-video-playlist' |
67ed6552 | 16 | import { VideoPlaylistUpdate } from '@shared/models' |
17119e4a | 17 | import { MyVideoPlaylistEdit } from './my-video-playlist-edit' |
830b4faf C |
18 | |
19 | @Component({ | |
17119e4a C |
20 | templateUrl: './my-video-playlist-edit.component.html', |
21 | styleUrls: [ './my-video-playlist-edit.component.scss' ] | |
830b4faf | 22 | }) |
17119e4a | 23 | export class MyVideoPlaylistUpdateComponent extends MyVideoPlaylistEdit implements OnInit, OnDestroy { |
830b4faf C |
24 | error: string |
25 | videoPlaylistToUpdate: VideoPlaylist | |
830b4faf C |
26 | |
27 | private paramsSub: Subscription | |
28 | ||
29 | constructor ( | |
30 | protected formValidatorService: FormValidatorService, | |
31 | private authService: AuthService, | |
830b4faf C |
32 | private notifier: Notifier, |
33 | private router: Router, | |
34 | private route: ActivatedRoute, | |
35 | private videoPlaylistService: VideoPlaylistService, | |
830b4faf C |
36 | private serverService: ServerService |
37 | ) { | |
38 | super() | |
39 | } | |
40 | ||
41 | ngOnInit () { | |
42 | this.buildForm({ | |
7ed1edbb C |
43 | displayName: VIDEO_PLAYLIST_DISPLAY_NAME_VALIDATOR, |
44 | privacy: VIDEO_PLAYLIST_PRIVACY_VALIDATOR, | |
45 | description: VIDEO_PLAYLIST_DESCRIPTION_VALIDATOR, | |
46 | videoChannelId: VIDEO_PLAYLIST_CHANNEL_ID_VALIDATOR, | |
830b4faf C |
47 | thumbnailfile: null |
48 | }) | |
49 | ||
978c9d49 | 50 | this.form.get('privacy').valueChanges.subscribe(privacy => { |
7ed1edbb | 51 | setPlaylistChannelValidator(this.form.get('videoChannelId'), privacy) |
978c9d49 C |
52 | }) |
53 | ||
d0800f76 | 54 | listUserChannelsForSelect(this.authService) |
9556ce48 | 55 | .subscribe(channels => this.userVideoChannels = channels) |
830b4faf | 56 | |
851f5daa C |
57 | this.paramsSub = this.route.params |
58 | .pipe( | |
59 | map(routeParams => routeParams['videoPlaylistId']), | |
ba430d75 C |
60 | switchMap(videoPlaylistId => { |
61 | return forkJoin([ | |
62 | this.videoPlaylistService.getVideoPlaylist(videoPlaylistId), | |
63 | this.serverService.getVideoPlaylistPrivacies() | |
64 | ]) | |
65 | }) | |
851f5daa | 66 | ) |
1378c0d3 | 67 | .subscribe({ |
9df52d66 | 68 | next: ([ videoPlaylistToUpdate, videoPlaylistPrivacies ]) => { |
851f5daa | 69 | this.videoPlaylistToUpdate = videoPlaylistToUpdate |
ba430d75 | 70 | this.videoPlaylistPrivacies = videoPlaylistPrivacies |
830b4faf | 71 | |
851f5daa C |
72 | this.hydrateFormFromPlaylist() |
73 | }, | |
830b4faf | 74 | |
9df52d66 C |
75 | error: err => { |
76 | this.error = err.message | |
77 | } | |
1378c0d3 | 78 | }) |
830b4faf C |
79 | } |
80 | ||
81 | ngOnDestroy () { | |
82 | if (this.paramsSub) this.paramsSub.unsubscribe() | |
83 | } | |
84 | ||
85 | formValidated () { | |
86 | this.error = undefined | |
87 | ||
88 | const body = this.form.value | |
89 | const videoPlaylistUpdate: VideoPlaylistUpdate = { | |
978c9d49 C |
90 | displayName: body.displayName, |
91 | privacy: body.privacy, | |
830b4faf C |
92 | description: body.description || null, |
93 | videoChannelId: body.videoChannelId || null, | |
94 | thumbnailfile: body.thumbnailfile || undefined | |
95 | } | |
96 | ||
1378c0d3 C |
97 | this.videoPlaylistService.updateVideoPlaylist(this.videoPlaylistToUpdate, videoPlaylistUpdate) |
98 | .subscribe({ | |
99 | next: () => { | |
100 | this.notifier.success($localize`Playlist ${videoPlaylistUpdate.displayName} updated.`) | |
101 | this.router.navigate([ '/my-library', 'video-playlists' ]) | |
102 | }, | |
830b4faf | 103 | |
9df52d66 C |
104 | error: err => { |
105 | this.error = err.message | |
106 | } | |
1378c0d3 | 107 | }) |
830b4faf C |
108 | } |
109 | ||
110 | isCreation () { | |
111 | return false | |
112 | } | |
113 | ||
114 | getFormButtonTitle () { | |
66357162 | 115 | return $localize`Update` |
830b4faf C |
116 | } |
117 | ||
118 | private hydrateFormFromPlaylist () { | |
119 | this.form.patchValue({ | |
978c9d49 | 120 | displayName: this.videoPlaylistToUpdate.displayName, |
830b4faf C |
121 | privacy: this.videoPlaylistToUpdate.privacy.id, |
122 | description: this.videoPlaylistToUpdate.description, | |
123 | videoChannelId: this.videoPlaylistToUpdate.videoChannel ? this.videoPlaylistToUpdate.videoChannel.id : null | |
124 | }) | |
125 | ||
126 | fetch(this.videoPlaylistToUpdate.thumbnailUrl) | |
127 | .then(response => response.blob()) | |
128 | .then(data => { | |
129 | this.form.patchValue({ | |
130 | thumbnailfile: data | |
131 | }) | |
132 | }) | |
133 | } | |
134 | } |