From e0b74fd1357dd8ca5f103f57a17faf92b3589e29 Mon Sep 17 00:00:00 2001 From: Desy Ayurianti Date: Fri, 25 Oct 2024 15:44:28 +0700 Subject: [PATCH] fix(dashboard): fix toastr handling error if no dat ain relay --- .../app/pages/dashboard/dashboard.component.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/agrilink_vocpro/src/app/pages/dashboard/dashboard.component.ts b/agrilink_vocpro/src/app/pages/dashboard/dashboard.component.ts index 0564e1c..b5e02a3 100644 --- a/agrilink_vocpro/src/app/pages/dashboard/dashboard.component.ts +++ b/agrilink_vocpro/src/app/pages/dashboard/dashboard.component.ts @@ -149,7 +149,7 @@ export class DashboardComponent implements OnInit { this.apiService.getRelayStatus().subscribe( (response) => { - if (Array.isArray(response.data)) { + if (Array.isArray(response.data) && response.data.length > 0) { this.relayStatus = response.data.map((relay) => ({ id: relay.id, number: relay.number, @@ -159,16 +159,22 @@ export class DashboardComponent implements OnInit { current_status: relay.current_status })); this.relayStatus.sort((a, b) => a.number - b.number); + this.noData = false; // Data available + } else { + // Throw an error if relay data is empty + this.handleError({ message: 'No available relay data.' }); + this.noData = true; } this.isLoaded = false; }, (error) => { - this.toast.error('Failed to get relay status, please try again'); - this.hasError = true; this.isLoaded = false; + this.handleError(error); + this.hasError = true; } ); - } +} + handleError(error: any): void { if (this.selectedButton === 'dht') { @@ -177,6 +183,8 @@ export class DashboardComponent implements OnInit { this.toast.error('Error fetching NPK1 sensor data. Please try again.', 'Error', { timeOut: 3000 }); } else if (this.selectedButton === 'npk2') { this.toast.error('Error fetching NPK2 sensor data. Please try again.', 'Error', { timeOut: 3000 }); + } else if (this.selectedButton === 'relay') { + this.toast.error('Error fetching relay status. Please try again.', 'Error', { timeOut: 3000 }); } this.hasError = true; }