diff --git a/frontend/src/components/LuasPopup.jsx b/frontend/src/components/LuasPopup.jsx
index cbba62e..2f48f97 100644
--- a/frontend/src/components/LuasPopup.jsx
+++ b/frontend/src/components/LuasPopup.jsx
@@ -1,16 +1,21 @@
import React, { useState } from "react";
+import { useMap } from "react-leaflet";
const LuasPopup = ({ item, objectTitle, luasLine }) => {
const [luasInfo, setLuasInfo] = useState("");
+ const map = useMap(); // Access the Leaflet map instance
const fetchLuasData = async () => {
+ let luasInfoHtml = "";
+
try {
- const response = await fetch(`https://3fzg2hdskc.execute-api.us-east-1.amazonaws.com/return_luas_data?luasStopCode=${item.luasStopCode}`);
+ const response = await fetch(
+ `https://3fzg2hdskc.execute-api.us-east-1.amazonaws.com/return_luas_data?luasStopCode=${item.luasStopCode}`
+ );
const data = await response.json();
if (!data.stopInfo || !data.stopInfo.direction) {
- setLuasInfo("No tram data available");
- return;
+ throw new Error("No tram data available");
}
const tramInfo = data.stopInfo.direction.map(direction => {
@@ -21,26 +26,29 @@ const LuasPopup = ({ item, objectTitle, luasLine }) => {
trams.forEach(tram => {
if (tram["@dueMins"] === "DUE") {
tramDetails += `
Destination: ${tram["@destination"]}; Arrival: DUE NOW.`;
- }
- else if (tram["@dueMins"] === "1") {
+ } else if (tram["@dueMins"] === "1") {
tramDetails += `
Destination: ${tram["@destination"]}; Arrival: 1 minute.`;
- }
- else if (tram["@destination"] == "No trams forecast") {
+ } else if (tram["@destination"] === "No trams forecast") {
tramDetails += "
No trams forecast";
- }
- else {
+ } else {
tramDetails += `
Destination: ${tram["@destination"]}; Arrival: ${tram["@dueMins"]} minutes.`;
}
});
-
return `${direction["@name"]}: ${tramDetails}`;
}).join("
");
- setLuasInfo(tramInfo);
+ luasInfoHtml = tramInfo;
} catch (error) {
- setLuasInfo("Failed to fetch Luas data");
+ luasInfoHtml = "Failed to fetch Luas data or no trams available.";
}
+
+ setLuasInfo(luasInfoHtml);
+
+ // Ensure the map pans to keep the popup in view even if an error occurs
+ setTimeout(() => {
+ map.panTo([item.latitude, item.longitude], { animate: true });
+ }, 300);
};
return (
@@ -50,16 +58,34 @@ const LuasPopup = ({ item, objectTitle, luasLine }) => {