]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - 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
1import { of } from 'rxjs'
2import { map, switchMap } from 'rxjs/operators'
3import { Component, HostListener, OnInit } from '@angular/core'
4import { ActivatedRoute, Router } from '@angular/router'
5import { Notifier } from '@app/core'
6import { FormReactive, FormValidatorService, SelectChannelItem } from '@app/shared/shared-forms'
7import { VideoCaptionEdit, VideoCaptionService, VideoDetails, VideoEdit, VideoService } from '@app/shared/shared-main'
8import { LiveVideoService } from '@app/shared/shared-video-live'
9import { LoadingBarService } from '@ngx-loading-bar/core'
10import { LiveVideo, LiveVideoUpdate, VideoPrivacy } from '@shared/models'
11import { hydrateFormFromVideo } from './shared/video-edit-utils'
12
13@Component({
14 selector: 'my-videos-update',
15 styleUrls: [ './shared/video-edit.component.scss' ],
16 templateUrl: './video-update.component.html'
17})
18export class VideoUpdateComponent extends FormReactive implements OnInit {
19 video: VideoEdit
20 videoDetails: VideoDetails
21 userVideoChannels: SelectChannelItem[] = []
22 videoCaptions: VideoCaptionEdit[] = []
23 liveVideo: LiveVideo
24
25 isUpdatingVideo = false
26 schedulePublicationPossible = false
27 waitTranscodingEnabled = true
28
29 private updateDone = false
30
31 constructor (
32 protected formValidatorService: FormValidatorService,
33 private route: ActivatedRoute,
34 private router: Router,
35 private notifier: Notifier,
36 private videoService: VideoService,
37 private loadingBar: LoadingBarService,
38 private videoCaptionService: VideoCaptionService,
39 private liveVideoService: LiveVideoService
40 ) {
41 super()
42 }
43
44 ngOnInit () {
45 this.buildForm({})
46
47 this.route.data
48 .pipe(map(data => data.videoData))
49 .subscribe(({ video, videoChannels, videoCaptions, liveVideo }) => {
50 this.video = new VideoEdit(video)
51 this.videoDetails = video
52
53 this.userVideoChannels = videoChannels
54 this.videoCaptions = videoCaptions
55 this.liveVideo = liveVideo
56
57 this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
58
59 // FIXME: Angular does not detect the change inside this subscription, so use the patched setTimeout
60 setTimeout(() => {
61 hydrateFormFromVideo(this.form, this.video, true)
62
63 if (this.liveVideo) {
64 this.form.patchValue({
65 saveReplay: this.liveVideo.saveReplay,
66 permanentLive: this.liveVideo.permanentLive
67 })
68 }
69 })
70 },
71
72 err => {
73 console.error(err)
74 this.notifier.error(err.message)
75 }
76 )
77 }
78
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 } {
90 if (this.updateDone === true) return { canDeactivate: true }
91
92 const text = $localize`You have unsaved changes! If you leave, your changes will be lost.`
93
94 for (const caption of this.videoCaptions) {
95 if (caption.action) return { canDeactivate: false, text }
96 }
97
98 return { canDeactivate: this.formChanged === false, text }
99 }
100
101 checkForm () {
102 this.forceCheck()
103
104 return this.form.valid
105 }
106
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
119 update () {
120 if (this.checkForm() === false
121 || this.isUpdatingVideo === true) {
122 return
123 }
124
125 this.video.patch(this.form.value)
126
127 const liveVideoUpdate: LiveVideoUpdate = {
128 saveReplay: this.form.value.saveReplay,
129 permanentLive: this.form.value.permanentLive
130 }
131
132 this.loadingBar.useRef().start()
133 this.isUpdatingVideo = true
134
135 // Update the video
136 this.videoService.updateVideo(this.video)
137 .pipe(
138 // Then update captions
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 })
146 )
147 .subscribe(
148 () => {
149 this.updateDone = true
150 this.isUpdatingVideo = false
151 this.loadingBar.useRef().complete()
152 this.notifier.success($localize`Video updated.`)
153 this.router.navigate([ '/videos/watch', this.video.uuid ])
154 },
155
156 err => {
157 this.loadingBar.useRef().complete()
158 this.isUpdatingVideo = false
159 this.notifier.error(err.message)
160 console.error(err)
161 }
162 )
163 }
164
165 hydratePluginFieldsFromVideo () {
166 if (!this.video.pluginData) return
167
168 this.form.patchValue({
169 pluginData: this.video.pluginData
170 })
171 }
172}