]>
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 | 5 | import 'rxjs/add/observable/forkJoin' |
63c4db6d | 6 | import { VideoPrivacy } from '../../../../../shared/models/videos' |
db7af09b | 7 | import { ServerService } from '../../core' |
15a7387d | 8 | import { AuthService } from '../../core/auth' |
4cc66133 | 9 | import { FormReactive } from '../../shared' |
63c4db6d | 10 | import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message' |
15a7387d | 11 | import { populateAsyncUserVideoChannels } from '../../shared/misc/utils' |
202f6b6c | 12 | import { VideoEdit } from '../../shared/video/video-edit.model' |
4cc66133 | 13 | import { VideoService } from '../../shared/video/video.service' |
1553e15d | 14 | |
dc8bc31b | 15 | @Component({ |
d8e689b8 | 16 | selector: 'my-videos-update', |
80958c78 | 17 | styleUrls: [ './shared/video-edit.component.scss' ], |
d8e689b8 | 18 | templateUrl: './video-update.component.html' |
dc8bc31b C |
19 | }) |
20 | ||
d8e689b8 | 21 | export class VideoUpdateComponent extends FormReactive implements OnInit { |
404b54e1 | 22 | video: VideoEdit |
4b2f33f3 | 23 | |
df98563e C |
24 | error: string = null |
25 | form: FormGroup | |
ff249f49 C |
26 | formErrors: { [ id: string ]: string } = {} |
27 | validationMessages: ValidatorMessage = {} | |
28 | videoPrivacies = [] | |
15a7387d | 29 | userVideoChannels = [] |
dc8bc31b | 30 | |
df98563e | 31 | constructor ( |
4b2f33f3 | 32 | private formBuilder: FormBuilder, |
d8e689b8 | 33 | private route: ActivatedRoute, |
7ddd02c9 | 34 | private router: Router, |
6e07c3de | 35 | private notificationsService: NotificationsService, |
db7af09b | 36 | private serverService: ServerService, |
15a7387d C |
37 | private videoService: VideoService, |
38 | private authService: AuthService | |
4b2f33f3 | 39 | ) { |
df98563e | 40 | super() |
4b2f33f3 | 41 | } |
dc8bc31b | 42 | |
df98563e | 43 | buildForm () { |
ff249f49 | 44 | this.form = this.formBuilder.group({}) |
df98563e | 45 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
e822fdae C |
46 | } |
47 | ||
df98563e C |
48 | ngOnInit () { |
49 | this.buildForm() | |
d8e689b8 | 50 | |
15a7387d C |
51 | this.serverService.videoPrivaciesLoaded |
52 | .subscribe( | |
53 | () => this.videoPrivacies = this.serverService.getVideoPrivacies() | |
54 | ) | |
55 | ||
56 | populateAsyncUserVideoChannels(this.authService, this.userVideoChannels) | |
6e07c3de | 57 | |
0a6658fd | 58 | const uuid: string = this.route.snapshot.params['uuid'] |
2de96f4d C |
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 | ||
ff249f49 | 70 | // We cannot set private a video that was not private |
fd45e8f4 C |
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 | ||
2de96f4d C |
80 | this.hydrateFormFromVideo() |
81 | }, | |
82 | ||
83 | err => { | |
84 | console.error(err) | |
85 | this.error = 'Cannot fetch video.' | |
86 | } | |
87 | ) | |
e822fdae C |
88 | } |
89 | ||
df98563e C |
90 | checkForm () { |
91 | this.forceCheck() | |
c24ac1c1 | 92 | |
df98563e | 93 | return this.form.valid |
c24ac1c1 C |
94 | } |
95 | ||
df98563e | 96 | update () { |
c24ac1c1 | 97 | if (this.checkForm() === false) { |
df98563e | 98 | return |
c24ac1c1 C |
99 | } |
100 | ||
df98563e | 101 | this.video.patch(this.form.value) |
d8e689b8 C |
102 | |
103 | this.videoService.updateVideo(this.video) | |
104 | .subscribe( | |
105 | () => { | |
df98563e | 106 | this.notificationsService.success('Success', 'Video updated.') |
0a6658fd | 107 | this.router.navigate([ '/videos/watch', this.video.uuid ]) |
d8e689b8 C |
108 | }, |
109 | ||
110 | err => { | |
df98563e C |
111 | this.error = 'Cannot update the video.' |
112 | console.error(err) | |
d8e689b8 | 113 | } |
df98563e | 114 | ) |
e822fdae | 115 | |
dc8bc31b | 116 | } |
e54163c2 | 117 | |
df98563e C |
118 | private hydrateFormFromVideo () { |
119 | this.form.patchValue(this.video.toJSON()) | |
d8e689b8 | 120 | } |
dc8bc31b | 121 | } |