]>
Commit | Line | Data |
---|---|---|
230809ef | 1 | import { Component, ElementRef, OnInit } from '@angular/core'; |
4b2f33f3 | 2 | import { FormBuilder, FormGroup } from '@angular/forms'; |
d8e689b8 | 3 | import { ActivatedRoute, Router } from '@angular/router'; |
dc8bc31b | 4 | |
ab32b0fc | 5 | import { FileUploader } from 'ng2-file-upload/ng2-file-upload'; |
7ddd02c9 | 6 | import { NotificationsService } from 'angular2-notifications'; |
8140a704 | 7 | |
693b1aba | 8 | import { AuthService } from '../../core'; |
6e07c3de C |
9 | import { |
10 | FormReactive, | |
11 | VIDEO_NAME, | |
12 | VIDEO_CATEGORY, | |
d07137b9 | 13 | VIDEO_LICENCE, |
db216afd | 14 | VIDEO_LANGUAGE, |
6e07c3de C |
15 | VIDEO_DESCRIPTION, |
16 | VIDEO_TAGS | |
17 | } from '../../shared'; | |
d8e689b8 | 18 | import { Video, VideoService } from '../shared'; |
1553e15d | 19 | |
dc8bc31b | 20 | @Component({ |
d8e689b8 C |
21 | selector: 'my-videos-update', |
22 | styleUrls: [ './video-edit.component.scss' ], | |
23 | templateUrl: './video-update.component.html' | |
dc8bc31b C |
24 | }) |
25 | ||
d8e689b8 | 26 | export class VideoUpdateComponent extends FormReactive implements OnInit { |
4b2f33f3 | 27 | tags: string[] = []; |
6e07c3de | 28 | videoCategories = []; |
d07137b9 | 29 | videoLicences = []; |
db216afd | 30 | videoLanguages = []; |
d8e689b8 | 31 | video: Video; |
4b2f33f3 | 32 | |
3758da94 C |
33 | tagValidators = VIDEO_TAGS.VALIDATORS; |
34 | tagValidatorsMessages = VIDEO_TAGS.MESSAGES; | |
35 | ||
4b2f33f3 C |
36 | error: string = null; |
37 | form: FormGroup; | |
38 | formErrors = { | |
e822fdae | 39 | name: '', |
6e07c3de | 40 | category: '', |
d07137b9 | 41 | licence: '', |
db216afd | 42 | language: '', |
3758da94 | 43 | description: '' |
4b2f33f3 C |
44 | }; |
45 | validationMessages = { | |
46 | name: VIDEO_NAME.MESSAGES, | |
6e07c3de | 47 | category: VIDEO_CATEGORY.MESSAGES, |
d07137b9 | 48 | licence: VIDEO_LICENCE.MESSAGES, |
db216afd | 49 | language: VIDEO_LANGUAGE.MESSAGES, |
3758da94 | 50 | description: VIDEO_DESCRIPTION.MESSAGES |
e822fdae | 51 | }; |
dc8bc31b | 52 | |
bf57d5ee C |
53 | fileError = ''; |
54 | ||
1553e15d | 55 | constructor( |
9bfe96e1 | 56 | private authService: AuthService, |
4fd8aa32 | 57 | private elementRef: ElementRef, |
4b2f33f3 | 58 | private formBuilder: FormBuilder, |
d8e689b8 | 59 | private route: ActivatedRoute, |
7ddd02c9 | 60 | private router: Router, |
6e07c3de C |
61 | private notificationsService: NotificationsService, |
62 | private videoService: VideoService | |
4b2f33f3 C |
63 | ) { |
64 | super(); | |
65 | } | |
dc8bc31b | 66 | |
4b2f33f3 C |
67 | buildForm() { |
68 | this.form = this.formBuilder.group({ | |
69 | name: [ '', VIDEO_NAME.VALIDATORS ], | |
92fb909c | 70 | nsfw: [ false ], |
6e07c3de | 71 | category: [ '', VIDEO_CATEGORY.VALIDATORS ], |
d07137b9 | 72 | licence: [ '', VIDEO_LICENCE.VALIDATORS ], |
db216afd | 73 | language: [ '', VIDEO_LANGUAGE.VALIDATORS ], |
4b2f33f3 | 74 | description: [ '', VIDEO_DESCRIPTION.VALIDATORS ], |
3758da94 | 75 | tags: [ '' ] |
4b2f33f3 C |
76 | }); |
77 | ||
78 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)); | |
e822fdae C |
79 | } |
80 | ||
dc8bc31b | 81 | ngOnInit() { |
d8e689b8 C |
82 | this.buildForm(); |
83 | ||
6e07c3de | 84 | this.videoCategories = this.videoService.videoCategories; |
d07137b9 | 85 | this.videoLicences = this.videoService.videoLicences; |
db216afd | 86 | this.videoLanguages = this.videoService.videoLanguages; |
6e07c3de | 87 | |
d8e689b8 C |
88 | const id = this.route.snapshot.params['id']; |
89 | this.videoService.getVideo(id) | |
90 | .subscribe( | |
91 | video => { | |
92 | this.video = video; | |
db216afd | 93 | |
d8e689b8 C |
94 | this.hydrateFormFromVideo(); |
95 | }, | |
4b2f33f3 | 96 | |
d8e689b8 C |
97 | err => this.error = 'Cannot fetch video.' |
98 | ); | |
e822fdae C |
99 | } |
100 | ||
d8e689b8 | 101 | update() { |
d8e689b8 C |
102 | this.video.patch(this.form.value); |
103 | ||
104 | this.videoService.updateVideo(this.video) | |
105 | .subscribe( | |
106 | () => { | |
107 | this.notificationsService.success('Success', 'Video updated.'); | |
108 | this.router.navigate([ '/videos/watch', this.video.id ]); | |
109 | }, | |
110 | ||
111 | err => { | |
112 | this.error = 'Cannot update the video.'; | |
113 | console.error(err); | |
114 | } | |
115 | ); | |
e822fdae | 116 | |
dc8bc31b | 117 | } |
e54163c2 | 118 | |
d8e689b8 C |
119 | private hydrateFormFromVideo() { |
120 | this.form.patchValue(this.video.toJSON()); | |
121 | } | |
dc8bc31b | 122 | } |