]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-edit/video-update.component.ts
Move to sass module
[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'
21e493d4 3import { SelectChannelItem } from 'src/types/select-options-item.model'
674a66bb 4import { Component, HostListener, OnInit } from '@angular/core'
df98563e 5import { ActivatedRoute, Router } from '@angular/router'
f8b2c1b4 6import { Notifier } from '@app/core'
21e493d4 7import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
f8c00564
C
8import { VideoCaptionEdit, VideoCaptionService, VideoDetails, VideoEdit, VideoService } from '@app/shared/shared-main'
9import { LiveVideoService } from '@app/shared/shared-video-live'
67ed6552 10import { LoadingBarService } from '@ngx-loading-bar/core'
b5b68755 11import { LiveVideo, LiveVideoUpdate, VideoPrivacy } from '@shared/models'
c6c0fa6c 12import { hydrateFormFromVideo } from './shared/video-edit-utils'
1553e15d 13
dc8bc31b 14@Component({
d8e689b8 15 selector: 'my-videos-update',
80958c78 16 styleUrls: [ './shared/video-edit.component.scss' ],
d8e689b8 17 templateUrl: './video-update.component.html'
dc8bc31b 18})
d8e689b8 19export class VideoUpdateComponent extends FormReactive implements OnInit {
404b54e1 20 video: VideoEdit
ddb62a85 21 videoDetails: VideoDetails
c6c0fa6c
C
22 userVideoChannels: SelectChannelItem[] = []
23 videoCaptions: VideoCaptionEdit[] = []
a5cf76af 24 liveVideo: LiveVideo
4b2f33f3 25
68e24d72 26 isUpdatingVideo = false
bbe0f064 27 schedulePublicationPossible = false
14e2014a 28 waitTranscodingEnabled = true
dc8bc31b 29
772d5642
C
30 private updateDone = false
31
df98563e 32 constructor (
d18d6478 33 protected formValidatorService: FormValidatorService,
d8e689b8 34 private route: ActivatedRoute,
7ddd02c9 35 private router: Router,
f8b2c1b4 36 private notifier: Notifier,
15a7387d 37 private videoService: VideoService,
6200d8d9 38 private loadingBar: LoadingBarService,
b5b68755
C
39 private videoCaptionService: VideoCaptionService,
40 private liveVideoService: LiveVideoService
66357162 41 ) {
df98563e 42 super()
4b2f33f3 43 }
dc8bc31b 44
df98563e 45 ngOnInit () {
d18d6478 46 this.buildForm({})
d8e689b8 47
308c4275
C
48 this.route.data
49 .pipe(map(data => data.videoData))
a5cf76af 50 .subscribe(({ video, videoChannels, videoCaptions, liveVideo }) => {
308c4275 51 this.video = new VideoEdit(video)
ddb62a85
C
52 this.videoDetails = video
53
308c4275
C
54 this.userVideoChannels = videoChannels
55 this.videoCaptions = videoCaptions
a5cf76af 56 this.liveVideo = liveVideo
db400f44 57
12bec528
C
58 this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
59
772d5642 60 // FIXME: Angular does not detect the change inside this subscription, so use the patched setTimeout
b5b68755
C
61 setTimeout(() => {
62 hydrateFormFromVideo(this.form, this.video, true)
63
64 if (this.liveVideo) {
65 this.form.patchValue({
bb4ba6d9
C
66 saveReplay: this.liveVideo.saveReplay,
67 permanentLive: this.liveVideo.permanentLive
b5b68755
C
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
ddb62a85
C
108 isWaitTranscodingEnabled () {
109 if (this.videoDetails.getFiles().length > 1) { // Already transcoded
110 return false
111 }
112
113 if (this.liveVideo && this.form.value['saveReplay'] !== true) {
114 return false
115 }
116
117 return true
118 }
119
df98563e 120 update () {
8c2b9756
RK
121 if (this.checkForm() === false
122 || this.isUpdatingVideo === true) {
df98563e 123 return
c24ac1c1
C
124 }
125
df98563e 126 this.video.patch(this.form.value)
d8e689b8 127
a02b93ce 128 this.loadingBar.useRef().start()
68e24d72 129 this.isUpdatingVideo = true
40e87e9e
C
130
131 // Update the video
d8e689b8 132 this.videoService.updateVideo(this.video)
40e87e9e
C
133 .pipe(
134 // Then update captions
b5b68755
C
135 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions)),
136
137 switchMap(() => {
138 if (!this.liveVideo) return of(undefined)
139
fd0fdc46 140 const liveVideoUpdate: LiveVideoUpdate = {
5d84d717
C
141 saveReplay: !!this.form.value.saveReplay,
142 permanentLive: !!this.form.value.permanentLive
fd0fdc46
C
143 }
144
fd0fdc46
C
145 // Don't update live attributes if they did not change
146 const liveChanged = Object.keys(liveVideoUpdate)
147 .some(key => this.liveVideo[key] !== liveVideoUpdate[key])
148 if (!liveChanged) return of(undefined)
149
b5b68755
C
150 return this.liveVideoService.updateLive(this.video.id, liveVideoUpdate)
151 })
40e87e9e
C
152 )
153 .subscribe(
154 () => {
772d5642 155 this.updateDone = true
40e87e9e 156 this.isUpdatingVideo = false
a02b93ce 157 this.loadingBar.useRef().complete()
66357162 158 this.notifier.success($localize`Video updated.`)
a1eda903 159 this.router.navigate([ '/w', this.video.uuid ])
40e87e9e
C
160 },
161
162 err => {
a02b93ce 163 this.loadingBar.useRef().complete()
40e87e9e 164 this.isUpdatingVideo = false
f8b2c1b4 165 this.notifier.error(err.message)
40e87e9e
C
166 console.error(err)
167 }
168 )
dc8bc31b 169 }
e54163c2 170
7294aab0
C
171 hydratePluginFieldsFromVideo () {
172 if (!this.video.pluginData) return
173
174 this.form.patchValue({
175 pluginData: this.video.pluginData
176 })
177 }
dc8bc31b 178}