]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-edit/video-update.component.ts
Fix webpack config
[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'
d4a8e7a6 8import { Video, VideoCaptionEdit, VideoCaptionService, VideoDetails, VideoEdit, VideoService } from '@app/shared/shared-main'
f8c00564 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))
1378c0d3
C
50 .subscribe({
51 next: ({ video, videoChannels, videoCaptions, liveVideo }) => {
52 this.video = new VideoEdit(video)
53 this.videoDetails = video
54
55 this.userVideoChannels = videoChannels
56 this.videoCaptions = videoCaptions
57 this.liveVideo = liveVideo
58
59 this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
60
61 // FIXME: Angular does not detect the change inside this subscription, so use the patched setTimeout
62 setTimeout(() => {
63 hydrateFormFromVideo(this.form, this.video, true)
64
65 if (this.liveVideo) {
66 this.form.patchValue({
67 saveReplay: this.liveVideo.saveReplay,
68 permanentLive: this.liveVideo.permanentLive
69 })
70 }
71 })
72 },
2de96f4d 73
1378c0d3
C
74 error: err => {
75 console.error(err)
76 this.notifier.error(err.message)
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
ddb62a85
C
109 isWaitTranscodingEnabled () {
110 if (this.videoDetails.getFiles().length > 1) { // Already transcoded
111 return false
112 }
113
114 if (this.liveVideo && this.form.value['saveReplay'] !== true) {
115 return false
116 }
117
118 return true
119 }
120
df98563e 121 update () {
8c2b9756
RK
122 if (this.checkForm() === false
123 || this.isUpdatingVideo === true) {
df98563e 124 return
c24ac1c1
C
125 }
126
df98563e 127 this.video.patch(this.form.value)
d8e689b8 128
a02b93ce 129 this.loadingBar.useRef().start()
68e24d72 130 this.isUpdatingVideo = true
40e87e9e
C
131
132 // Update the video
d8e689b8 133 this.videoService.updateVideo(this.video)
40e87e9e
C
134 .pipe(
135 // Then update captions
b5b68755
C
136 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions)),
137
138 switchMap(() => {
139 if (!this.liveVideo) return of(undefined)
140
fd0fdc46 141 const liveVideoUpdate: LiveVideoUpdate = {
5d84d717
C
142 saveReplay: !!this.form.value.saveReplay,
143 permanentLive: !!this.form.value.permanentLive
fd0fdc46
C
144 }
145
fd0fdc46
C
146 // Don't update live attributes if they did not change
147 const liveChanged = Object.keys(liveVideoUpdate)
148 .some(key => this.liveVideo[key] !== liveVideoUpdate[key])
149 if (!liveChanged) return of(undefined)
150
b5b68755
C
151 return this.liveVideoService.updateLive(this.video.id, liveVideoUpdate)
152 })
40e87e9e 153 )
1378c0d3
C
154 .subscribe({
155 next: () => {
772d5642 156 this.updateDone = true
40e87e9e 157 this.isUpdatingVideo = false
a02b93ce 158 this.loadingBar.useRef().complete()
66357162 159 this.notifier.success($localize`Video updated.`)
d4a8e7a6 160 this.router.navigateByUrl(Video.buildWatchUrl(this.video))
40e87e9e
C
161 },
162
1378c0d3 163 error: err => {
a02b93ce 164 this.loadingBar.useRef().complete()
40e87e9e 165 this.isUpdatingVideo = false
f8b2c1b4 166 this.notifier.error(err.message)
40e87e9e
C
167 console.error(err)
168 }
1378c0d3 169 })
dc8bc31b 170 }
e54163c2 171
7294aab0
C
172 hydratePluginFieldsFromVideo () {
173 if (!this.video.pluginData) return
174
175 this.form.patchValue({
176 pluginData: this.video.pluginData
177 })
178 }
d4a8e7a6
C
179
180 getVideoUrl () {
181 return Video.buildWatchUrl(this.videoDetails)
182 }
dc8bc31b 183}