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