]>
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 | 24 | form: FormGroup |
ff249f49 C |
25 | formErrors: { [ id: string ]: string } = {} |
26 | validationMessages: ValidatorMessage = {} | |
27 | videoPrivacies = [] | |
15a7387d | 28 | userVideoChannels = [] |
dc8bc31b | 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, |
15a7387d C |
36 | private videoService: VideoService, |
37 | private authService: AuthService | |
4b2f33f3 | 38 | ) { |
df98563e | 39 | super() |
4b2f33f3 | 40 | } |
dc8bc31b | 41 | |
df98563e | 42 | buildForm () { |
ff249f49 | 43 | this.form = this.formBuilder.group({}) |
df98563e | 44 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
e822fdae C |
45 | } |
46 | ||
df98563e C |
47 | ngOnInit () { |
48 | this.buildForm() | |
d8e689b8 | 49 | |
15a7387d C |
50 | this.serverService.videoPrivaciesLoaded |
51 | .subscribe( | |
52 | () => this.videoPrivacies = this.serverService.getVideoPrivacies() | |
53 | ) | |
54 | ||
55 | populateAsyncUserVideoChannels(this.authService, this.userVideoChannels) | |
6e07c3de | 56 | |
0a6658fd | 57 | const uuid: string = this.route.snapshot.params['uuid'] |
2de96f4d C |
58 | this.videoService.getVideo(uuid) |
59 | .switchMap(video => { | |
60 | return this.videoService | |
61 | .loadCompleteDescription(video.descriptionPath) | |
62 | .do(description => video.description = description) | |
63 | .map(() => video) | |
64 | }) | |
65 | .subscribe( | |
66 | video => { | |
67 | this.video = new VideoEdit(video) | |
68 | ||
ff249f49 | 69 | // We cannot set private a video that was not private |
fd45e8f4 C |
70 | if (video.privacy !== VideoPrivacy.PRIVATE) { |
71 | const newVideoPrivacies = [] | |
72 | for (const p of this.videoPrivacies) { | |
73 | if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p) | |
74 | } | |
75 | ||
76 | this.videoPrivacies = newVideoPrivacies | |
77 | } | |
78 | ||
2de96f4d C |
79 | this.hydrateFormFromVideo() |
80 | }, | |
81 | ||
82 | err => { | |
83 | console.error(err) | |
ce5496d6 | 84 | this.notificationsService.error('Error', err.message) |
2de96f4d C |
85 | } |
86 | ) | |
e822fdae C |
87 | } |
88 | ||
df98563e C |
89 | checkForm () { |
90 | this.forceCheck() | |
c24ac1c1 | 91 | |
df98563e | 92 | return this.form.valid |
c24ac1c1 C |
93 | } |
94 | ||
df98563e | 95 | update () { |
c24ac1c1 | 96 | if (this.checkForm() === false) { |
df98563e | 97 | return |
c24ac1c1 C |
98 | } |
99 | ||
df98563e | 100 | this.video.patch(this.form.value) |
d8e689b8 C |
101 | |
102 | this.videoService.updateVideo(this.video) | |
103 | .subscribe( | |
104 | () => { | |
df98563e | 105 | this.notificationsService.success('Success', 'Video updated.') |
0a6658fd | 106 | this.router.navigate([ '/videos/watch', this.video.uuid ]) |
d8e689b8 C |
107 | }, |
108 | ||
109 | err => { | |
ce5496d6 | 110 | this.notificationsService.error('Error', err.message) |
df98563e | 111 | console.error(err) |
d8e689b8 | 112 | } |
df98563e | 113 | ) |
e822fdae | 114 | |
dc8bc31b | 115 | } |
e54163c2 | 116 | |
df98563e C |
117 | private hydrateFormFromVideo () { |
118 | this.form.patchValue(this.video.toJSON()) | |
d8e689b8 | 119 | } |
dc8bc31b | 120 | } |