]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-edit/video-update.component.ts
Add unicode emoji to markdown
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-edit / video-update.component.ts
CommitLineData
db400f44 1import { map, switchMap } from 'rxjs/operators'
674a66bb 2import { Component, HostListener, OnInit } from '@angular/core'
df98563e 3import { ActivatedRoute, Router } from '@angular/router'
f8b2c1b4 4import { Notifier } from '@app/core'
52c4976f 5import { FormReactive, FormValidatorService, SelectChannelItem } from '@app/shared/shared-forms'
67ed6552
C
6import { VideoCaptionEdit, VideoCaptionService, VideoDetails, VideoEdit, VideoService } from '@app/shared/shared-main'
7import { LoadingBarService } from '@ngx-loading-bar/core'
12bec528 8import { VideoPrivacy } from '@shared/models'
1553e15d 9
dc8bc31b 10@Component({
d8e689b8 11 selector: 'my-videos-update',
80958c78 12 styleUrls: [ './shared/video-edit.component.scss' ],
d8e689b8 13 templateUrl: './video-update.component.html'
dc8bc31b 14})
d8e689b8 15export class VideoUpdateComponent extends FormReactive implements OnInit {
404b54e1 16 video: VideoEdit
4b2f33f3 17
68e24d72 18 isUpdatingVideo = false
9abd170d 19 userVideoChannels: SelectChannelItem[] = []
bbe0f064 20 schedulePublicationPossible = false
ef4c78da 21 videoCaptions: VideoCaptionEdit[] = []
14e2014a 22 waitTranscodingEnabled = true
dc8bc31b 23
772d5642
C
24 private updateDone = false
25
df98563e 26 constructor (
d18d6478 27 protected formValidatorService: FormValidatorService,
d8e689b8 28 private route: ActivatedRoute,
7ddd02c9 29 private router: Router,
f8b2c1b4 30 private notifier: Notifier,
15a7387d 31 private videoService: VideoService,
6200d8d9 32 private loadingBar: LoadingBarService,
66357162
C
33 private videoCaptionService: VideoCaptionService
34 ) {
df98563e 35 super()
4b2f33f3 36 }
dc8bc31b 37
df98563e 38 ngOnInit () {
d18d6478 39 this.buildForm({})
d8e689b8 40
308c4275
C
41 this.route.data
42 .pipe(map(data => data.videoData))
43 .subscribe(({ video, videoChannels, videoCaptions }) => {
44 this.video = new VideoEdit(video)
45 this.userVideoChannels = videoChannels
46 this.videoCaptions = videoCaptions
db400f44 47
12bec528
C
48 this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
49
5a71acd2 50 const videoFiles = (video as VideoDetails).getFiles()
14e2014a
C
51 if (videoFiles.length > 1) { // Already transcoded
52 this.waitTranscodingEnabled = false
53 }
54
772d5642 55 // FIXME: Angular does not detect the change inside this subscription, so use the patched setTimeout
308c4275
C
56 setTimeout(() => this.hydrateFormFromVideo())
57 },
2de96f4d 58
308c4275
C
59 err => {
60 console.error(err)
f8b2c1b4 61 this.notifier.error(err.message)
308c4275
C
62 }
63 )
e822fdae
C
64 }
65
674a66bb
C
66 @HostListener('window:beforeunload', [ '$event' ])
67 onUnload (event: any) {
68 const { text, canDeactivate } = this.canDeactivate()
69
70 if (canDeactivate) return
71
72 event.returnValue = text
73 return text
74 }
75
76 canDeactivate (): { canDeactivate: boolean, text?: string } {
772d5642
C
77 if (this.updateDone === true) return { canDeactivate: true }
78
66357162 79 const text = $localize`You have unsaved changes! If you leave, your changes will be lost.`
674a66bb 80
772d5642 81 for (const caption of this.videoCaptions) {
674a66bb 82 if (caption.action) return { canDeactivate: false, text }
772d5642
C
83 }
84
674a66bb 85 return { canDeactivate: this.formChanged === false, text }
772d5642
C
86 }
87
df98563e
C
88 checkForm () {
89 this.forceCheck()
c24ac1c1 90
df98563e 91 return this.form.valid
c24ac1c1
C
92 }
93
df98563e 94 update () {
8c2b9756
RK
95 if (this.checkForm() === false
96 || this.isUpdatingVideo === true) {
df98563e 97 return
c24ac1c1
C
98 }
99
df98563e 100 this.video.patch(this.form.value)
d8e689b8 101
a02b93ce 102 this.loadingBar.useRef().start()
68e24d72 103 this.isUpdatingVideo = true
40e87e9e
C
104
105 // Update the video
d8e689b8 106 this.videoService.updateVideo(this.video)
40e87e9e
C
107 .pipe(
108 // Then update captions
109 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions))
110 )
111 .subscribe(
112 () => {
772d5642 113 this.updateDone = true
40e87e9e 114 this.isUpdatingVideo = false
a02b93ce 115 this.loadingBar.useRef().complete()
66357162 116 this.notifier.success($localize`Video updated.`)
40e87e9e
C
117 this.router.navigate([ '/videos/watch', this.video.uuid ])
118 },
119
120 err => {
a02b93ce 121 this.loadingBar.useRef().complete()
40e87e9e 122 this.isUpdatingVideo = false
f8b2c1b4 123 this.notifier.error(err.message)
40e87e9e
C
124 console.error(err)
125 }
126 )
dc8bc31b 127 }
e54163c2 128
df98563e 129 private hydrateFormFromVideo () {
bbe0f064 130 this.form.patchValue(this.video.toFormPatch())
6de36768
C
131
132 const objects = [
133 {
134 url: 'thumbnailUrl',
135 name: 'thumbnailfile'
136 },
137 {
138 url: 'previewUrl',
139 name: 'previewfile'
140 }
141 ]
142
143 for (const obj of objects) {
144 fetch(this.video[obj.url])
145 .then(response => response.blob())
146 .then(data => {
147 this.form.patchValue({
148 [ obj.name ]: data
149 })
150 })
151 }
d8e689b8 152 }
dc8bc31b 153}