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