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