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