]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-update.component.ts
remove unused imports
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-update.component.ts
1 import { map, switchMap } from 'rxjs/operators'
2 import { Component, OnInit } from '@angular/core'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { LoadingBarService } from '@ngx-loading-bar/core'
5 import { NotificationsService } from 'angular2-notifications'
6 import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos'
7 import { ServerService } from '../../core'
8 import { FormReactive } from '../../shared'
9 import { VideoEdit } from '../../shared/video/video-edit.model'
10 import { VideoService } from '../../shared/video/video.service'
11 import { I18n } from '@ngx-translate/i18n-polyfill'
12 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
13 import { VideoCaptionService } from '@app/shared/video-caption'
14 import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.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 videoPrivacies: VideoConstant<VideoPrivacy>[] = []
26 userVideoChannels: { id: number, label: string, support: string }[] = []
27 schedulePublicationPossible = false
28 videoCaptions: VideoCaptionEdit[] = []
29
30 private updateDone = false
31
32 constructor (
33 protected formValidatorService: FormValidatorService,
34 private route: ActivatedRoute,
35 private router: Router,
36 private notificationsService: NotificationsService,
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.serverService.videoPrivaciesLoaded
50 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
51
52 this.route.data
53 .pipe(map(data => data.videoData))
54 .subscribe(({ video, videoChannels, videoCaptions }) => {
55 this.video = new VideoEdit(video)
56 this.userVideoChannels = videoChannels
57 this.videoCaptions = videoCaptions
58
59 // We cannot set private a video that was not private
60 if (this.video.privacy !== VideoPrivacy.PRIVATE) {
61 this.videoPrivacies = this.videoPrivacies.filter(p => p.id !== VideoPrivacy.PRIVATE)
62 } else { // We can schedule video publication only if it it is private
63 this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
64 }
65
66 this.videoPrivacies = this.videoService.explainedPrivacyLabels(this.videoPrivacies)
67
68 // FIXME: Angular does not detect the change inside this subscription, so use the patched setTimeout
69 setTimeout(() => this.hydrateFormFromVideo())
70 },
71
72 err => {
73 console.error(err)
74 this.notificationsService.error(this.i18n('Error'), err.message)
75 }
76 )
77 }
78
79 canDeactivate () {
80 if (this.updateDone === true) return { canDeactivate: true }
81
82 for (const caption of this.videoCaptions) {
83 if (caption.action) return { canDeactivate: false }
84 }
85
86 return { canDeactivate: this.formChanged === false }
87 }
88
89 checkForm () {
90 this.forceCheck()
91
92 return this.form.valid
93 }
94
95 update () {
96 if (this.checkForm() === false) {
97 return
98 }
99
100 this.video.patch(this.form.value)
101
102 this.loadingBar.start()
103 this.isUpdatingVideo = true
104
105 // Update the video
106 this.videoService.updateVideo(this.video)
107 .pipe(
108 // Then update captions
109 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions))
110 )
111 .subscribe(
112 () => {
113 this.updateDone = true
114 this.isUpdatingVideo = false
115 this.loadingBar.complete()
116 this.notificationsService.success(this.i18n('Success'), this.i18n('Video updated.'))
117 this.router.navigate([ '/videos/watch', this.video.uuid ])
118 },
119
120 err => {
121 this.loadingBar.complete()
122 this.isUpdatingVideo = false
123 this.notificationsService.error(this.i18n('Error'), err.message)
124 console.error(err)
125 }
126 )
127 }
128
129 private hydrateFormFromVideo () {
130 this.form.patchValue(this.video.toFormPatch())
131
132 const objects = [
133 {
134 url: 'thumbnailUrl',
135 name: 'thumbnailfile'
136 },
137 {
138 url: 'previewUrl',
139 name: 'previewfile'
140 }
141 ]
142
143 for (const obj of objects) {
144 fetch(this.video[obj.url])
145 .then(response => response.blob())
146 .then(data => {
147 this.form.patchValue({
148 [ obj.name ]: data
149 })
150 })
151 }
152 }
153 }