]>
Commit | Line | Data |
---|---|---|
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 { FormReactive } from '../../shared' | |
9 | import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message' | |
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 | constructor ( | |
29 | private formBuilder: FormBuilder, | |
30 | private route: ActivatedRoute, | |
31 | private router: Router, | |
32 | private notificationsService: NotificationsService, | |
33 | private serverService: ServerService, | |
34 | private videoService: VideoService | |
35 | ) { | |
36 | super() | |
37 | } | |
38 | ||
39 | buildForm () { | |
40 | this.form = this.formBuilder.group({}) | |
41 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) | |
42 | } | |
43 | ||
44 | ngOnInit () { | |
45 | this.buildForm() | |
46 | ||
47 | this.videoPrivacies = this.serverService.getVideoPrivacies() | |
48 | ||
49 | const uuid: string = this.route.snapshot.params['uuid'] | |
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 | ||
61 | // We cannot set private a video that was not private | |
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 | ||
71 | this.hydrateFormFromVideo() | |
72 | }, | |
73 | ||
74 | err => { | |
75 | console.error(err) | |
76 | this.error = 'Cannot fetch video.' | |
77 | } | |
78 | ) | |
79 | } | |
80 | ||
81 | checkForm () { | |
82 | this.forceCheck() | |
83 | ||
84 | return this.form.valid | |
85 | } | |
86 | ||
87 | update () { | |
88 | if (this.checkForm() === false) { | |
89 | return | |
90 | } | |
91 | ||
92 | this.video.patch(this.form.value) | |
93 | ||
94 | this.videoService.updateVideo(this.video) | |
95 | .subscribe( | |
96 | () => { | |
97 | this.notificationsService.success('Success', 'Video updated.') | |
98 | this.router.navigate([ '/videos/watch', this.video.uuid ]) | |
99 | }, | |
100 | ||
101 | err => { | |
102 | this.error = 'Cannot update the video.' | |
103 | console.error(err) | |
104 | } | |
105 | ) | |
106 | ||
107 | } | |
108 | ||
109 | private hydrateFormFromVideo () { | |
110 | this.form.patchValue(this.video.toJSON()) | |
111 | } | |
112 | } |