]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-edit/video-update.component.ts
Try to improve tools doc
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-edit / video-update.component.ts
CommitLineData
f8c00564 1import { of } from 'rxjs'
db400f44 2import { map, switchMap } from 'rxjs/operators'
674a66bb 3import { Component, HostListener, OnInit } from '@angular/core'
df98563e 4import { ActivatedRoute, Router } from '@angular/router'
f8b2c1b4 5import { Notifier } from '@app/core'
52c4976f 6import { FormReactive, FormValidatorService, SelectChannelItem } from '@app/shared/shared-forms'
f8c00564
C
7import { VideoCaptionEdit, VideoCaptionService, VideoDetails, VideoEdit, VideoService } from '@app/shared/shared-main'
8import { LiveVideoService } from '@app/shared/shared-video-live'
67ed6552 9import { LoadingBarService } from '@ngx-loading-bar/core'
b5b68755 10import { LiveVideo, LiveVideoUpdate, VideoPrivacy } from '@shared/models'
c6c0fa6c 11import { hydrateFormFromVideo } from './shared/video-edit-utils'
1553e15d 12
dc8bc31b 13@Component({
d8e689b8 14 selector: 'my-videos-update',
80958c78 15 styleUrls: [ './shared/video-edit.component.scss' ],
d8e689b8 16 templateUrl: './video-update.component.html'
dc8bc31b 17})
d8e689b8 18export class VideoUpdateComponent extends FormReactive implements OnInit {
404b54e1 19 video: VideoEdit
c6c0fa6c
C
20 userVideoChannels: SelectChannelItem[] = []
21 videoCaptions: VideoCaptionEdit[] = []
a5cf76af 22 liveVideo: LiveVideo
4b2f33f3 23
68e24d72 24 isUpdatingVideo = false
bbe0f064 25 schedulePublicationPossible = false
14e2014a 26 waitTranscodingEnabled = true
dc8bc31b 27
772d5642
C
28 private updateDone = false
29
df98563e 30 constructor (
d18d6478 31 protected formValidatorService: FormValidatorService,
d8e689b8 32 private route: ActivatedRoute,
7ddd02c9 33 private router: Router,
f8b2c1b4 34 private notifier: Notifier,
15a7387d 35 private videoService: VideoService,
6200d8d9 36 private loadingBar: LoadingBarService,
b5b68755
C
37 private videoCaptionService: VideoCaptionService,
38 private liveVideoService: LiveVideoService
66357162 39 ) {
df98563e 40 super()
4b2f33f3 41 }
dc8bc31b 42
df98563e 43 ngOnInit () {
d18d6478 44 this.buildForm({})
d8e689b8 45
308c4275
C
46 this.route.data
47 .pipe(map(data => data.videoData))
a5cf76af 48 .subscribe(({ video, videoChannels, videoCaptions, liveVideo }) => {
308c4275
C
49 this.video = new VideoEdit(video)
50 this.userVideoChannels = videoChannels
51 this.videoCaptions = videoCaptions
a5cf76af 52 this.liveVideo = liveVideo
db400f44 53
12bec528
C
54 this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
55
5a71acd2 56 const videoFiles = (video as VideoDetails).getFiles()
14e2014a
C
57 if (videoFiles.length > 1) { // Already transcoded
58 this.waitTranscodingEnabled = false
59 }
60
772d5642 61 // FIXME: Angular does not detect the change inside this subscription, so use the patched setTimeout
b5b68755
C
62 setTimeout(() => {
63 hydrateFormFromVideo(this.form, this.video, true)
64
65 if (this.liveVideo) {
66 this.form.patchValue({
bb4ba6d9
C
67 saveReplay: this.liveVideo.saveReplay,
68 permanentLive: this.liveVideo.permanentLive
b5b68755
C
69 })
70 }
71 })
308c4275 72 },
2de96f4d 73
308c4275
C
74 err => {
75 console.error(err)
f8b2c1b4 76 this.notifier.error(err.message)
308c4275
C
77 }
78 )
e822fdae
C
79 }
80
674a66bb
C
81 @HostListener('window:beforeunload', [ '$event' ])
82 onUnload (event: any) {
83 const { text, canDeactivate } = this.canDeactivate()
84
85 if (canDeactivate) return
86
87 event.returnValue = text
88 return text
89 }
90
91 canDeactivate (): { canDeactivate: boolean, text?: string } {
772d5642
C
92 if (this.updateDone === true) return { canDeactivate: true }
93
66357162 94 const text = $localize`You have unsaved changes! If you leave, your changes will be lost.`
674a66bb 95
772d5642 96 for (const caption of this.videoCaptions) {
674a66bb 97 if (caption.action) return { canDeactivate: false, text }
772d5642
C
98 }
99
674a66bb 100 return { canDeactivate: this.formChanged === false, text }
772d5642
C
101 }
102
df98563e
C
103 checkForm () {
104 this.forceCheck()
c24ac1c1 105
df98563e 106 return this.form.valid
c24ac1c1
C
107 }
108
df98563e 109 update () {
8c2b9756
RK
110 if (this.checkForm() === false
111 || this.isUpdatingVideo === true) {
df98563e 112 return
c24ac1c1
C
113 }
114
df98563e 115 this.video.patch(this.form.value)
d8e689b8 116
b5b68755 117 const liveVideoUpdate: LiveVideoUpdate = {
bb4ba6d9
C
118 saveReplay: this.form.value.saveReplay,
119 permanentLive: this.form.value.permanentLive
b5b68755
C
120 }
121
a02b93ce 122 this.loadingBar.useRef().start()
68e24d72 123 this.isUpdatingVideo = true
40e87e9e
C
124
125 // Update the video
d8e689b8 126 this.videoService.updateVideo(this.video)
40e87e9e
C
127 .pipe(
128 // Then update captions
b5b68755
C
129 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions)),
130
131 switchMap(() => {
132 if (!this.liveVideo) return of(undefined)
133
134 return this.liveVideoService.updateLive(this.video.id, liveVideoUpdate)
135 })
40e87e9e
C
136 )
137 .subscribe(
138 () => {
772d5642 139 this.updateDone = true
40e87e9e 140 this.isUpdatingVideo = false
a02b93ce 141 this.loadingBar.useRef().complete()
66357162 142 this.notifier.success($localize`Video updated.`)
40e87e9e
C
143 this.router.navigate([ '/videos/watch', this.video.uuid ])
144 },
145
146 err => {
a02b93ce 147 this.loadingBar.useRef().complete()
40e87e9e 148 this.isUpdatingVideo = false
f8b2c1b4 149 this.notifier.error(err.message)
40e87e9e
C
150 console.error(err)
151 }
152 )
dc8bc31b 153 }
e54163c2 154
7294aab0
C
155 hydratePluginFieldsFromVideo () {
156 if (!this.video.pluginData) return
157
158 this.form.patchValue({
159 pluginData: this.video.pluginData
160 })
161 }
dc8bc31b 162}