diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-10-09 14:28:44 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-10-09 14:28:44 +0200 |
commit | a685e25ca05f08ad1b3f7fbaccc8744727bd8d27 (patch) | |
tree | e50fbc2f260a0017113c4668c3c0f3d2fd76ab87 /client/src/app/videos/+video-edit/video-update.component.ts | |
parent | 2ed6a0aedc2d2f6b1ac2fd9a1ac137772831f713 (diff) | |
download | PeerTube-a685e25ca05f08ad1b3f7fbaccc8744727bd8d27.tar.gz PeerTube-a685e25ca05f08ad1b3f7fbaccc8744727bd8d27.tar.zst PeerTube-a685e25ca05f08ad1b3f7fbaccc8744727bd8d27.zip |
Try to optimize frontend
Diffstat (limited to 'client/src/app/videos/+video-edit/video-update.component.ts')
-rw-r--r-- | client/src/app/videos/+video-edit/video-update.component.ts | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/client/src/app/videos/+video-edit/video-update.component.ts b/client/src/app/videos/+video-edit/video-update.component.ts new file mode 100644 index 000000000..141ed3522 --- /dev/null +++ b/client/src/app/videos/+video-edit/video-update.component.ts | |||
@@ -0,0 +1,134 @@ | |||
1 | import { Component, ElementRef, OnInit } from '@angular/core' | ||
2 | import { FormBuilder, FormGroup } from '@angular/forms' | ||
3 | import { ActivatedRoute, Router } from '@angular/router' | ||
4 | |||
5 | import { NotificationsService } from 'angular2-notifications' | ||
6 | |||
7 | import { AuthService } from '../../core' | ||
8 | import { | ||
9 | FormReactive, | ||
10 | VIDEO_NAME, | ||
11 | VIDEO_CATEGORY, | ||
12 | VIDEO_LICENCE, | ||
13 | VIDEO_LANGUAGE, | ||
14 | VIDEO_DESCRIPTION, | ||
15 | VIDEO_TAGS | ||
16 | } from '../../shared' | ||
17 | import { Video, VideoService } from '../shared' | ||
18 | |||
19 | @Component({ | ||
20 | selector: 'my-videos-update', | ||
21 | styleUrls: [ './video-edit.component.scss' ], | ||
22 | templateUrl: './video-update.component.html' | ||
23 | }) | ||
24 | |||
25 | export class VideoUpdateComponent extends FormReactive implements OnInit { | ||
26 | tags: string[] = [] | ||
27 | videoCategories = [] | ||
28 | videoLicences = [] | ||
29 | videoLanguages = [] | ||
30 | video: Video | ||
31 | |||
32 | tagValidators = VIDEO_TAGS.VALIDATORS | ||
33 | tagValidatorsMessages = VIDEO_TAGS.MESSAGES | ||
34 | |||
35 | error: string = null | ||
36 | form: FormGroup | ||
37 | formErrors = { | ||
38 | name: '', | ||
39 | category: '', | ||
40 | licence: '', | ||
41 | language: '', | ||
42 | description: '' | ||
43 | } | ||
44 | validationMessages = { | ||
45 | name: VIDEO_NAME.MESSAGES, | ||
46 | category: VIDEO_CATEGORY.MESSAGES, | ||
47 | licence: VIDEO_LICENCE.MESSAGES, | ||
48 | language: VIDEO_LANGUAGE.MESSAGES, | ||
49 | description: VIDEO_DESCRIPTION.MESSAGES | ||
50 | } | ||
51 | |||
52 | fileError = '' | ||
53 | |||
54 | constructor ( | ||
55 | private authService: AuthService, | ||
56 | private elementRef: ElementRef, | ||
57 | private formBuilder: FormBuilder, | ||
58 | private route: ActivatedRoute, | ||
59 | private router: Router, | ||
60 | private notificationsService: NotificationsService, | ||
61 | private videoService: VideoService | ||
62 | ) { | ||
63 | super() | ||
64 | } | ||
65 | |||
66 | buildForm () { | ||
67 | this.form = this.formBuilder.group({ | ||
68 | name: [ '', VIDEO_NAME.VALIDATORS ], | ||
69 | nsfw: [ false ], | ||
70 | category: [ '', VIDEO_CATEGORY.VALIDATORS ], | ||
71 | licence: [ '', VIDEO_LICENCE.VALIDATORS ], | ||
72 | language: [ '', VIDEO_LANGUAGE.VALIDATORS ], | ||
73 | description: [ '', VIDEO_DESCRIPTION.VALIDATORS ], | ||
74 | tags: [ '' ] | ||
75 | }) | ||
76 | |||
77 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) | ||
78 | } | ||
79 | |||
80 | ngOnInit () { | ||
81 | this.buildForm() | ||
82 | |||
83 | this.videoCategories = this.videoService.videoCategories | ||
84 | this.videoLicences = this.videoService.videoLicences | ||
85 | this.videoLanguages = this.videoService.videoLanguages | ||
86 | |||
87 | const uuid: string = this.route.snapshot.params['uuid'] | ||
88 | this.videoService.getVideo(uuid) | ||
89 | .subscribe( | ||
90 | video => { | ||
91 | this.video = video | ||
92 | |||
93 | this.hydrateFormFromVideo() | ||
94 | }, | ||
95 | |||
96 | err => { | ||
97 | console.error(err) | ||
98 | this.error = 'Cannot fetch video.' | ||
99 | } | ||
100 | ) | ||
101 | } | ||
102 | |||
103 | checkForm () { | ||
104 | this.forceCheck() | ||
105 | |||
106 | return this.form.valid | ||
107 | } | ||
108 | |||
109 | update () { | ||
110 | if (this.checkForm() === false) { | ||
111 | return | ||
112 | } | ||
113 | |||
114 | this.video.patch(this.form.value) | ||
115 | |||
116 | this.videoService.updateVideo(this.video) | ||
117 | .subscribe( | ||
118 | () => { | ||
119 | this.notificationsService.success('Success', 'Video updated.') | ||
120 | this.router.navigate([ '/videos/watch', this.video.uuid ]) | ||
121 | }, | ||
122 | |||
123 | err => { | ||
124 | this.error = 'Cannot update the video.' | ||
125 | console.error(err) | ||
126 | } | ||
127 | ) | ||
128 | |||
129 | } | ||
130 | |||
131 | private hydrateFormFromVideo () { | ||
132 | this.form.patchValue(this.video.toJSON()) | ||
133 | } | ||
134 | } | ||