Commit 3454f63d authored by Janik Münzenberger's avatar Janik Münzenberger
Browse files

Changed error handling

parent 22403765
......@@ -32,10 +32,11 @@
</div>
</form>
<div class="col-md-12">
<div class="col-md-8 alert alert-danger mx-auto" style="display: none;" #error>
{{"ANALYSIS.NO_RESULT" | translate}}
<div class="col-md-12 mt-4">
<div class="col-md-8 alert alert-danger mx-auto" *ngIf="errorMessage">
{{errorMessage | translate}}
</div>
<div class="col-md-8 alert alert-success mx-auto" [hidden]="!push">
<i class="fas fa-check mr-3"></i>{{"ANALYSIS.PUSH" | translate}}</div>
<i class="fas fa-check mr-3"></i>{{"ANALYSIS.PUSH" | translate}}
</div>
</div>
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { PlantService } from '../../services/plant.service';
import { IPlant } from '../../model/IPlant';
import { FormGroup, FormControl, Validators, NgForm } from '@angular/forms';
......@@ -11,25 +11,24 @@ import { NotificationService } from '../../services/notification.service';
@Component({
selector: 'app-analyse',
templateUrl: './analyse.component.html',
styleUrls: ['./analyse.component.css'],
providers: [PlantService]
styleUrls: ['./analyse.component.css']
})
export class AnalyseComponent implements OnInit {
url = '';
showImage = false;
push: boolean = false;
load: boolean = false;
errorMessage: string;
notification_email: string = "";
plants: IPlant[] = [];
selected: number;
@ViewChild(NgForm) analysisForm:NgForm;
@ViewChild(NgForm) analysisForm: NgForm;
@ViewChild('image') image;
@ViewChild('error') error:ElementRef;
constructor(private pService: PlantService,
constructor(private pService: PlantService,
private aService: AnalysisService,
public userService: UserService,
private notificationService: NotificationService,
......@@ -39,44 +38,53 @@ export class AnalyseComponent implements OnInit {
}
ngOnInit() {
if(this.userService.getUser()) this.notification_email = this.userService.getUser().email;
if (this.userService.getUser()) this.notification_email = this.userService.getUser().email;
this.getPlants();
}
getPlants(){
getPlants() {
this.pService.getAllPlants().then(plants => {
this.plants = plants;
this.route.queryParams.subscribe(params => {
let preselect = params["plant"];
if(preselect) this.selected = +preselect;
if (preselect) this.selected = +preselect;
else this.selected = plants[0]._id;
})
});
}
onSubmit(){
onSubmit() {
let element = this.image.nativeElement;
if(this.selected && element.files && element.files[0]){
if (this.selected && element.files && element.files[0]) {
const formData = new FormData();
formData.append('image_file', element.files[0]);
formData.append('crop_id', this.selected.toString());
formData.append('notification_email', this.notification_email);
if(this.notificationService.subscription)
if (this.notificationService.subscription)
formData.append('subscription', JSON.stringify(this.notificationService.subscription));
this.load = true;
this.load = true;
this.errorMessage = null;
this.aService.startAnalysis(formData)
.then(res => {
if(res.method === 'poll'){
if (res.method === 'poll') {
this.router.navigate(["result", res.data]);
}else{
} else {
this.push = true;
}
this.load = false;
})
.catch(err => {
this.error.nativeElement.style.display = 'block';
this.load = false;
if (err.status === 504 || !err.status) {
this.errorMessage = "ERRORS.NO_CONNECTION";
} else if (err.error) {
this.errorMessage = err.error.error.message;
} else {
this.errorMessage = "ANALYSIS.NO_RESULT";
}
console.error(err);
this.load = false;
});
}
}
......@@ -90,7 +98,7 @@ export class AnalyseComponent implements OnInit {
reader.readAsDataURL(event.target.files[0]); // read file as data url
reader.onload = (filevent: any) => { // called once readAsDataURL is completed
this.url = filevent.target.result;
this.url = filevent.target.result;
};
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment