]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-update.component.ts
Merge branch 'feature/design' into develop
[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/video-privacy.enum'
7 import { ServerService } from '../../core'
8 import { FormReactive } from '../../shared'
9 import { ValidatorMessage } from '../../shared/forms/form-validators'
10 import { VideoEdit } from '../../shared/video/video-edit.model'
11 import { VideoService } from '../../shared/video/video.service'
12
13 @Component({
14 selector: 'my-videos-update',
15 styleUrls: [ './shared/video-edit.component.scss' ],
16 templateUrl: './video-update.component.html'
17 })
18
19 export class VideoUpdateComponent extends FormReactive implements OnInit {
20 video: VideoEdit
21
22 error: string = null
23 form: FormGroup
24 formErrors: { [ id: string ]: string } = {}
25 validationMessages: ValidatorMessage = {}
26 videoPrivacies = []
27
28 fileError = ''
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 ) {
38 super()
39 }
40
41 buildForm () {
42 this.form = this.formBuilder.group({})
43 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
44 }
45
46 ngOnInit () {
47 this.buildForm()
48
49 this.videoPrivacies = this.serverService.getVideoPrivacies()
50
51 const uuid: string = this.route.snapshot.params['uuid']
52 this.videoService.getVideo(uuid)
53 .switchMap(video => {
54 return this.videoService
55 .loadCompleteDescription(video.descriptionPath)
56 .do(description => video.description = description)
57 .map(() => video)
58 })
59 .subscribe(
60 video => {
61 this.video = new VideoEdit(video)
62
63 // We cannot set private a video that was not private
64 if (video.privacy !== VideoPrivacy.PRIVATE) {
65 const newVideoPrivacies = []
66 for (const p of this.videoPrivacies) {
67 if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p)
68 }
69
70 this.videoPrivacies = newVideoPrivacies
71 }
72
73 this.hydrateFormFromVideo()
74 },
75
76 err => {
77 console.error(err)
78 this.error = 'Cannot fetch video.'
79 }
80 )
81 }
82
83 checkForm () {
84 this.forceCheck()
85
86 return this.form.valid
87 }
88
89 update () {
90 if (this.checkForm() === false) {
91 return
92 }
93
94 this.video.patch(this.form.value)
95
96 this.videoService.updateVideo(this.video)
97 .subscribe(
98 () => {
99 this.notificationsService.success('Success', 'Video updated.')
100 this.router.navigate([ '/videos/watch', this.video.uuid ])
101 },
102
103 err => {
104 this.error = 'Cannot update the video.'
105 console.error(err)
106 }
107 )
108
109 }
110
111 private hydrateFormFromVideo () {
112 this.form.patchValue(this.video.toJSON())
113 }
114 }