Client: try to improve ux for the upload form

This commit is contained in:
Chocobozzz 2016-11-27 18:10:26 +01:00
parent 447fde2774
commit bf57d5eebf
4 changed files with 57 additions and 28 deletions

View File

@ -21,4 +21,20 @@ export abstract class FormReactive {
} }
} }
} }
// Same as onValueChanged but force checking even if the field is not dirty
protected forceCheck() {
for (const field in this.formErrors) {
// clear previous error message (if any)
this.formErrors[field] = '';
const control = this.form.get(field);
if (control && !control.valid) {
const messages = this.validationMessages[field];
for (const key in control.errors) {
this.formErrors[field] += messages[key] + ' ';
}
}
}
}
} }

View File

@ -15,7 +15,7 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="tags">Tags</label> <label for="tags">Tags</label> <span class="little-information">(press enter to add the tag)</span>
<input <input
type="text" class="form-control" id="currentTag" type="text" class="form-control" id="currentTag"
formControlName="currentTag" (keyup)="onTagKeyPress($event)" formControlName="currentTag" (keyup)="onTagKeyPress($event)"
@ -32,6 +32,10 @@
</div> </div>
</div> </div>
<div *ngIf="tagsError" class="alert alert-danger">
{{ tagsError }}
</div>
<div class="form-group"> <div class="form-group">
<label for="videofile">File</label> <label for="videofile">File</label>
<div class="btn btn-default btn-file" [ngClass]="{ 'disabled': filename !== null }" > <div class="btn btn-default btn-file" [ngClass]="{ 'disabled': filename !== null }" >
@ -39,6 +43,7 @@
<input <input
type="file" name="videofile" id="videofile" type="file" name="videofile" id="videofile"
ng2FileSelect [uploader]="uploader" [disabled]="filename !== null" ng2FileSelect [uploader]="uploader" [disabled]="filename !== null"
(change)="fileChanged()"
> >
</div> </div>
</div> </div>
@ -50,6 +55,10 @@
</div> </div>
</div> </div>
<div *ngIf="fileError" class="alert alert-danger">
{{ fileError }}
</div>
<div class="form-group"> <div class="form-group">
<label for="description">Description</label> <label for="description">Description</label>
<textarea <textarea
@ -69,7 +78,6 @@
<div class="form-group"> <div class="form-group">
<input <input
type="button" value="Upload" class="btn btn-default form-control" type="button" value="Upload" class="btn btn-default form-control"
[title]="getInvalidFieldsTitle()" [disabled]="!form.valid || tags.length === 0 || filename === null"
(click)="upload()" (click)="upload()"
> >
</div> </div>

View File

@ -51,6 +51,7 @@ div.file-to-upload {
} }
} }
div.progress { .little-information {
// height: 40px; font-size: 0.8em;
font-style: italic;
} }

View File

@ -30,6 +30,10 @@ export class VideoAddComponent extends FormReactive implements OnInit {
currentTag: VIDEO_TAGS.MESSAGES currentTag: VIDEO_TAGS.MESSAGES
}; };
// Special error messages
tagsError = '';
fileError = '';
constructor( constructor(
private authService: AuthService, private authService: AuthService,
private elementRef: ElementRef, private elementRef: ElementRef,
@ -57,30 +61,6 @@ export class VideoAddComponent extends FormReactive implements OnInit {
this.form.valueChanges.subscribe(data => this.onValueChanged(data)); this.form.valueChanges.subscribe(data => this.onValueChanged(data));
} }
getInvalidFieldsTitle() {
let title = '';
const nameControl = this.form.controls['name'];
const descriptionControl = this.form.controls['description'];
if (!nameControl.valid) {
title += 'A name is required\n';
}
if (this.tags.length === 0) {
title += 'At least one tag is required\n';
}
if (this.filename === null) {
title += 'A file is required\n';
}
if (!descriptionControl.valid) {
title += 'A description is required\n';
}
return title;
}
ngOnInit() { ngOnInit() {
this.uploader = new FileUploader({ this.uploader = new FileUploader({
authToken: this.authService.getRequestHeaderValue(), authToken: this.authService.getRequestHeaderValue(),
@ -104,6 +84,24 @@ export class VideoAddComponent extends FormReactive implements OnInit {
this.buildForm(); this.buildForm();
} }
checkForm() {
this.forceCheck();
if (this.tags.length === 0) {
this.tagsError = 'You have 0 tags';
}
if (this.filename === null) {
this.fileError = 'You did not add a file.';
}
return this.form.valid === true && this.tagsError === '' && this.fileError === '';
}
fileChanged() {
this.fileError = '';
}
onTagKeyPress(event: KeyboardEvent) { onTagKeyPress(event: KeyboardEvent) {
const currentTag = this.form.value['currentTag']; const currentTag = this.form.value['currentTag'];
@ -121,6 +119,8 @@ export class VideoAddComponent extends FormReactive implements OnInit {
if (this.tags.length >= 3) { if (this.tags.length >= 3) {
this.form.get('currentTag').disable(); this.form.get('currentTag').disable();
} }
this.tagsError = '';
} }
} }
} }
@ -135,6 +135,10 @@ export class VideoAddComponent extends FormReactive implements OnInit {
} }
upload() { upload() {
if (this.checkForm() === false) {
return;
}
const item = this.uploader.queue[0]; const item = this.uploader.queue[0];
// TODO: wait for https://github.com/valor-software/ng2-file-upload/pull/242 // TODO: wait for https://github.com/valor-software/ng2-file-upload/pull/242
item.alias = 'videofile'; item.alias = 'videofile';