]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-edit/video-update.component.ts
Add live info in watch page
[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({
67 saveReplay: this.liveVideo.saveReplay
68 })
69 }
70 })
308c4275 71 },
2de96f4d 72
308c4275
C
73 err => {
74 console.error(err)
f8b2c1b4 75 this.notifier.error(err.message)
308c4275
C
76 }
77 )
e822fdae
C
78 }
79
674a66bb
C
80 @HostListener('window:beforeunload', [ '$event' ])
81 onUnload (event: any) {
82 const { text, canDeactivate } = this.canDeactivate()
83
84 if (canDeactivate) return
85
86 event.returnValue = text
87 return text
88 }
89
90 canDeactivate (): { canDeactivate: boolean, text?: string } {
772d5642
C
91 if (this.updateDone === true) return { canDeactivate: true }
92
66357162 93 const text = $localize`You have unsaved changes! If you leave, your changes will be lost.`
674a66bb 94
772d5642 95 for (const caption of this.videoCaptions) {
674a66bb 96 if (caption.action) return { canDeactivate: false, text }
772d5642
C
97 }
98
674a66bb 99 return { canDeactivate: this.formChanged === false, text }
772d5642
C
100 }
101
df98563e
C
102 checkForm () {
103 this.forceCheck()
c24ac1c1 104
df98563e 105 return this.form.valid
c24ac1c1
C
106 }
107
df98563e 108 update () {
8c2b9756
RK
109 if (this.checkForm() === false
110 || this.isUpdatingVideo === true) {
df98563e 111 return
c24ac1c1
C
112 }
113
df98563e 114 this.video.patch(this.form.value)
d8e689b8 115
b5b68755
C
116 const liveVideoUpdate: LiveVideoUpdate = {
117 saveReplay: this.form.value.saveReplay
118 }
119
a02b93ce 120 this.loadingBar.useRef().start()
68e24d72 121 this.isUpdatingVideo = true
40e87e9e
C
122
123 // Update the video
d8e689b8 124 this.videoService.updateVideo(this.video)
40e87e9e
C
125 .pipe(
126 // Then update captions
b5b68755
C
127 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions)),
128
129 switchMap(() => {
130 if (!this.liveVideo) return of(undefined)
131
132 return this.liveVideoService.updateLive(this.video.id, liveVideoUpdate)
133 })
40e87e9e
C
134 )
135 .subscribe(
136 () => {
772d5642 137 this.updateDone = true
40e87e9e 138 this.isUpdatingVideo = false
a02b93ce 139 this.loadingBar.useRef().complete()
66357162 140 this.notifier.success($localize`Video updated.`)
40e87e9e
C
141 this.router.navigate([ '/videos/watch', this.video.uuid ])
142 },
143
144 err => {
a02b93ce 145 this.loadingBar.useRef().complete()
40e87e9e 146 this.isUpdatingVideo = false
f8b2c1b4 147 this.notifier.error(err.message)
40e87e9e
C
148 console.error(err)
149 }
150 )
dc8bc31b 151 }
e54163c2 152
7294aab0
C
153 hydratePluginFieldsFromVideo () {
154 if (!this.video.pluginData) return
155
156 this.form.patchValue({
157 pluginData: this.video.pluginData
158 })
159 }
dc8bc31b 160}