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