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