]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/video-update.component.ts
Redirect to uuid video route after upload
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-update.component.ts
CommitLineData
db7af09b 1import { Component, OnInit } from '@angular/core'
df98563e
C
2import { FormBuilder, FormGroup } from '@angular/forms'
3import { ActivatedRoute, Router } from '@angular/router'
df98563e 4import { NotificationsService } from 'angular2-notifications'
202f6b6c 5import 'rxjs/add/observable/forkJoin'
63c4db6d 6import { VideoPrivacy } from '../../../../../shared/models/videos'
db7af09b 7import { ServerService } from '../../core'
4cc66133 8import { FormReactive } from '../../shared'
63c4db6d 9import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
202f6b6c 10import { VideoEdit } from '../../shared/video/video-edit.model'
4cc66133 11import { VideoService } from '../../shared/video/video.service'
1553e15d 12
dc8bc31b 13@Component({
d8e689b8 14 selector: 'my-videos-update',
80958c78 15 styleUrls: [ './shared/video-edit.component.scss' ],
d8e689b8 16 templateUrl: './video-update.component.html'
dc8bc31b
C
17})
18
d8e689b8 19export class VideoUpdateComponent extends FormReactive implements OnInit {
404b54e1 20 video: VideoEdit
4b2f33f3 21
df98563e
C
22 error: string = null
23 form: FormGroup
ff249f49
C
24 formErrors: { [ id: string ]: string } = {}
25 validationMessages: ValidatorMessage = {}
26 videoPrivacies = []
dc8bc31b 27
df98563e 28 constructor (
4b2f33f3 29 private formBuilder: FormBuilder,
d8e689b8 30 private route: ActivatedRoute,
7ddd02c9 31 private router: Router,
6e07c3de 32 private notificationsService: NotificationsService,
db7af09b 33 private serverService: ServerService,
6e07c3de 34 private videoService: VideoService
4b2f33f3 35 ) {
df98563e 36 super()
4b2f33f3 37 }
dc8bc31b 38
df98563e 39 buildForm () {
ff249f49 40 this.form = this.formBuilder.group({})
df98563e 41 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
e822fdae
C
42 }
43
df98563e
C
44 ngOnInit () {
45 this.buildForm()
d8e689b8 46
fd45e8f4 47 this.videoPrivacies = this.serverService.getVideoPrivacies()
6e07c3de 48
0a6658fd 49 const uuid: string = this.route.snapshot.params['uuid']
2de96f4d
C
50 this.videoService.getVideo(uuid)
51 .switchMap(video => {
52 return this.videoService
53 .loadCompleteDescription(video.descriptionPath)
54 .do(description => video.description = description)
55 .map(() => video)
56 })
57 .subscribe(
58 video => {
59 this.video = new VideoEdit(video)
60
ff249f49 61 // We cannot set private a video that was not private
fd45e8f4
C
62 if (video.privacy !== VideoPrivacy.PRIVATE) {
63 const newVideoPrivacies = []
64 for (const p of this.videoPrivacies) {
65 if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p)
66 }
67
68 this.videoPrivacies = newVideoPrivacies
69 }
70
2de96f4d
C
71 this.hydrateFormFromVideo()
72 },
73
74 err => {
75 console.error(err)
76 this.error = 'Cannot fetch video.'
77 }
78 )
e822fdae
C
79 }
80
df98563e
C
81 checkForm () {
82 this.forceCheck()
c24ac1c1 83
df98563e 84 return this.form.valid
c24ac1c1
C
85 }
86
df98563e 87 update () {
c24ac1c1 88 if (this.checkForm() === false) {
df98563e 89 return
c24ac1c1
C
90 }
91
df98563e 92 this.video.patch(this.form.value)
d8e689b8
C
93
94 this.videoService.updateVideo(this.video)
95 .subscribe(
96 () => {
df98563e 97 this.notificationsService.success('Success', 'Video updated.')
0a6658fd 98 this.router.navigate([ '/videos/watch', this.video.uuid ])
d8e689b8
C
99 },
100
101 err => {
df98563e
C
102 this.error = 'Cannot update the video.'
103 console.error(err)
d8e689b8 104 }
df98563e 105 )
e822fdae 106
dc8bc31b 107 }
e54163c2 108
df98563e
C
109 private hydrateFormFromVideo () {
110 this.form.patchValue(this.video.toJSON())
d8e689b8 111 }
dc8bc31b 112}