]>
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 { AuthService } from '../../core/auth' | |
9 | import { FormReactive } from '../../shared' | |
10 | import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message' | |
11 | import { populateAsyncUserVideoChannels } from '../../shared/misc/utils' | |
12 | import { VideoEdit } from '../../shared/video/video-edit.model' | |
13 | import { VideoService } from '../../shared/video/video.service' | |
14 | ||
15 | @Component({ | |
16 | selector: 'my-videos-update', | |
17 | styleUrls: [ './shared/video-edit.component.scss' ], | |
18 | templateUrl: './video-update.component.html' | |
19 | }) | |
20 | ||
21 | export class VideoUpdateComponent extends FormReactive implements OnInit { | |
22 | video: VideoEdit | |
23 | ||
24 | error: string = null | |
25 | form: FormGroup | |
26 | formErrors: { [ id: string ]: string } = {} | |
27 | validationMessages: ValidatorMessage = {} | |
28 | videoPrivacies = [] | |
29 | userVideoChannels = [] | |
30 | ||
31 | constructor ( | |
32 | private formBuilder: FormBuilder, | |
33 | private route: ActivatedRoute, | |
34 | private router: Router, | |
35 | private notificationsService: NotificationsService, | |
36 | private serverService: ServerService, | |
37 | private videoService: VideoService, | |
38 | private authService: AuthService | |
39 | ) { | |
40 | super() | |
41 | } | |
42 | ||
43 | buildForm () { | |
44 | this.form = this.formBuilder.group({}) | |
45 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) | |
46 | } | |
47 | ||
48 | ngOnInit () { | |
49 | this.buildForm() | |
50 | ||
51 | this.serverService.videoPrivaciesLoaded | |
52 | .subscribe( | |
53 | () => this.videoPrivacies = this.serverService.getVideoPrivacies() | |
54 | ) | |
55 | ||
56 | populateAsyncUserVideoChannels(this.authService, this.userVideoChannels) | |
57 | ||
58 | const uuid: string = this.route.snapshot.params['uuid'] | |
59 | this.videoService.getVideo(uuid) | |
60 | .switchMap(video => { | |
61 | return this.videoService | |
62 | .loadCompleteDescription(video.descriptionPath) | |
63 | .do(description => video.description = description) | |
64 | .map(() => video) | |
65 | }) | |
66 | .subscribe( | |
67 | video => { | |
68 | this.video = new VideoEdit(video) | |
69 | ||
70 | // We cannot set private a video that was not private | |
71 | if (video.privacy !== 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.error = 'Cannot fetch video.' | |
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.videoService.updateVideo(this.video) | |
104 | .subscribe( | |
105 | () => { | |
106 | this.notificationsService.success('Success', 'Video updated.') | |
107 | this.router.navigate([ '/videos/watch', this.video.uuid ]) | |
108 | }, | |
109 | ||
110 | err => { | |
111 | this.error = 'Cannot update the video.' | |
112 | console.error(err) | |
113 | } | |
114 | ) | |
115 | ||
116 | } | |
117 | ||
118 | private hydrateFormFromVideo () { | |
119 | this.form.patchValue(this.video.toJSON()) | |
120 | } | |
121 | } |