]>
Commit | Line | Data |
---|---|---|
db400f44 | 1 | import { map, switchMap } from 'rxjs/operators' |
db7af09b | 2 | import { Component, OnInit } from '@angular/core' |
df98563e | 3 | import { ActivatedRoute, Router } from '@angular/router' |
68e24d72 | 4 | import { LoadingBarService } from '@ngx-loading-bar/core' |
df98563e | 5 | import { NotificationsService } from 'angular2-notifications' |
ef4c78da | 6 | import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos' |
db7af09b | 7 | import { ServerService } from '../../core' |
15a7387d | 8 | import { AuthService } from '../../core/auth' |
4cc66133 | 9 | import { FormReactive } from '../../shared' |
202f6b6c | 10 | import { VideoEdit } from '../../shared/video/video-edit.model' |
4cc66133 | 11 | import { VideoService } from '../../shared/video/video.service' |
6200d8d9 | 12 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' |
b1d40cff | 13 | import { I18n } from '@ngx-translate/i18n-polyfill' |
d18d6478 | 14 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' |
40e87e9e | 15 | import { VideoCaptionService } from '@app/shared/video-caption' |
ef4c78da | 16 | import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model' |
1553e15d | 17 | |
dc8bc31b | 18 | @Component({ |
d8e689b8 | 19 | selector: 'my-videos-update', |
80958c78 | 20 | styleUrls: [ './shared/video-edit.component.scss' ], |
d8e689b8 | 21 | templateUrl: './video-update.component.html' |
dc8bc31b | 22 | }) |
d8e689b8 | 23 | export class VideoUpdateComponent extends FormReactive implements OnInit { |
404b54e1 | 24 | video: VideoEdit |
4b2f33f3 | 25 | |
68e24d72 | 26 | isUpdatingVideo = false |
ef4c78da C |
27 | videoPrivacies: VideoConstant<string>[] = [] |
28 | userVideoChannels: { id: number, label: string, support: string }[] = [] | |
bbe0f064 | 29 | schedulePublicationPossible = false |
ef4c78da | 30 | videoCaptions: VideoCaptionEdit[] = [] |
dc8bc31b | 31 | |
df98563e | 32 | constructor ( |
d18d6478 | 33 | protected formValidatorService: FormValidatorService, |
d8e689b8 | 34 | private route: ActivatedRoute, |
7ddd02c9 | 35 | private router: Router, |
6e07c3de | 36 | private notificationsService: NotificationsService, |
db7af09b | 37 | private serverService: ServerService, |
15a7387d | 38 | private videoService: VideoService, |
68e24d72 | 39 | private authService: AuthService, |
6200d8d9 | 40 | private loadingBar: LoadingBarService, |
b1d40cff | 41 | private videoChannelService: VideoChannelService, |
40e87e9e | 42 | private videoCaptionService: VideoCaptionService, |
b1d40cff | 43 | private i18n: I18n |
4b2f33f3 | 44 | ) { |
df98563e | 45 | super() |
4b2f33f3 | 46 | } |
dc8bc31b | 47 | |
df98563e | 48 | ngOnInit () { |
d18d6478 | 49 | this.buildForm({}) |
d8e689b8 | 50 | |
15a7387d | 51 | this.serverService.videoPrivaciesLoaded |
308c4275 | 52 | .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies()) |
15a7387d | 53 | |
308c4275 C |
54 | this.route.data |
55 | .pipe(map(data => data.videoData)) | |
56 | .subscribe(({ video, videoChannels, videoCaptions }) => { | |
57 | this.video = new VideoEdit(video) | |
58 | this.userVideoChannels = videoChannels | |
59 | this.videoCaptions = videoCaptions | |
db400f44 | 60 | |
308c4275 C |
61 | // We cannot set private a video that was not private |
62 | if (this.video.privacy !== VideoPrivacy.PRIVATE) { | |
63 | this.videoPrivacies = this.videoPrivacies.filter(p => p.id.toString() !== VideoPrivacy.PRIVATE.toString()) | |
64 | } else { // We can schedule video publication only if it it is private | |
65 | this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE | |
66 | } | |
fd45e8f4 | 67 | |
308c4275 C |
68 | // FIXME: Angular does not detec |
69 | setTimeout(() => this.hydrateFormFromVideo()) | |
70 | }, | |
2de96f4d | 71 | |
308c4275 C |
72 | err => { |
73 | console.error(err) | |
74 | this.notificationsService.error(this.i18n('Error'), err.message) | |
75 | } | |
76 | ) | |
e822fdae C |
77 | } |
78 | ||
df98563e C |
79 | checkForm () { |
80 | this.forceCheck() | |
c24ac1c1 | 81 | |
df98563e | 82 | return this.form.valid |
c24ac1c1 C |
83 | } |
84 | ||
df98563e | 85 | update () { |
c24ac1c1 | 86 | if (this.checkForm() === false) { |
df98563e | 87 | return |
c24ac1c1 C |
88 | } |
89 | ||
df98563e | 90 | this.video.patch(this.form.value) |
d8e689b8 | 91 | |
68e24d72 C |
92 | this.loadingBar.start() |
93 | this.isUpdatingVideo = true | |
40e87e9e C |
94 | |
95 | // Update the video | |
d8e689b8 | 96 | this.videoService.updateVideo(this.video) |
40e87e9e C |
97 | .pipe( |
98 | // Then update captions | |
99 | switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions)) | |
100 | ) | |
101 | .subscribe( | |
102 | () => { | |
103 | this.isUpdatingVideo = false | |
104 | this.loadingBar.complete() | |
105 | this.notificationsService.success(this.i18n('Success'), this.i18n('Video updated.')) | |
106 | this.router.navigate([ '/videos/watch', this.video.uuid ]) | |
107 | }, | |
108 | ||
109 | err => { | |
110 | this.isUpdatingVideo = false | |
111 | this.notificationsService.error(this.i18n('Error'), err.message) | |
112 | console.error(err) | |
113 | } | |
114 | ) | |
e822fdae | 115 | |
dc8bc31b | 116 | } |
e54163c2 | 117 | |
df98563e | 118 | private hydrateFormFromVideo () { |
bbe0f064 | 119 | this.form.patchValue(this.video.toFormPatch()) |
6de36768 C |
120 | |
121 | const objects = [ | |
122 | { | |
123 | url: 'thumbnailUrl', | |
124 | name: 'thumbnailfile' | |
125 | }, | |
126 | { | |
127 | url: 'previewUrl', | |
128 | name: 'previewfile' | |
129 | } | |
130 | ] | |
131 | ||
132 | for (const obj of objects) { | |
133 | fetch(this.video[obj.url]) | |
134 | .then(response => response.blob()) | |
135 | .then(data => { | |
136 | this.form.patchValue({ | |
137 | [ obj.name ]: data | |
138 | }) | |
139 | }) | |
140 | } | |
d8e689b8 | 141 | } |
dc8bc31b | 142 | } |