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 } 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 { I18n } from '@ngx-translate/i18n-polyfill'
9 import { VideoPrivacy } from '@shared/models'
12 selector: 'my-videos-update',
13 styleUrls: [ './shared/video-edit.component.scss' ],
14 templateUrl: './video-update.component.html'
16 export class VideoUpdateComponent extends FormReactive implements OnInit {
19 isUpdatingVideo = false
20 userVideoChannels: { id: number, label: string, support: string, avatar?: string }[] = []
21 schedulePublicationPossible = false
22 videoCaptions: VideoCaptionEdit[] = []
23 waitTranscodingEnabled = true
25 private updateDone = false
28 protected formValidatorService: FormValidatorService,
29 private route: ActivatedRoute,
30 private router: Router,
31 private notifier: Notifier,
32 private videoService: VideoService,
33 private loadingBar: LoadingBarService,
34 private videoCaptionService: VideoCaptionService,
44 .pipe(map(data => data.videoData))
45 .subscribe(({ video, videoChannels, videoCaptions }) => {
46 this.video = new VideoEdit(video)
47 this.userVideoChannels = videoChannels
48 this.videoCaptions = videoCaptions
50 this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
52 const videoFiles = (video as VideoDetails).getFiles()
53 if (videoFiles.length > 1) { // Already transcoded
54 this.waitTranscodingEnabled = false
57 // FIXME: Angular does not detect the change inside this subscription, so use the patched setTimeout
58 setTimeout(() => this.hydrateFormFromVideo())
63 this.notifier.error(err.message)
68 @HostListener('window:beforeunload', [ '$event' ])
69 onUnload (event: any) {
70 const { text, canDeactivate } = this.canDeactivate()
72 if (canDeactivate) return
74 event.returnValue = text
78 canDeactivate (): { canDeactivate: boolean, text?: string } {
79 if (this.updateDone === true) return { canDeactivate: true }
81 const text = this.i18n('You have unsaved changes! If you leave, your changes will be lost.')
83 for (const caption of this.videoCaptions) {
84 if (caption.action) return { canDeactivate: false, text }
87 return { canDeactivate: this.formChanged === false, text }
93 return this.form.valid
97 if (this.checkForm() === false
98 || this.isUpdatingVideo === true) {
102 this.video.patch(this.form.value)
104 this.loadingBar.useRef().start()
105 this.isUpdatingVideo = true
108 this.videoService.updateVideo(this.video)
110 // Then update captions
111 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions))
115 this.updateDone = true
116 this.isUpdatingVideo = false
117 this.loadingBar.useRef().complete()
118 this.notifier.success(this.i18n('Video updated.'))
119 this.router.navigate([ '/videos/watch', this.video.uuid ])
123 this.loadingBar.useRef().complete()
124 this.isUpdatingVideo = false
125 this.notifier.error(err.message)
131 private hydrateFormFromVideo () {
132 this.form.patchValue(this.video.toFormPatch())
137 name: 'thumbnailfile'
145 for (const obj of objects) {
146 fetch(this.video[obj.url])
147 .then(response => response.blob())
149 this.form.patchValue({