adding missing i18n for schedule option
[oweals/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 { AuthService } from '../../core/auth'
9 import { FormReactive } from '../../shared'
10 import { VideoEdit } from '../../shared/video/video-edit.model'
11 import { VideoService } from '../../shared/video/video.service'
12 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
13 import { I18n } from '@ngx-translate/i18n-polyfill'
14 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
15 import { VideoCaptionService } from '@app/shared/video-caption'
16 import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model'
17
18 @Component({
19   selector: 'my-videos-update',
20   styleUrls: [ './shared/video-edit.component.scss' ],
21   templateUrl: './video-update.component.html'
22 })
23 export class VideoUpdateComponent extends FormReactive implements OnInit {
24   video: VideoEdit
25
26   isUpdatingVideo = false
27   videoPrivacies: VideoConstant<VideoPrivacy>[] = []
28   userVideoChannels: { id: number, label: string, support: string }[] = []
29   schedulePublicationPossible = false
30   videoCaptions: VideoCaptionEdit[] = []
31
32   private updateDone = false
33
34   constructor (
35     protected formValidatorService: FormValidatorService,
36     private route: ActivatedRoute,
37     private router: Router,
38     private notificationsService: NotificationsService,
39     private serverService: ServerService,
40     private videoService: VideoService,
41     private authService: AuthService,
42     private loadingBar: LoadingBarService,
43     private videoChannelService: VideoChannelService,
44     private videoCaptionService: VideoCaptionService,
45     private i18n: I18n
46   ) {
47     super()
48   }
49
50   ngOnInit () {
51     this.buildForm({})
52
53     this.serverService.videoPrivaciesLoaded
54         .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
55
56     this.route.data
57         .pipe(map(data => data.videoData))
58         .subscribe(({ video, videoChannels, videoCaptions }) => {
59           this.video = new VideoEdit(video)
60           this.userVideoChannels = videoChannels
61           this.videoCaptions = videoCaptions
62
63           // We cannot set private a video that was not private
64           if (this.video.privacy !== VideoPrivacy.PRIVATE) {
65             this.videoPrivacies = this.videoPrivacies.filter(p => p.id !== VideoPrivacy.PRIVATE)
66           } else { // We can schedule video publication only if it it is private
67             this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
68           }
69
70           this.videoPrivacies = this.videoService.explainedPrivacyLabels(this.videoPrivacies)
71
72           // FIXME: Angular does not detect the change inside this subscription, so use the patched setTimeout
73           setTimeout(() => this.hydrateFormFromVideo())
74         },
75
76         err => {
77           console.error(err)
78           this.notificationsService.error(this.i18n('Error'), err.message)
79         }
80       )
81   }
82
83   canDeactivate () {
84     if (this.updateDone === true) return { canDeactivate: true }
85
86     for (const caption of this.videoCaptions) {
87       if (caption.action) return { canDeactivate: false }
88     }
89
90     return { canDeactivate: this.formChanged === false }
91   }
92
93   checkForm () {
94     this.forceCheck()
95
96     return this.form.valid
97   }
98
99   update () {
100     if (this.checkForm() === false) {
101       return
102     }
103
104     this.video.patch(this.form.value)
105
106     this.loadingBar.start()
107     this.isUpdatingVideo = true
108
109     // Update the video
110     this.videoService.updateVideo(this.video)
111         .pipe(
112           // Then update captions
113           switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions))
114         )
115         .subscribe(
116           () => {
117             this.updateDone = true
118             this.isUpdatingVideo = false
119             this.loadingBar.complete()
120             this.notificationsService.success(this.i18n('Success'), this.i18n('Video updated.'))
121             this.router.navigate([ '/videos/watch', this.video.uuid ])
122           },
123
124           err => {
125             this.loadingBar.complete()
126             this.isUpdatingVideo = false
127             this.notificationsService.error(this.i18n('Error'), err.message)
128             console.error(err)
129           }
130         )
131   }
132
133   private hydrateFormFromVideo () {
134     this.form.patchValue(this.video.toFormPatch())
135
136     const objects = [
137       {
138         url: 'thumbnailUrl',
139         name: 'thumbnailfile'
140       },
141       {
142         url: 'previewUrl',
143         name: 'previewfile'
144       }
145     ]
146
147     for (const obj of objects) {
148       fetch(this.video[obj.url])
149         .then(response => response.blob())
150         .then(data => {
151           this.form.patchValue({
152             [ obj.name ]: data
153           })
154         })
155     }
156   }
157 }