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