]>
Commit | Line | Data |
---|---|---|
db7af09b | 1 | import { Component, OnInit } from '@angular/core' |
df98563e C |
2 | import { FormBuilder, FormGroup } from '@angular/forms' |
3 | import { ActivatedRoute, Router } from '@angular/router' | |
df98563e | 4 | import { NotificationsService } from 'angular2-notifications' |
202f6b6c C |
5 | import 'rxjs/add/observable/forkJoin' |
6 | import { VideoPrivacy } from '../../../../../shared/models/videos/video-privacy.enum' | |
db7af09b | 7 | import { ServerService } from '../../core' |
4cc66133 | 8 | import { FormReactive } from '../../shared' |
ff249f49 | 9 | import { ValidatorMessage } from '../../shared/forms/form-validators' |
202f6b6c | 10 | import { VideoEdit } from '../../shared/video/video-edit.model' |
4cc66133 | 11 | import { 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 | 19 | export 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 | fileError = '' |
bf57d5ee | 29 | |
df98563e | 30 | constructor ( |
4b2f33f3 | 31 | private formBuilder: FormBuilder, |
d8e689b8 | 32 | private route: ActivatedRoute, |
7ddd02c9 | 33 | private router: Router, |
6e07c3de | 34 | private notificationsService: NotificationsService, |
db7af09b | 35 | private serverService: ServerService, |
6e07c3de | 36 | private videoService: VideoService |
4b2f33f3 | 37 | ) { |
df98563e | 38 | super() |
4b2f33f3 | 39 | } |
dc8bc31b | 40 | |
df98563e | 41 | buildForm () { |
ff249f49 | 42 | this.form = this.formBuilder.group({}) |
df98563e | 43 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
e822fdae C |
44 | } |
45 | ||
df98563e C |
46 | ngOnInit () { |
47 | this.buildForm() | |
d8e689b8 | 48 | |
fd45e8f4 | 49 | this.videoPrivacies = this.serverService.getVideoPrivacies() |
6e07c3de | 50 | |
0a6658fd | 51 | const uuid: string = this.route.snapshot.params['uuid'] |
2de96f4d C |
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 | ||
ff249f49 | 63 | // We cannot set private a video that was not private |
fd45e8f4 C |
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 | ||
2de96f4d C |
73 | this.hydrateFormFromVideo() |
74 | }, | |
75 | ||
76 | err => { | |
77 | console.error(err) | |
78 | this.error = 'Cannot fetch video.' | |
79 | } | |
80 | ) | |
e822fdae C |
81 | } |
82 | ||
df98563e C |
83 | checkForm () { |
84 | this.forceCheck() | |
c24ac1c1 | 85 | |
df98563e | 86 | return this.form.valid |
c24ac1c1 C |
87 | } |
88 | ||
df98563e | 89 | update () { |
c24ac1c1 | 90 | if (this.checkForm() === false) { |
df98563e | 91 | return |
c24ac1c1 C |
92 | } |
93 | ||
df98563e | 94 | this.video.patch(this.form.value) |
d8e689b8 C |
95 | |
96 | this.videoService.updateVideo(this.video) | |
97 | .subscribe( | |
98 | () => { | |
df98563e | 99 | this.notificationsService.success('Success', 'Video updated.') |
0a6658fd | 100 | this.router.navigate([ '/videos/watch', this.video.uuid ]) |
d8e689b8 C |
101 | }, |
102 | ||
103 | err => { | |
df98563e C |
104 | this.error = 'Cannot update the video.' |
105 | console.error(err) | |
d8e689b8 | 106 | } |
df98563e | 107 | ) |
e822fdae | 108 | |
dc8bc31b | 109 | } |
e54163c2 | 110 | |
df98563e C |
111 | private hydrateFormFromVideo () { |
112 | this.form.patchValue(this.video.toJSON()) | |
d8e689b8 | 113 | } |
dc8bc31b | 114 | } |