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