]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-update.component.ts
Add ability to update a video channel
[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 { LoadingBarService } from '@ngx-loading-bar/core'
5 import { NotificationsService } from 'angular2-notifications'
6 import 'rxjs/add/observable/forkJoin'
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 { populateAsyncUserVideoChannels } from '@app/shared/misc/utils'
15
16 @Component({
17 selector: 'my-videos-update',
18 styleUrls: [ './shared/video-edit.component.scss' ],
19 templateUrl: './video-update.component.html'
20 })
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 ) {
42 super()
43 }
44
45 buildForm () {
46 this.form = this.formBuilder.group({})
47 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
48 }
49
50 ngOnInit () {
51 this.buildForm()
52
53 this.serverService.videoPrivaciesLoaded
54 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
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 .map(description => Object.assign(video, { description }))
62 })
63 .subscribe(
64 video => {
65 this.video = new VideoEdit(video)
66
67 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
68 .catch(err => console.error(err))
69
70 // We cannot set private a video that was not private
71 if (video.privacy.id !== VideoPrivacy.PRIVATE) {
72 const newVideoPrivacies = []
73 for (const p of this.videoPrivacies) {
74 if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p)
75 }
76
77 this.videoPrivacies = newVideoPrivacies
78 }
79
80 this.hydrateFormFromVideo()
81 },
82
83 err => {
84 console.error(err)
85 this.notificationsService.error('Error', err.message)
86 }
87 )
88 }
89
90 checkForm () {
91 this.forceCheck()
92
93 return this.form.valid
94 }
95
96 update () {
97 if (this.checkForm() === false) {
98 return
99 }
100
101 this.video.patch(this.form.value)
102
103 this.loadingBar.start()
104 this.isUpdatingVideo = true
105 this.videoService.updateVideo(this.video)
106 .subscribe(
107 () => {
108 this.isUpdatingVideo = false
109 this.loadingBar.complete()
110 this.notificationsService.success('Success', 'Video updated.')
111 this.router.navigate([ '/videos/watch', this.video.uuid ])
112 },
113
114 err => {
115 this.isUpdatingVideo = false
116 this.notificationsService.error('Error', err.message)
117 console.error(err)
118 }
119 )
120
121 }
122
123 private hydrateFormFromVideo () {
124 this.form.patchValue(this.video.toJSON())
125
126 const objects = [
127 {
128 url: 'thumbnailUrl',
129 name: 'thumbnailfile'
130 },
131 {
132 url: 'previewUrl',
133 name: 'previewfile'
134 }
135 ]
136
137 for (const obj of objects) {
138 fetch(this.video[obj.url])
139 .then(response => response.blob())
140 .then(data => {
141 this.form.patchValue({
142 [ obj.name ]: data
143 })
144 })
145 }
146 }
147 }