]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-update.component.ts
Add i18n attributes
[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 { FormBuilder, FormGroup } from '@angular/forms'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { LoadingBarService } from '@ngx-loading-bar/core'
6 import { NotificationsService } from 'angular2-notifications'
7 import { VideoPrivacy } from '../../../../../shared/models/videos'
8 import { ServerService } from '../../core'
9 import { AuthService } from '../../core/auth'
10 import { FormReactive } from '../../shared'
11 import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
12 import { VideoEdit } from '../../shared/video/video-edit.model'
13 import { VideoService } from '../../shared/video/video.service'
14 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
15 import { I18n } from '@ngx-translate/i18n-polyfill'
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 form: FormGroup
27 formErrors: { [ id: string ]: string } = {}
28 validationMessages: ValidatorMessage = {}
29 videoPrivacies = []
30 userVideoChannels = []
31
32 constructor (
33 private formBuilder: FormBuilder,
34 private route: ActivatedRoute,
35 private router: Router,
36 private notificationsService: NotificationsService,
37 private serverService: ServerService,
38 private videoService: VideoService,
39 private authService: AuthService,
40 private loadingBar: LoadingBarService,
41 private videoChannelService: VideoChannelService,
42 private i18n: I18n
43 ) {
44 super()
45 }
46
47 buildForm () {
48 this.form = this.formBuilder.group({})
49 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
50 }
51
52 ngOnInit () {
53 this.buildForm()
54
55 this.serverService.videoPrivaciesLoaded
56 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
57
58 const uuid: string = this.route.snapshot.params[ 'uuid' ]
59 this.videoService.getVideo(uuid)
60 .pipe(
61 switchMap(video => {
62 return this.videoService
63 .loadCompleteDescription(video.descriptionPath)
64 .pipe(map(description => Object.assign(video, { description })))
65 }),
66 switchMap(video => {
67 return this.videoChannelService
68 .listAccountVideoChannels(video.account)
69 .pipe(
70 map(result => result.data),
71 map(videoChannels => videoChannels.map(c => ({ id: c.id, label: c.displayName, support: c.support }))),
72 map(videoChannels => ({ video, videoChannels }))
73 )
74 })
75 )
76 .subscribe(
77 ({ video, videoChannels }) => {
78 this.video = new VideoEdit(video)
79 this.userVideoChannels = videoChannels
80
81 // We cannot set private a video that was not private
82 if (video.privacy.id !== VideoPrivacy.PRIVATE) {
83 const newVideoPrivacies = []
84 for (const p of this.videoPrivacies) {
85 if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p)
86 }
87
88 this.videoPrivacies = newVideoPrivacies
89 }
90
91 this.hydrateFormFromVideo()
92 },
93
94 err => {
95 console.error(err)
96 this.notificationsService.error(this.i18n('Error'), err.message)
97 }
98 )
99 }
100
101 checkForm () {
102 this.forceCheck()
103
104 return this.form.valid
105 }
106
107 update () {
108 if (this.checkForm() === false) {
109 return
110 }
111
112 this.video.patch(this.form.value)
113
114 this.loadingBar.start()
115 this.isUpdatingVideo = true
116 this.videoService.updateVideo(this.video)
117 .subscribe(
118 () => {
119 this.isUpdatingVideo = false
120 this.loadingBar.complete()
121 this.notificationsService.success(this.i18n('Success'), this.i18n('Video updated.'))
122 this.router.navigate([ '/videos/watch', this.video.uuid ])
123 },
124
125 err => {
126 this.isUpdatingVideo = false
127 this.notificationsService.error(this.i18n('Error'), err.message)
128 console.error(err)
129 }
130 )
131
132 }
133
134 private hydrateFormFromVideo () {
135 this.form.patchValue(this.video.toJSON())
136
137 const objects = [
138 {
139 url: 'thumbnailUrl',
140 name: 'thumbnailfile'
141 },
142 {
143 url: 'previewUrl',
144 name: 'previewfile'
145 }
146 ]
147
148 for (const obj of objects) {
149 fetch(this.video[obj.url])
150 .then(response => response.blob())
151 .then(data => {
152 this.form.patchValue({
153 [ obj.name ]: data
154 })
155 })
156 }
157 }
158 }