1 import { map, switchMap } from 'rxjs/operators'
2 import { Component, HostListener, OnInit } from '@angular/core'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { Notifier } from '@app/core'
5 import { FormReactive, FormValidatorService, SelectChannelItem } from '@app/shared/shared-forms'
6 import { VideoCaptionEdit, VideoCaptionService, VideoDetails, VideoEdit, VideoService } from '@app/shared/shared-main'
7 import { LoadingBarService } from '@ngx-loading-bar/core'
8 import { VideoPrivacy } from '@shared/models'
11 selector: 'my-videos-update',
12 styleUrls: [ './shared/video-edit.component.scss' ],
13 templateUrl: './video-update.component.html'
15 export class VideoUpdateComponent extends FormReactive implements OnInit {
18 isUpdatingVideo = false
19 userVideoChannels: SelectChannelItem[] = []
20 schedulePublicationPossible = false
21 videoCaptions: VideoCaptionEdit[] = []
22 waitTranscodingEnabled = true
24 private updateDone = false
27 protected formValidatorService: FormValidatorService,
28 private route: ActivatedRoute,
29 private router: Router,
30 private notifier: Notifier,
31 private videoService: VideoService,
32 private loadingBar: LoadingBarService,
33 private videoCaptionService: VideoCaptionService
42 .pipe(map(data => data.videoData))
43 .subscribe(({ video, videoChannels, videoCaptions }) => {
44 this.video = new VideoEdit(video)
45 this.userVideoChannels = videoChannels
46 this.videoCaptions = videoCaptions
48 this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
50 const videoFiles = (video as VideoDetails).getFiles()
51 if (videoFiles.length > 1) { // Already transcoded
52 this.waitTranscodingEnabled = false
55 // FIXME: Angular does not detect the change inside this subscription, so use the patched setTimeout
56 setTimeout(() => this.hydrateFormFromVideo())
61 this.notifier.error(err.message)
66 @HostListener('window:beforeunload', [ '$event' ])
67 onUnload (event: any) {
68 const { text, canDeactivate } = this.canDeactivate()
70 if (canDeactivate) return
72 event.returnValue = text
76 canDeactivate (): { canDeactivate: boolean, text?: string } {
77 if (this.updateDone === true) return { canDeactivate: true }
79 const text = $localize`You have unsaved changes! If you leave, your changes will be lost.`
81 for (const caption of this.videoCaptions) {
82 if (caption.action) return { canDeactivate: false, text }
85 return { canDeactivate: this.formChanged === false, text }
91 return this.form.valid
95 if (this.checkForm() === false
96 || this.isUpdatingVideo === true) {
100 this.video.patch(this.form.value)
102 this.loadingBar.useRef().start()
103 this.isUpdatingVideo = true
106 this.videoService.updateVideo(this.video)
108 // Then update captions
109 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions))
113 this.updateDone = true
114 this.isUpdatingVideo = false
115 this.loadingBar.useRef().complete()
116 this.notifier.success($localize`Video updated.`)
117 this.router.navigate([ '/videos/watch', this.video.uuid ])
121 this.loadingBar.useRef().complete()
122 this.isUpdatingVideo = false
123 this.notifier.error(err.message)
129 hydratePluginFieldsFromVideo () {
130 if (!this.video.pluginData) return
132 this.form.patchValue({
133 pluginData: this.video.pluginData
137 private hydrateFormFromVideo () {
138 this.form.patchValue(this.video.toFormPatch())
143 name: 'thumbnailfile'
151 for (const obj of objects) {
152 fetch(this.video[obj.url])
153 .then(response => response.blob())
155 this.form.patchValue({