]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/video-add/video-add.component.ts
Client: use templateUrl/styleUrls instead of require
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / video-add / video-add.component.ts
CommitLineData
230809ef 1import { Component, ElementRef, OnInit } from '@angular/core';
4b2f33f3 2import { FormBuilder, FormGroup } from '@angular/forms';
00a44645 3import { Router } from '@angular/router';
dc8bc31b 4
ab32b0fc 5import { FileUploader } from 'ng2-file-upload/ng2-file-upload';
8140a704 6
4b2f33f3 7import { AuthService, FormReactive, VIDEO_NAME, VIDEO_DESCRIPTION, VIDEO_TAGS } from '../../shared';
1553e15d 8
dc8bc31b
C
9@Component({
10 selector: 'my-videos-add',
ec8d8440
C
11 styleUrls: [ './video-add.component.scss' ],
12 templateUrl: './video-add.component.html'
dc8bc31b
C
13})
14
4b2f33f3
C
15export class VideoAddComponent extends FormReactive implements OnInit {
16 tags: string[] = [];
e822fdae 17 uploader: FileUploader;
4b2f33f3
C
18
19 error: string = null;
20 form: FormGroup;
21 formErrors = {
e822fdae 22 name: '',
4b2f33f3
C
23 description: '',
24 currentTag: ''
25 };
26 validationMessages = {
27 name: VIDEO_NAME.MESSAGES,
28 description: VIDEO_DESCRIPTION.MESSAGES,
29 currentTag: VIDEO_TAGS.MESSAGES
e822fdae 30 };
dc8bc31b 31
1553e15d 32 constructor(
9bfe96e1 33 private authService: AuthService,
4fd8aa32 34 private elementRef: ElementRef,
4b2f33f3 35 private formBuilder: FormBuilder,
9bfe96e1 36 private router: Router
4b2f33f3
C
37 ) {
38 super();
39 }
dc8bc31b 40
e822fdae
C
41 get filename() {
42 if (this.uploader.queue.length === 0) {
43 return null;
44 }
45
46 return this.uploader.queue[0].file.name;
47 }
48
4b2f33f3
C
49 buildForm() {
50 this.form = this.formBuilder.group({
51 name: [ '', VIDEO_NAME.VALIDATORS ],
52 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
53 currentTag: [ '', VIDEO_TAGS.VALIDATORS ]
54 });
55
56 this.form.valueChanges.subscribe(data => this.onValueChanged(data));
e822fdae
C
57 }
58
59 getInvalidFieldsTitle() {
60 let title = '';
4b2f33f3
C
61 const nameControl = this.form.controls['name'];
62 const descriptionControl = this.form.controls['description'];
e822fdae
C
63
64 if (!nameControl.valid) {
65 title += 'A name is required\n';
66 }
67
4b2f33f3 68 if (this.tags.length === 0) {
e822fdae
C
69 title += 'At least one tag is required\n';
70 }
71
72 if (this.filename === null) {
73 title += 'A file is required\n';
74 }
75
76 if (!descriptionControl.valid) {
77 title += 'A description is required\n';
78 }
79
80 return title;
81 }
82
dc8bc31b 83 ngOnInit() {
e822fdae
C
84 this.uploader = new FileUploader({
85 authToken: this.authService.getRequestHeaderValue(),
86 queueLimit: 1,
98b01bac 87 url: '/api/v1/videos',
e822fdae 88 removeAfterUpload: true
dc8bc31b 89 });
e822fdae
C
90
91 this.uploader.onBuildItemForm = (item, form) => {
4b2f33f3
C
92 const name = this.form.value['name'];
93 const description = this.form.value['description'];
94
95 form.append('name', name);
96 form.append('description', description);
e822fdae 97
4b2f33f3
C
98 for (let i = 0; i < this.tags.length; i++) {
99 form.append(`tags[${i}]`, this.tags[i]);
e822fdae
C
100 }
101 };
4b2f33f3
C
102
103 this.buildForm();
e822fdae
C
104 }
105
106 onTagKeyPress(event: KeyboardEvent) {
4b2f33f3
C
107 const currentTag = this.form.value['currentTag'];
108
e822fdae
C
109 // Enter press
110 if (event.keyCode === 13) {
111 // Check if the tag is valid and does not already exist
112 if (
4b2f33f3
C
113 currentTag !== '' &&
114 this.form.controls['currentTag'].valid &&
115 this.tags.indexOf(currentTag) === -1
e822fdae 116 ) {
4b2f33f3
C
117 this.tags.push(currentTag);
118 this.form.patchValue({ currentTag: '' });
119
120 if (this.tags.length >= 3) {
121 this.form.get('currentTag').disable();
122 }
e822fdae
C
123 }
124 }
125 }
126
127 removeFile() {
128 this.uploader.clearQueue();
129 }
130
131 removeTag(tag: string) {
4b2f33f3 132 this.tags.splice(this.tags.indexOf(tag), 1);
dc8bc31b
C
133 }
134
e822fdae
C
135 upload() {
136 const item = this.uploader.queue[0];
137 // TODO: wait for https://github.com/valor-software/ng2-file-upload/pull/242
138 item.alias = 'videofile';
139
140 item.onSuccess = () => {
141 console.log('Video uploaded.');
142
143 // Print all the videos once it's finished
c6de16eb 144 this.router.navigate(['/videos/list']);
e822fdae
C
145 };
146
147 item.onError = (response: string, status: number) => {
bd5c83a8
C
148 // We need to handle manually these cases beceause we use the FileUpload component
149 if (status === 400) {
150 this.error = response;
151 } else if (status === 401) {
152 this.error = 'Access token was expired, refreshing token...';
153 this.authService.refreshAccessToken().subscribe(
154 () => {
155 // Update the uploader request header
156 this.uploader.authToken = this.authService.getRequestHeaderValue();
157 this.error += ' access token refreshed. Please retry your request.';
158 }
159 );
160 } else {
161 this.error = 'Unknow error';
162 console.error(this.error);
163 }
e822fdae
C
164 };
165
166
167 this.uploader.uploadAll();
dc8bc31b
C
168 }
169}