]>
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' |
6e07c3de C |
8 | import { |
9 | FormReactive, | |
6e07c3de C |
10 | VIDEO_CATEGORY, |
11 | VIDEO_DESCRIPTION, | |
202f6b6c C |
12 | VIDEO_LANGUAGE, |
13 | VIDEO_LICENCE, | |
14 | VIDEO_NAME, | |
15 | VIDEO_PRIVACY, | |
16 | VIDEO_TAGS | |
df98563e | 17 | } from '../../shared' |
202f6b6c C |
18 | import { VideoService } from '../../shared/video/video.service' |
19 | import { VideoEdit } from '../../shared/video/video-edit.model' | |
1553e15d | 20 | |
dc8bc31b | 21 | @Component({ |
d8e689b8 | 22 | selector: 'my-videos-update', |
80958c78 | 23 | styleUrls: [ './shared/video-edit.component.scss' ], |
d8e689b8 | 24 | templateUrl: './video-update.component.html' |
dc8bc31b C |
25 | }) |
26 | ||
d8e689b8 | 27 | export class VideoUpdateComponent extends FormReactive implements OnInit { |
df98563e C |
28 | tags: string[] = [] |
29 | videoCategories = [] | |
30 | videoLicences = [] | |
31 | videoLanguages = [] | |
fd45e8f4 | 32 | videoPrivacies = [] |
404b54e1 | 33 | video: VideoEdit |
4b2f33f3 | 34 | |
df98563e C |
35 | tagValidators = VIDEO_TAGS.VALIDATORS |
36 | tagValidatorsMessages = VIDEO_TAGS.MESSAGES | |
3758da94 | 37 | |
df98563e C |
38 | error: string = null |
39 | form: FormGroup | |
4b2f33f3 | 40 | formErrors = { |
e822fdae | 41 | name: '', |
fd45e8f4 | 42 | privacy: '', |
6e07c3de | 43 | category: '', |
d07137b9 | 44 | licence: '', |
db216afd | 45 | language: '', |
3758da94 | 46 | description: '' |
df98563e | 47 | } |
4b2f33f3 C |
48 | validationMessages = { |
49 | name: VIDEO_NAME.MESSAGES, | |
fd45e8f4 | 50 | privacy: VIDEO_PRIVACY.MESSAGES, |
6e07c3de | 51 | category: VIDEO_CATEGORY.MESSAGES, |
d07137b9 | 52 | licence: VIDEO_LICENCE.MESSAGES, |
db216afd | 53 | language: VIDEO_LANGUAGE.MESSAGES, |
3758da94 | 54 | description: VIDEO_DESCRIPTION.MESSAGES |
df98563e | 55 | } |
dc8bc31b | 56 | |
df98563e | 57 | fileError = '' |
bf57d5ee | 58 | |
df98563e | 59 | constructor ( |
4b2f33f3 | 60 | private formBuilder: FormBuilder, |
d8e689b8 | 61 | private route: ActivatedRoute, |
7ddd02c9 | 62 | private router: Router, |
6e07c3de | 63 | private notificationsService: NotificationsService, |
db7af09b | 64 | private serverService: ServerService, |
6e07c3de | 65 | private videoService: VideoService |
4b2f33f3 | 66 | ) { |
df98563e | 67 | super() |
4b2f33f3 | 68 | } |
dc8bc31b | 69 | |
df98563e | 70 | buildForm () { |
4b2f33f3 C |
71 | this.form = this.formBuilder.group({ |
72 | name: [ '', VIDEO_NAME.VALIDATORS ], | |
fd45e8f4 | 73 | privacy: [ '', VIDEO_PRIVACY.VALIDATORS ], |
92fb909c | 74 | nsfw: [ false ], |
6e07c3de | 75 | category: [ '', VIDEO_CATEGORY.VALIDATORS ], |
d07137b9 | 76 | licence: [ '', VIDEO_LICENCE.VALIDATORS ], |
db216afd | 77 | language: [ '', VIDEO_LANGUAGE.VALIDATORS ], |
4b2f33f3 | 78 | description: [ '', VIDEO_DESCRIPTION.VALIDATORS ], |
3758da94 | 79 | tags: [ '' ] |
df98563e | 80 | }) |
4b2f33f3 | 81 | |
df98563e | 82 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
e822fdae C |
83 | } |
84 | ||
df98563e C |
85 | ngOnInit () { |
86 | this.buildForm() | |
d8e689b8 | 87 | |
db7af09b C |
88 | this.videoCategories = this.serverService.getVideoCategories() |
89 | this.videoLicences = this.serverService.getVideoLicences() | |
90 | this.videoLanguages = this.serverService.getVideoLanguages() | |
fd45e8f4 | 91 | this.videoPrivacies = this.serverService.getVideoPrivacies() |
6e07c3de | 92 | |
0a6658fd | 93 | const uuid: string = this.route.snapshot.params['uuid'] |
4b2f33f3 | 94 | |
2de96f4d C |
95 | this.videoService.getVideo(uuid) |
96 | .switchMap(video => { | |
97 | return this.videoService | |
98 | .loadCompleteDescription(video.descriptionPath) | |
99 | .do(description => video.description = description) | |
100 | .map(() => video) | |
101 | }) | |
102 | .subscribe( | |
103 | video => { | |
104 | this.video = new VideoEdit(video) | |
105 | ||
fd45e8f4 C |
106 | // We cannot set private a video that was not private anymore |
107 | if (video.privacy !== VideoPrivacy.PRIVATE) { | |
108 | const newVideoPrivacies = [] | |
109 | for (const p of this.videoPrivacies) { | |
110 | if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p) | |
111 | } | |
112 | ||
113 | this.videoPrivacies = newVideoPrivacies | |
114 | } | |
115 | ||
2de96f4d C |
116 | this.hydrateFormFromVideo() |
117 | }, | |
118 | ||
119 | err => { | |
120 | console.error(err) | |
121 | this.error = 'Cannot fetch video.' | |
122 | } | |
123 | ) | |
e822fdae C |
124 | } |
125 | ||
df98563e C |
126 | checkForm () { |
127 | this.forceCheck() | |
c24ac1c1 | 128 | |
df98563e | 129 | return this.form.valid |
c24ac1c1 C |
130 | } |
131 | ||
df98563e | 132 | update () { |
c24ac1c1 | 133 | if (this.checkForm() === false) { |
df98563e | 134 | return |
c24ac1c1 C |
135 | } |
136 | ||
df98563e | 137 | this.video.patch(this.form.value) |
d8e689b8 C |
138 | |
139 | this.videoService.updateVideo(this.video) | |
140 | .subscribe( | |
141 | () => { | |
df98563e | 142 | this.notificationsService.success('Success', 'Video updated.') |
0a6658fd | 143 | this.router.navigate([ '/videos/watch', this.video.uuid ]) |
d8e689b8 C |
144 | }, |
145 | ||
146 | err => { | |
df98563e C |
147 | this.error = 'Cannot update the video.' |
148 | console.error(err) | |
d8e689b8 | 149 | } |
df98563e | 150 | ) |
e822fdae | 151 | |
dc8bc31b | 152 | } |
e54163c2 | 153 | |
df98563e C |
154 | private hydrateFormFromVideo () { |
155 | this.form.patchValue(this.video.toJSON()) | |
d8e689b8 | 156 | } |
dc8bc31b | 157 | } |