]>
Commit | Line | Data |
---|---|---|
df98563e C |
1 | import { Component, ElementRef, OnInit } from '@angular/core' |
2 | import { FormBuilder, FormGroup } from '@angular/forms' | |
3 | import { ActivatedRoute, Router } from '@angular/router' | |
dc8bc31b | 4 | |
df98563e C |
5 | import { FileUploader } from 'ng2-file-upload/ng2-file-upload' |
6 | import { NotificationsService } from 'angular2-notifications' | |
8140a704 | 7 | |
df98563e | 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 | |
df98563e C |
17 | } from '../../shared' |
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 { |
df98563e C |
27 | tags: string[] = [] |
28 | videoCategories = [] | |
29 | videoLicences = [] | |
30 | videoLanguages = [] | |
31 | video: Video | |
4b2f33f3 | 32 | |
df98563e C |
33 | tagValidators = VIDEO_TAGS.VALIDATORS |
34 | tagValidatorsMessages = VIDEO_TAGS.MESSAGES | |
3758da94 | 35 | |
df98563e C |
36 | error: string = null |
37 | form: FormGroup | |
4b2f33f3 | 38 | formErrors = { |
e822fdae | 39 | name: '', |
6e07c3de | 40 | category: '', |
d07137b9 | 41 | licence: '', |
db216afd | 42 | language: '', |
3758da94 | 43 | description: '' |
df98563e | 44 | } |
4b2f33f3 C |
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 |
df98563e | 51 | } |
dc8bc31b | 52 | |
df98563e | 53 | fileError = '' |
bf57d5ee | 54 | |
df98563e | 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 | 63 | ) { |
df98563e | 64 | super() |
4b2f33f3 | 65 | } |
dc8bc31b | 66 | |
df98563e | 67 | buildForm () { |
4b2f33f3 C |
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: [ '' ] |
df98563e | 76 | }) |
4b2f33f3 | 77 | |
df98563e | 78 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
e822fdae C |
79 | } |
80 | ||
df98563e C |
81 | ngOnInit () { |
82 | this.buildForm() | |
d8e689b8 | 83 | |
df98563e C |
84 | this.videoCategories = this.videoService.videoCategories |
85 | this.videoLicences = this.videoService.videoLicences | |
86 | this.videoLanguages = this.videoService.videoLanguages | |
6e07c3de | 87 | |
0a6658fd C |
88 | const uuid: string = this.route.snapshot.params['uuid'] |
89 | this.videoService.getVideo(uuid) | |
d8e689b8 C |
90 | .subscribe( |
91 | video => { | |
df98563e | 92 | this.video = video |
db216afd | 93 | |
df98563e | 94 | this.hydrateFormFromVideo() |
d8e689b8 | 95 | }, |
4b2f33f3 | 96 | |
df98563e C |
97 | err => { |
98 | console.error(err) | |
99 | this.error = 'Cannot fetch video.' | |
100 | } | |
101 | ) | |
e822fdae C |
102 | } |
103 | ||
df98563e C |
104 | checkForm () { |
105 | this.forceCheck() | |
c24ac1c1 | 106 | |
df98563e | 107 | return this.form.valid |
c24ac1c1 C |
108 | } |
109 | ||
df98563e | 110 | update () { |
c24ac1c1 | 111 | if (this.checkForm() === false) { |
df98563e | 112 | return |
c24ac1c1 C |
113 | } |
114 | ||
df98563e | 115 | this.video.patch(this.form.value) |
d8e689b8 C |
116 | |
117 | this.videoService.updateVideo(this.video) | |
118 | .subscribe( | |
119 | () => { | |
df98563e | 120 | this.notificationsService.success('Success', 'Video updated.') |
0a6658fd | 121 | this.router.navigate([ '/videos/watch', this.video.uuid ]) |
d8e689b8 C |
122 | }, |
123 | ||
124 | err => { | |
df98563e C |
125 | this.error = 'Cannot update the video.' |
126 | console.error(err) | |
d8e689b8 | 127 | } |
df98563e | 128 | ) |
e822fdae | 129 | |
dc8bc31b | 130 | } |
e54163c2 | 131 | |
df98563e C |
132 | private hydrateFormFromVideo () { |
133 | this.form.patchValue(this.video.toJSON()) | |
d8e689b8 | 134 | } |
dc8bc31b | 135 | } |