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