default error message is displayed

This commit is contained in:
Gansner Pascal committed 2020-05-08 10:27:45 +02:00
1 parent 7453b8e50a
commit a3c7e4a1cb
3 files changed
+81 -85

No files matched your search

+17 -1
View File
@@ -1,10 +1,26 @@
import { HttpHeaders, HttpClient } from '@angular/common/http';
import { HttpHeaders, HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { ToastController } from '@ionic/angular';
export class SoapApiHelper {
public static call<T>(http: HttpClient, url: string, data: object): Observable<T> {
return http.post<T>(url, data, { responseType: 'json', headers: new HttpHeaders({ 'Content-Type': 'application/json' }) })
.pipe(map((response: any) => response.d));
}
/**
* TODO: handle specific error
*
* @param controller toast controller
* @param error error object
*/
public static handleError(controller: ToastController, error: any): void {
let message = 'Unexpected error. Please try again later.';
controller.create({
message: message,
duration: 5000
}).then(toast => toast.present());
}
}
@@ -1,8 +1,9 @@
import { Component, Input, OnInit } from '@angular/core';
import {ModalController, PopoverController, ToastController} from '@ionic/angular';
import { ModalController, PopoverController, ToastController } from '@ionic/angular';
import { Restaurant, TimeframeElement, Timeframe, TimeframeSelection, TimeframeQueue, LIMITS_CATERING, CateringStatus } from 'src/app/interfaces/catering-schedule';
import { CoronaCateringService } from 'src/app/services/corona-catering.service';
import { CoronaCateringPopoverComponent } from 'src/app/components/corona-catering-popover/corona-catering-popover.component';
import { SoapApiHelper } from 'src/app/classes/api-helper';
@Component({
selector: 'app-coronarestaurant',
@@ -48,14 +49,14 @@ export class CoronarestaurantPage implements OnInit {
this.loadQueues();
this.loadRecommendedNb();
this.loadSelection();
}).catch(error => this.handleError(error));
}).catch(error => SoapApiHelper.handleError(this.toastController, error));
} else {
this.cateringService.changeTimeframeAsync(this.restaurant, frame, this.date)
.then(_ => {
this.loadQueues();
this.loadRecommendedNb();
this.loadSelection();
}).catch(error => this.handleError(error));
}).catch(error => SoapApiHelper.handleError(this.toastController, error));
}
}
@@ -113,7 +114,7 @@ export class CoronarestaurantPage implements OnInit {
this.queues.push({ timeframe: frame.value, currentNb: data });
}
},
error => this.handleError(error)
error => SoapApiHelper.handleError(this.toastController, error)
);
}
}
@@ -133,20 +134,9 @@ export class CoronarestaurantPage implements OnInit {
*/
private loadRecommendedNb(): void {
this.cateringService.getMaxNbOase()
.subscribe(data => this.recommendedNbOase = data, error => this.handleError(error));
.subscribe(data => this.recommendedNbOase = data, error => SoapApiHelper.handleError(this.toastController, error));
this.cateringService.getMaxNbTimeout()
.subscribe(data => this.recommendedNbTimeout = data, error => this.handleError(error));
}
/**
* Notifies user when error occurs
*/
async handleError(error) {
const toast = await this.toastController.create({
message: error,
duration: 2000
});
toast.present();
.subscribe(data => this.recommendedNbTimeout = data, error => SoapApiHelper.handleError(this.toastController, error));
}
// cancels the settings modal
@@ -1,6 +1,6 @@
import {Component, OnInit, Input, OnDestroy} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {environment} from '../../../environments/environment';
import { Component, OnInit, Input, OnDestroy } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { environment } from '../../../environments/environment';
import {
BusSchedule,
BusDirection,
@@ -11,10 +11,11 @@ import {
BusStatus,
BUS_LIMITS
} from '../../interfaces/bus-schedule';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {ModalController, Platform, PopoverController, ToastController} from '@ionic/angular';
import {CoronatransportpopoverComponent} from '../../components/coronatransportpopover/coronatransportpopover.component';
import {CoronaBusService} from '../../services/corona-bus.service';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ModalController, Platform, PopoverController, ToastController } from '@ionic/angular';
import { CoronatransportpopoverComponent } from '../../components/coronatransportpopover/coronatransportpopover.component';
import { CoronaBusService } from '../../services/corona-bus.service';
import { SoapApiHelper } from 'src/app/classes/api-helper';
@Component({
@@ -37,11 +38,11 @@ export class CoronatransportPage implements OnInit {
public limits = BUS_LIMITS;
constructor(private http: HttpClient,
private _FB: FormBuilder,
private modalController: ModalController,
private popoverController: PopoverController,
private toastController: ToastController,
private busService: CoronaBusService) {
private _FB: FormBuilder,
private modalController: ModalController,
private popoverController: PopoverController,
private toastController: ToastController,
private busService: CoronaBusService) {
this.arrivalForm = _FB.group({
'time': ['', Validators.required],
'from': ['', Validators.required],
@@ -56,10 +57,10 @@ export class CoronatransportPage implements OnInit {
ngOnInit() {
this.http.get<BusSchedule>(this.assets + '/busfahrplan.json')
.subscribe(data => {
this.busSchedule = data;
this.initForm();
});
.subscribe(data => {
this.busSchedule = data;
this.initForm();
});
this.loadBusesStatus();
this.updateSelection();
}
@@ -73,24 +74,24 @@ export class CoronatransportPage implements OnInit {
// Check if start station is Siggenthal
if (this.arrivalForm.controls['from'].value == 'Siggenthal') {
this.filteredAData = this.busSchedule.arrival.filter(a => a.middle_time != ''
&& new Date(this.date.toDateString() + ' ' + a.start_time) >= new Date(this.arrivalForm.controls['time'].value)
&& a.date == '10.05.2020');
&& new Date(this.date.toDateString() + ' ' + a.start_time) >= new Date(this.arrivalForm.controls['time'].value)
&& a.date == '10.05.2020');
} else {
this.filteredAData = this.busSchedule.arrival.filter(a => a.start_station == this.arrivalForm.controls['from'].value
&& new Date(this.date.toDateString() + ' ' + a.start_time) >= new Date(this.arrivalForm.controls['time'].value)
&& a.date == '10.05.2020');
&& new Date(this.date.toDateString() + ' ' + a.start_time) >= new Date(this.arrivalForm.controls['time'].value)
&& a.date == '10.05.2020');
}
// Check if end station is Siggenthal
if (this.departureForm.controls['to'].value == 'Siggenthal') {
this.filteredDData = this.busSchedule.departure.filter(d => d.middle_time != ''
&& (new Date(this.date.toDateString() + ' ' + d.start_time_east) >= new Date(this.departureForm.controls['time'].value)
|| new Date(this.date.toDateString() + ' ' + d.start_time_west) >= new Date(this.departureForm.controls['time'].value))
&& d.date == '10.05.2020');
&& (new Date(this.date.toDateString() + ' ' + d.start_time_east) >= new Date(this.departureForm.controls['time'].value)
|| new Date(this.date.toDateString() + ' ' + d.start_time_west) >= new Date(this.departureForm.controls['time'].value))
&& d.date == '10.05.2020');
} else {
this.filteredDData = this.busSchedule.departure.filter(d => d.end_station == this.departureForm.controls['to'].value
&& (new Date(this.date.toDateString() + ' ' + d.start_time_east) >= new Date(this.departureForm.controls['time'].value)
|| new Date(this.date.toDateString() + ' ' + d.start_time_west) >= new Date(this.departureForm.controls['time'].value))
&& d.date == '10.05.2020');
&& (new Date(this.date.toDateString() + ' ' + d.start_time_east) >= new Date(this.departureForm.controls['time'].value)
|| new Date(this.date.toDateString() + ' ' + d.start_time_west) >= new Date(this.departureForm.controls['time'].value))
&& d.date == '10.05.2020');
}
// Check if today's date is lower or above 11th May 2020
@@ -98,24 +99,24 @@ export class CoronatransportPage implements OnInit {
// Check if start station is Siggenthal
if (this.arrivalForm.controls['from'].value == 'Siggenthal') {
this.filteredAData = this.busSchedule.arrival.filter(a => a.middle_time != ''
&& new Date(this.date.toDateString() + ' ' + a.start_time) >= new Date(this.arrivalForm.controls['time'].value)
&& a.date == '11.05.2020');
&& new Date(this.date.toDateString() + ' ' + a.start_time) >= new Date(this.arrivalForm.controls['time'].value)
&& a.date == '11.05.2020');
} else {
this.filteredAData = this.busSchedule.arrival.filter(a => a.start_station == this.arrivalForm.controls['from'].value
&& new Date(this.date.toDateString() + ' ' + a.start_time) >= new Date(this.arrivalForm.controls['time'].value)
&& a.date == '11.05.2020');
&& new Date(this.date.toDateString() + ' ' + a.start_time) >= new Date(this.arrivalForm.controls['time'].value)
&& a.date == '11.05.2020');
}
// Check if end station is Siggenthal
if (this.departureForm.controls['to'].value == 'Siggenthal') {
this.filteredDData = this.busSchedule.departure.filter(d => d.middle_time != ''
&& (new Date(this.date.toDateString() + ' ' + d.start_time_east) >= new Date(this.departureForm.controls['time'].value)
|| new Date(this.date.toDateString() + ' ' + d.start_time_west) >= new Date(this.departureForm.controls['time'].value))
&& d.date == '11.05.2020');
&& (new Date(this.date.toDateString() + ' ' + d.start_time_east) >= new Date(this.departureForm.controls['time'].value)
|| new Date(this.date.toDateString() + ' ' + d.start_time_west) >= new Date(this.departureForm.controls['time'].value))
&& d.date == '11.05.2020');
} else {
this.filteredDData = this.busSchedule.departure.filter(d => d.end_station == this.departureForm.controls['to'].value
&& (new Date(this.date.toDateString() + ' ' + d.start_time_east) >= new Date(this.departureForm.controls['time'].value)
|| new Date(this.date.toDateString() + ' ' + d.start_time_west) >= new Date(this.departureForm.controls['time'].value))
&& d.date == '11.05.2020');
&& (new Date(this.date.toDateString() + ' ' + d.start_time_east) >= new Date(this.departureForm.controls['time'].value)
|| new Date(this.date.toDateString() + ' ' + d.start_time_west) >= new Date(this.departureForm.controls['time'].value))
&& d.date == '11.05.2020');
}
}
}
@@ -125,7 +126,7 @@ export class CoronatransportPage implements OnInit {
*/
public getArrivalSelection(): BusSelection {
return this.selections.find(selection => selection.date.getTime() === this.date.getTime()
&& selection.direction === BusDirection.ToPSI);
&& selection.direction === BusDirection.ToPSI);
}
/**
@@ -133,7 +134,7 @@ export class CoronatransportPage implements OnInit {
*/
public getDepartureSelection(): BusSelection {
return this.selections.find(selection => selection.date.getTime() === this.date.getTime()
&& selection.direction === BusDirection.FromPSI);
&& selection.direction === BusDirection.FromPSI);
}
/**
@@ -142,7 +143,7 @@ export class CoronatransportPage implements OnInit {
* @param id unique id of the bus
*/
public getBusStatus(id: number): BusInformation {
return this.busesStatus.find(status => status.id == id) || {id, currentNb: 0, status: BusStatus.Ok};
return this.busesStatus.find(status => status.id == id) || { id, currentNb: 0, status: BusStatus.Ok };
}
/**
@@ -153,16 +154,16 @@ export class CoronatransportPage implements OnInit {
public selectArrivalBusSchedule(id: number) {
if (!this.busService.isBusSelected(this.date, BusDirection.ToPSI)) {
this.busService.selectBusAsync(this.date, id, BusDirection.ToPSI)
.then(_ => {
this.updateSelection();
this.loadBusesStatus();
}).catch(error => this.handleError(error));
.then(_ => {
this.updateSelection();
this.loadBusesStatus();
}).catch(error => SoapApiHelper.handleError(this.toastController, error));
} else {
this.busService.changeSelectionAsync(this.date, id, BusDirection.ToPSI)
.then(_ => {
this.updateSelection();
this.loadBusesStatus();
}).catch(error => this.handleError(error));
.then(_ => {
this.updateSelection();
this.loadBusesStatus();
}).catch(error => SoapApiHelper.handleError(this.toastController, error));
}
}
@@ -174,16 +175,16 @@ export class CoronatransportPage implements OnInit {
public selectDepartureBusSchedule(id: number) {
if (!this.busService.isBusSelected(this.date, BusDirection.FromPSI)) {
this.busService.selectBusAsync(this.date, id, BusDirection.FromPSI)
.then(_ => {
this.updateSelection();
this.loadBusesStatus();
}).catch(error => this.handleError(error));
.then(_ => {
this.updateSelection();
this.loadBusesStatus();
}).catch(error => SoapApiHelper.handleError(this.toastController, error));
} else {
this.busService.changeSelectionAsync(this.date, id, BusDirection.FromPSI)
.then(_ => {
this.updateSelection();
this.loadBusesStatus();
}).catch(error => this.handleError(error));
.then(_ => {
this.updateSelection();
this.loadBusesStatus();
}).catch(error => SoapApiHelper.handleError(this.toastController, error));
}
}
@@ -232,17 +233,6 @@ export class CoronatransportPage implements OnInit {
this.filterBusschedule();
}
/**
* Notifies user when error occurs
*/
async handleError(error) {
const toast = await this.toastController.create({
message: error,
duration: 2000
});
toast.present();
}
/**
* Updates the current selection.
*/
@@ -255,7 +245,7 @@ export class CoronatransportPage implements OnInit {
*/
private loadBusesStatus(): void {
this.busService.getBusesStatus(this.date)
.subscribe(data => this.busesStatus = data, error => this.handleError(error));
.subscribe(data => this.busesStatus = data, error => SoapApiHelper.handleError(this.toastController, error));
}
// closes the settings modal