[server]: Fetch Luas stop schedule info from AWS proxy API
This commit is contained in:
@ -5,18 +5,25 @@ const LuasPopup = ({ item, objectTitle, luasLine }) => {
|
|||||||
|
|
||||||
const fetchLuasData = async () => {
|
const fetchLuasData = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`http://luasforecasts.rpa.ie/xml/get.ashx?action=forecast&stop=${item.luasStopCode}&encrypt=false`);
|
const response = await fetch(`https://3fzg2hdskc.execute-api.us-east-1.amazonaws.com/return_luas_data?luasStopCode=${item.luasStopCode}`);
|
||||||
const text = await response.text();
|
const data = await response.json();
|
||||||
const parser = new DOMParser();
|
|
||||||
const xml = parser.parseFromString(text, "text/xml");
|
|
||||||
const trams = Array.from(xml.getElementsByTagName("tram"));
|
|
||||||
|
|
||||||
if (trams.length === 0) {
|
if (!data.stopInfo || !data.stopInfo.direction) {
|
||||||
setLuasInfo("No trams available");
|
setLuasInfo("No tram data available");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const tramInfo = trams.map(tram => `Destination: ${tram.getAttribute("destination")}, Arrival: ${tram.getAttribute("dueMins")} mins`).join("<br>");
|
const tramInfo = data.stopInfo.direction.map(direction => {
|
||||||
|
// Ensure 'tram' is an array, if it's not, convert it into an array
|
||||||
|
const trams = Array.isArray(direction.tram) ? direction.tram : [direction.tram];
|
||||||
|
|
||||||
|
const tramDetails = trams.map(tram =>
|
||||||
|
`Destination: ${tram["@destination"]}, Arrival: ${tram["@dueMins"]} mins`
|
||||||
|
).join("<br>");
|
||||||
|
|
||||||
|
return `<b>${direction["@name"]}:</b><br>${tramDetails}`;
|
||||||
|
}).join("<br><br>");
|
||||||
|
|
||||||
setLuasInfo(tramInfo);
|
setLuasInfo(tramInfo);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setLuasInfo("Failed to fetch Luas data");
|
setLuasInfo("Failed to fetch Luas data");
|
||||||
|
Reference in New Issue
Block a user