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