From 6a6b1224ca11ba720d946e967f76904a3559b4bd Mon Sep 17 00:00:00 2001 From: Come Tisserand Date: Wed, 16 Oct 2024 11:42:01 +0200 Subject: [PATCH] fix: undefined error on main map --- frontend/components/core/map/main-map.tsx | 4 ++-- frontend/services/backend-rest-client.ts | 13 ++++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/frontend/components/core/map/main-map.tsx b/frontend/components/core/map/main-map.tsx index 8aa45c7d..08d80c75 100644 --- a/frontend/components/core/map/main-map.tsx +++ b/frontend/components/core/map/main-map.tsx @@ -192,7 +192,7 @@ export default function CoreMap({ vesselsPositions }: CoreMapProps) { ) } function toSegmentsGeo({ segments, vesselId }: VesselExcursionSegments): any { - const segmentsGeo = segments.map((segment: VesselExcursionSegment) => { + const segmentsGeo = segments?.map((segment: VesselExcursionSegment) => { return { speed: segment.average_speed, navigational_status: "unknown", @@ -205,6 +205,6 @@ function toSegmentsGeo({ segments, vesselId }: VesselExcursionSegments): any { } } }) - return { vesselId, type: "FeatureCollection", features: segmentsGeo }; + return { vesselId, type: "FeatureCollection", features: segmentsGeo ?? [] }; } diff --git a/frontend/services/backend-rest-client.ts b/frontend/services/backend-rest-client.ts index 69062534..9c015e9d 100644 --- a/frontend/services/backend-rest-client.ts +++ b/frontend/services/backend-rest-client.ts @@ -2,6 +2,7 @@ import { Vessel, VesselExcursion, VesselExcursionSegment, VesselPositions, VesselTrackingTimeDto } from "@/types/vessel"; import { ZoneVisitTimeDto } from "@/types/zone"; import axios, { InternalAxiosRequestConfig } from "axios"; +import { log } from "console"; const BASE_URL = process.env.NEXT_PUBLIC_BACKEND_BASE_URL; const API_KEY = process.env.NEXT_PUBLIC_BACKEND_API_KEY ?? 'no-key-found'; @@ -37,9 +38,15 @@ export function getVesselSegments(vesselId: number, excursionId: number) { } export async function getVesselFirstExcursionSegments(vesselId: number) { - const response = await getVesselExcursion(vesselId); - const excursionId = response?.data[0]?.id; - return !!excursionId ? getVesselSegments(vesselId, excursionId) : []; + try { + const response = await getVesselExcursion(vesselId); + const excursionId = response?.data[0]?.id; + return !!excursionId ? getVesselSegments(vesselId, excursionId) : []; + + } catch(error) { + console.error(error); + return []; + } } export function getTopVesselsInActivity(startAt: string, endAt: string, topVesselsLimit: number) {