]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-update.component.ts
ad6452835f4d522242a05e4e86f134943c36137f
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-update.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { FormBuilder, FormGroup } from '@angular/forms'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { NotificationsService } from 'angular2-notifications'
5 import 'rxjs/add/observable/forkJoin'
6 import { VideoPrivacy } from '../../../../../shared/models/videos'
7 import { ServerService } from '../../core'
8 import { AuthService } from '../../core/auth'
9 import { FormReactive } from '../../shared'
10 import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
11 import { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
12 import { VideoEdit } from '../../shared/video/video-edit.model'
13 import { VideoService } from '../../shared/video/video.service'
14
15 @Component({
16 selector: 'my-videos-update',
17 styleUrls: [ './shared/video-edit.component.scss' ],
18 templateUrl: './video-update.component.html'
19 })
20
21 export class VideoUpdateComponent extends FormReactive implements OnInit {
22 video: VideoEdit
23
24 form: FormGroup
25 formErrors: { [ id: string ]: string } = {}
26 validationMessages: ValidatorMessage = {}
27 videoPrivacies = []
28 userVideoChannels = []
29
30 constructor (
31 private formBuilder: FormBuilder,
32 private route: ActivatedRoute,
33 private router: Router,
34 private notificationsService: NotificationsService,
35 private serverService: ServerService,
36 private videoService: VideoService,
37 private authService: AuthService
38 ) {
39 super()
40 }
41
42 buildForm () {
43 this.form = this.formBuilder.group({})
44 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
45 }
46
47 ngOnInit () {
48 this.buildForm()
49
50 this.serverService.videoPrivaciesLoaded
51 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
52
53 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
54 .catch(err => console.error('Cannot populate async user video channels.', err))
55
56 const uuid: string = this.route.snapshot.params['uuid']
57 this.videoService.getVideo(uuid)
58 .switchMap(video => {
59 return this.videoService
60 .loadCompleteDescription(video.descriptionPath)
61 .do(description => video.description = description)
62 .map(() => video)
63 })
64 .subscribe(
65 video => {
66 this.video = new VideoEdit(video)
67
68 // We cannot set private a video that was not private
69 if (video.privacy !== VideoPrivacy.PRIVATE) {
70 const newVideoPrivacies = []
71 for (const p of this.videoPrivacies) {
72 if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p)
73 }
74
75 this.videoPrivacies = newVideoPrivacies
76 }
77
78 this.hydrateFormFromVideo()
79 },
80
81 err => {
82 console.error(err)
83 this.notificationsService.error('Error', err.message)
84 }
85 )
86 }
87
88 checkForm () {
89 this.forceCheck()
90
91 return this.form.valid
92 }
93
94 update () {
95 if (this.checkForm() === false) {
96 return
97 }
98
99 this.video.patch(this.form.value)
100
101 this.videoService.updateVideo(this.video)
102 .subscribe(
103 () => {
104 this.notificationsService.success('Success', 'Video updated.')
105 this.router.navigate([ '/videos/watch', this.video.uuid ])
106 },
107
108 err => {
109 this.notificationsService.error('Error', err.message)
110 console.error(err)
111 }
112 )
113
114 }
115
116 private hydrateFormFromVideo () {
117 this.form.patchValue(this.video.toJSON())
118
119 const objects = [
120 {
121 url: 'thumbnailUrl',
122 name: 'thumbnailfile'
123 },
124 {
125 url: 'previewUrl',
126 name: 'previewfile'
127 }
128 ]
129
130 for (const obj of objects) {
131 fetch(this.video[obj.url])
132 .then(response => response.blob())
133 .then(data => {
134 this.form.patchValue({
135 [ obj.name ]: data
136 })
137 })
138 }
139 }
140 }