]>
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 | |
772d5642 C |
32 | private updateDone = false |
33 | ||
df98563e | 34 | constructor ( |
d18d6478 | 35 | protected formValidatorService: FormValidatorService, |
d8e689b8 | 36 | private route: ActivatedRoute, |
7ddd02c9 | 37 | private router: Router, |
6e07c3de | 38 | private notificationsService: NotificationsService, |
db7af09b | 39 | private serverService: ServerService, |
15a7387d | 40 | private videoService: VideoService, |
68e24d72 | 41 | private authService: AuthService, |
6200d8d9 | 42 | private loadingBar: LoadingBarService, |
b1d40cff | 43 | private videoChannelService: VideoChannelService, |
40e87e9e | 44 | private videoCaptionService: VideoCaptionService, |
b1d40cff | 45 | private i18n: I18n |
4b2f33f3 | 46 | ) { |
df98563e | 47 | super() |
4b2f33f3 | 48 | } |
dc8bc31b | 49 | |
df98563e | 50 | ngOnInit () { |
d18d6478 | 51 | this.buildForm({}) |
d8e689b8 | 52 | |
15a7387d | 53 | this.serverService.videoPrivaciesLoaded |
308c4275 | 54 | .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies()) |
15a7387d | 55 | |
308c4275 C |
56 | this.route.data |
57 | .pipe(map(data => data.videoData)) | |
58 | .subscribe(({ video, videoChannels, videoCaptions }) => { | |
59 | this.video = new VideoEdit(video) | |
60 | this.userVideoChannels = videoChannels | |
61 | this.videoCaptions = videoCaptions | |
db400f44 | 62 | |
308c4275 C |
63 | // We cannot set private a video that was not private |
64 | if (this.video.privacy !== VideoPrivacy.PRIVATE) { | |
65 | this.videoPrivacies = this.videoPrivacies.filter(p => p.id.toString() !== VideoPrivacy.PRIVATE.toString()) | |
66 | } else { // We can schedule video publication only if it it is private | |
67 | this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE | |
68 | } | |
fd45e8f4 | 69 | |
772d5642 | 70 | // FIXME: Angular does not detect the change inside this subscription, so use the patched setTimeout |
308c4275 C |
71 | setTimeout(() => this.hydrateFormFromVideo()) |
72 | }, | |
2de96f4d | 73 | |
308c4275 C |
74 | err => { |
75 | console.error(err) | |
76 | this.notificationsService.error(this.i18n('Error'), err.message) | |
77 | } | |
78 | ) | |
e822fdae C |
79 | } |
80 | ||
772d5642 C |
81 | canDeactivate () { |
82 | if (this.updateDone === true) return { canDeactivate: true } | |
83 | ||
84 | for (const caption of this.videoCaptions) { | |
85 | if (caption.action) return { canDeactivate: false } | |
86 | } | |
87 | ||
88 | return { canDeactivate: this.formChanged === false } | |
89 | } | |
90 | ||
df98563e C |
91 | checkForm () { |
92 | this.forceCheck() | |
c24ac1c1 | 93 | |
df98563e | 94 | return this.form.valid |
c24ac1c1 C |
95 | } |
96 | ||
df98563e | 97 | update () { |
c24ac1c1 | 98 | if (this.checkForm() === false) { |
df98563e | 99 | return |
c24ac1c1 C |
100 | } |
101 | ||
df98563e | 102 | this.video.patch(this.form.value) |
d8e689b8 | 103 | |
68e24d72 C |
104 | this.loadingBar.start() |
105 | this.isUpdatingVideo = true | |
40e87e9e C |
106 | |
107 | // Update the video | |
d8e689b8 | 108 | this.videoService.updateVideo(this.video) |
40e87e9e C |
109 | .pipe( |
110 | // Then update captions | |
111 | switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions)) | |
112 | ) | |
113 | .subscribe( | |
114 | () => { | |
772d5642 | 115 | this.updateDone = true |
40e87e9e C |
116 | this.isUpdatingVideo = false |
117 | this.loadingBar.complete() | |
118 | this.notificationsService.success(this.i18n('Success'), this.i18n('Video updated.')) | |
119 | this.router.navigate([ '/videos/watch', this.video.uuid ]) | |
120 | }, | |
121 | ||
122 | err => { | |
0f7fedc3 | 123 | this.loadingBar.complete() |
40e87e9e C |
124 | this.isUpdatingVideo = false |
125 | this.notificationsService.error(this.i18n('Error'), err.message) | |
126 | console.error(err) | |
127 | } | |
128 | ) | |
dc8bc31b | 129 | } |
e54163c2 | 130 | |
df98563e | 131 | private hydrateFormFromVideo () { |
bbe0f064 | 132 | this.form.patchValue(this.video.toFormPatch()) |
6de36768 C |
133 | |
134 | const objects = [ | |
135 | { | |
136 | url: 'thumbnailUrl', | |
137 | name: 'thumbnailfile' | |
138 | }, | |
139 | { | |
140 | url: 'previewUrl', | |
141 | name: 'previewfile' | |
142 | } | |
143 | ] | |
144 | ||
145 | for (const obj of objects) { | |
146 | fetch(this.video[obj.url]) | |
147 | .then(response => response.blob()) | |
148 | .then(data => { | |
149 | this.form.patchValue({ | |
150 | [ obj.name ]: data | |
151 | }) | |
152 | }) | |
153 | } | |
d8e689b8 | 154 | } |
dc8bc31b | 155 | } |