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 { LiveVideoService, VideoCaptionEdit, VideoCaptionService, VideoDetails, VideoEdit, VideoService } from '@app/shared/shared-main'
7 import { LoadingBarService } from '@ngx-loading-bar/core'
8 import { LiveVideo, LiveVideoUpdate, VideoPrivacy } from '@shared/models'
9 import { hydrateFormFromVideo } from './shared/video-edit-utils'
10 import { of } from 'rxjs'
13 selector: 'my-videos-update',
14 styleUrls: [ './shared/video-edit.component.scss' ],
15 templateUrl: './video-update.component.html'
17 export class VideoUpdateComponent extends FormReactive implements OnInit {
19 userVideoChannels: SelectChannelItem[] = []
20 videoCaptions: VideoCaptionEdit[] = []
23 isUpdatingVideo = false
24 schedulePublicationPossible = false
25 waitTranscodingEnabled = true
27 private updateDone = false
30 protected formValidatorService: FormValidatorService,
31 private route: ActivatedRoute,
32 private router: Router,
33 private notifier: Notifier,
34 private videoService: VideoService,
35 private loadingBar: LoadingBarService,
36 private videoCaptionService: VideoCaptionService,
37 private liveVideoService: LiveVideoService
46 .pipe(map(data => data.videoData))
47 .subscribe(({ video, videoChannels, videoCaptions, liveVideo }) => {
48 this.video = new VideoEdit(video)
49 this.userVideoChannels = videoChannels
50 this.videoCaptions = videoCaptions
51 this.liveVideo = liveVideo
53 this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
55 const videoFiles = (video as VideoDetails).getFiles()
56 if (videoFiles.length > 1) { // Already transcoded
57 this.waitTranscodingEnabled = false
60 // FIXME: Angular does not detect the change inside this subscription, so use the patched setTimeout
62 hydrateFormFromVideo(this.form, this.video, true)
65 this.form.patchValue({
66 saveReplay: this.liveVideo.saveReplay
74 this.notifier.error(err.message)
79 @HostListener('window:beforeunload', [ '$event' ])
80 onUnload (event: any) {
81 const { text, canDeactivate } = this.canDeactivate()
83 if (canDeactivate) return
85 event.returnValue = text
89 canDeactivate (): { canDeactivate: boolean, text?: string } {
90 if (this.updateDone === true) return { canDeactivate: true }
92 const text = $localize`You have unsaved changes! If you leave, your changes will be lost.`
94 for (const caption of this.videoCaptions) {
95 if (caption.action) return { canDeactivate: false, text }
98 return { canDeactivate: this.formChanged === false, text }
104 return this.form.valid
108 if (this.checkForm() === false
109 || this.isUpdatingVideo === true) {
113 this.video.patch(this.form.value)
115 const liveVideoUpdate: LiveVideoUpdate = {
116 saveReplay: this.form.value.saveReplay
119 this.loadingBar.useRef().start()
120 this.isUpdatingVideo = true
123 this.videoService.updateVideo(this.video)
125 // Then update captions
126 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions)),
129 if (!this.liveVideo) return of(undefined)
131 return this.liveVideoService.updateLive(this.video.id, liveVideoUpdate)
136 this.updateDone = true
137 this.isUpdatingVideo = false
138 this.loadingBar.useRef().complete()
139 this.notifier.success($localize`Video updated.`)
140 this.router.navigate([ '/videos/watch', this.video.uuid ])
144 this.loadingBar.useRef().complete()
145 this.isUpdatingVideo = false
146 this.notifier.error(err.message)
152 hydratePluginFieldsFromVideo () {
153 if (!this.video.pluginData) return
155 this.form.patchValue({
156 pluginData: this.video.pluginData