[frontend]: Refactor fetchData to use loop rather than stream
Loops more efficient than streams
This commit is contained in:
@ -139,38 +139,60 @@ function App() {
|
|||||||
const fetchData = async (enabledSources, numberInputValue) => {
|
const fetchData = async (enabledSources, numberInputValue) => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
const transientTypes = dataSources.filter(({ id, api }) => enabledSources.includes(id) && api === "transient").map(({ objectType }) => objectType);
|
const transientTypes = [];
|
||||||
const permanentTypes = dataSources.filter(({ id, api }) => enabledSources.includes(id) && api === "permanent").map(({ objectType }) => objectType);
|
const permanentTypes = [];
|
||||||
|
|
||||||
|
for (let i = 0; i < dataSources.length; i++) {
|
||||||
|
const { id, api, objectType } = dataSources[i];
|
||||||
|
if (enabledSources.includes(id)) {
|
||||||
|
if (api === "transient") {
|
||||||
|
transientTypes.push(objectType);
|
||||||
|
} else if (api === "permanent") {
|
||||||
|
permanentTypes.push(objectType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const requests = [];
|
const requests = [];
|
||||||
if (transientTypes.length) requests.push(fetch(`${TRANSIENT_DATA_API}?objectType=${transientTypes.join(",")}`).then(res => res.json()));
|
if (transientTypes.length) {
|
||||||
if (permanentTypes.length) requests.push(fetch(`${PERMANENT_DATA_API}?objectType=${permanentTypes.join(",")}`).then(res => res.json()));
|
requests.push(
|
||||||
|
fetch(`${TRANSIENT_DATA_API}?objectType=${transientTypes.join(",")}`).then(res => res.json())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (permanentTypes.length) {
|
||||||
|
requests.push(
|
||||||
|
fetch(`${PERMANENT_DATA_API}?objectType=${permanentTypes.join(",")}`).then(res => res.json())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const responses = await Promise.all(requests);
|
const responses = await Promise.all(requests);
|
||||||
|
|
||||||
const newMarkers = responses.flat()
|
const newMarkers = [];
|
||||||
.map((item) => {
|
const showMainline = enabledSources.includes("mainline");
|
||||||
const showMainline = enabledSources.includes("mainline");
|
const showSuburban = enabledSources.includes("suburban");
|
||||||
const showSuburban = enabledSources.includes("suburban");
|
const showDart = enabledSources.includes("dart");
|
||||||
const showDart = enabledSources.includes("dart");
|
const showRunning = enabledSources.includes("running");
|
||||||
const showRunning = enabledSources.includes("running");
|
const showNotYetRunning = enabledSources.includes("not-yet-running");
|
||||||
const showNotYetRunning = enabledSources.includes("not-yet-running");
|
const showTerminated = enabledSources.includes("terminated");
|
||||||
const showTerminated = enabledSources.includes("terminated");
|
const showEarly = enabledSources.includes("early");
|
||||||
const showEarly = enabledSources.includes("early");
|
const showOnTime = enabledSources.includes("on-time");
|
||||||
const showOnTime = enabledSources.includes("on-time");
|
const showLate = enabledSources.includes("late");
|
||||||
const showLate = enabledSources.includes("late");
|
const showRedLine = enabledSources.includes("red-line");
|
||||||
|
const showGreenLine = enabledSources.includes("green-line");
|
||||||
|
const showParkAndRide = enabledSources.includes("park-and-ride");
|
||||||
|
const showCycleAndRide = enabledSources.includes("cycle-and-ride");
|
||||||
|
const showEnabled = enabledSources.includes("enabled");
|
||||||
|
const showDisabled = enabledSources.includes("disabled");
|
||||||
|
|
||||||
const showRedLine = enabledSources.includes("red-line");
|
for (let r = 0; r < responses.length; r++) {
|
||||||
const showGreenLine = enabledSources.includes("green-line");
|
const response = responses[r];
|
||||||
const showParkAndRide = enabledSources.includes("park-and-ride");
|
for (let j = 0; j < response.length; j++) {
|
||||||
const showCycleAndRide = enabledSources.includes("cycle-and-ride");
|
const item = response[j];
|
||||||
const showEnabled = enabledSources.includes("enabled");
|
|
||||||
const showDisabled = enabledSources.includes("disabled");
|
|
||||||
|
|
||||||
let icon = item.objectType;
|
let icon = item.objectType;
|
||||||
let popupContent;
|
let popupContent;
|
||||||
let objectTitle;
|
let objectTitle;
|
||||||
let display = false;
|
let display = false;
|
||||||
let markerText = "";
|
let markerText = "";
|
||||||
|
|
||||||
switch (item.objectType) {
|
switch (item.objectType) {
|
||||||
@ -178,7 +200,7 @@ function App() {
|
|||||||
objectTitle = "Irish Rail Train: " + item.trainCode;
|
objectTitle = "Irish Rail Train: " + item.trainCode;
|
||||||
icon = item.trainTypeFull + item.trainPunctualityStatus;
|
icon = item.trainTypeFull + item.trainPunctualityStatus;
|
||||||
|
|
||||||
if (item.trainStatusFull == "Terminated" || item.trainStatusFull == "Not yet running") {
|
if (item.trainStatusFull === "Terminated" || item.trainStatusFull === "Not yet running") {
|
||||||
icon = item.trainTypeFull + "NotRunning";
|
icon = item.trainTypeFull + "NotRunning";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,15 +213,36 @@ function App() {
|
|||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|
||||||
markerText = item.trainPublicMessage + " " + item.trainDirection;
|
markerText = item.trainPublicMessage + " " + item.trainDirection;
|
||||||
|
|
||||||
display =
|
display =
|
||||||
((item.latitude !== "0" && item.longitude !== "0") &&
|
item.latitude !== "0" &&
|
||||||
((showMainline && item.trainTypeFull == "Mainline") || (showSuburban && item.trainTypeFull == "Suburban") || (showDart && item.trainTypeFull == "DART")) &&
|
item.longitude !== "0" &&
|
||||||
((showRunning && item.trainStatusFull == "Running") || (showNotYetRunning && item.trainStatusFull == "Not yet running") || (showTerminated && item.trainStatusFull == "Terminated")) &&
|
(
|
||||||
((item.trainStatusFull == "Running" && showEarly && item.trainPunctualityStatus == "early") || (item.trainStatusFull == "Running" && showOnTime && item.trainPunctualityStatus == "On time") || (item.trainStatusFull == "Running" && showLate && item.trainPunctualityStatus == "late")
|
(showMainline && item.trainTypeFull === "Mainline") ||
|
||||||
|| (item.trainStatusFull == "Not yet running" && showNotYetRunning) || (item.trainStatusFull == "Terminated" && showTerminated))) &&
|
(showSuburban && item.trainTypeFull === "Suburban") ||
|
||||||
(numberInputValue && userLocationAvailable ? haversineDistance(userLocation, [item.latitude, item.longitude]) < numberInputValue : true) &&
|
(showDart && item.trainTypeFull === "DART")
|
||||||
(showFaovouritesOnly ? favourites.IrishRailTrain.includes(item.trainCode) : true);
|
) &&
|
||||||
|
(
|
||||||
|
(showRunning && item.trainStatusFull === "Running") ||
|
||||||
|
(showNotYetRunning && item.trainStatusFull === "Not yet running") ||
|
||||||
|
(showTerminated && item.trainStatusFull === "Terminated")
|
||||||
|
) &&
|
||||||
|
(
|
||||||
|
(item.trainStatusFull === "Running" &&
|
||||||
|
(
|
||||||
|
(showEarly && item.trainPunctualityStatus === "early") ||
|
||||||
|
(showOnTime && item.trainPunctualityStatus === "On time") ||
|
||||||
|
(showLate && item.trainPunctualityStatus === "late")
|
||||||
|
)
|
||||||
|
) ||
|
||||||
|
(item.trainStatusFull === "Not yet running" && showNotYetRunning) ||
|
||||||
|
(item.trainStatusFull === "Terminated" && showTerminated)
|
||||||
|
) &&
|
||||||
|
(numberInputValue && userLocationAvailable
|
||||||
|
? haversineDistance(userLocation, [item.latitude, item.longitude]) < numberInputValue
|
||||||
|
: true) &&
|
||||||
|
(showFaovouritesOnly ? favourites.IrishRailTrain.includes(item.trainCode) : true);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -213,12 +256,13 @@ function App() {
|
|||||||
favourites={favourites}
|
favourites={favourites}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|
||||||
markerText = item.trainStationCode + " " + item.trainStationDesc;
|
markerText = item.trainStationCode + " " + item.trainStationDesc;
|
||||||
display = (item.latitude !== "0" && item.longitude !== "0") &&
|
display = item.latitude !== "0" &&
|
||||||
(numberInputValue && userLocationAvailable ? haversineDistance(userLocation, [item.latitude, item.longitude]) < numberInputValue : true) &&
|
item.longitude !== "0" &&
|
||||||
|
(numberInputValue && userLocationAvailable
|
||||||
|
? haversineDistance(userLocation, [item.latitude, item.longitude]) < numberInputValue
|
||||||
|
: true) &&
|
||||||
(showFaovouritesOnly ? favourites.IrishRailStation.includes(item.trainStationCode) : true);
|
(showFaovouritesOnly ? favourites.IrishRailStation.includes(item.trainStationCode) : true);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "Bus":
|
case "Bus":
|
||||||
@ -231,12 +275,13 @@ function App() {
|
|||||||
favourites={favourites}
|
favourites={favourites}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|
||||||
markerText = item.busRouteAgencyName + " " + item.busRouteShortName + " " + item.busRouteLongName;
|
markerText = item.busRouteAgencyName + " " + item.busRouteShortName + " " + item.busRouteLongName;
|
||||||
display = (item.latitude !== "0" && item.longitude !== "0") &&
|
display = item.latitude !== "0" &&
|
||||||
(numberInputValue && userLocationAvailable ? haversineDistance(userLocation, [item.latitude, item.longitude]) < numberInputValue : true) &&
|
item.longitude !== "0" &&
|
||||||
|
(numberInputValue && userLocationAvailable
|
||||||
|
? haversineDistance(userLocation, [item.latitude, item.longitude]) < numberInputValue
|
||||||
|
: true) &&
|
||||||
(showFaovouritesOnly ? favourites.Bus.includes(item.busRoute) : true);
|
(showFaovouritesOnly ? favourites.Bus.includes(item.busRoute) : true);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "BusStop":
|
case "BusStop":
|
||||||
@ -249,12 +294,13 @@ function App() {
|
|||||||
favourites={favourites}
|
favourites={favourites}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|
||||||
markerText = item.busStopName;
|
markerText = item.busStopName;
|
||||||
display = (item.latitude !== "0" && item.longitude !== "0") &&
|
display = item.latitude !== "0" &&
|
||||||
(numberInputValue && userLocationAvailable ? haversineDistance(userLocation, [item.latitude, item.longitude]) < numberInputValue : true) &&
|
item.longitude !== "0" &&
|
||||||
|
(numberInputValue && userLocationAvailable
|
||||||
|
? haversineDistance(userLocation, [item.latitude, item.longitude]) < numberInputValue
|
||||||
|
: true) &&
|
||||||
(showFaovouritesOnly ? favourites.BusStop.includes(item.busStopID) : true);
|
(showFaovouritesOnly ? favourites.BusStop.includes(item.busStopID) : true);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "LuasStop":
|
case "LuasStop":
|
||||||
@ -273,6 +319,7 @@ function App() {
|
|||||||
default:
|
default:
|
||||||
luasLine = "N/A";
|
luasLine = "N/A";
|
||||||
}
|
}
|
||||||
|
|
||||||
popupContent = (
|
popupContent = (
|
||||||
<LuasPopup
|
<LuasPopup
|
||||||
item={item}
|
item={item}
|
||||||
@ -282,18 +329,23 @@ function App() {
|
|||||||
favourites={favourites}
|
favourites={favourites}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|
||||||
markerText = item.luasStopIrishName + " " + item.luasStopName + " " + luasLine;
|
markerText = item.luasStopIrishName + " " + item.luasStopName + " " + luasLine;
|
||||||
display = (
|
display = item.latitude !== "0" &&
|
||||||
(item.latitude !== "0" && item.longitude !== "0") &&
|
item.longitude !== "0" &&
|
||||||
(showGreenLine && luasLine === "Green Line" || showRedLine && luasLine === "Red Line") &&
|
(
|
||||||
(showEnabled && item.luasStopIsEnabled === "1" || showDisabled && item.luasStopIsEnabled === "0") &&
|
(showGreenLine && luasLine === "Green Line") ||
|
||||||
|
(showRedLine && luasLine === "Red Line")
|
||||||
|
) &&
|
||||||
|
(
|
||||||
|
(showEnabled && item.luasStopIsEnabled === "1") ||
|
||||||
|
(showDisabled && item.luasStopIsEnabled === "0")
|
||||||
|
) &&
|
||||||
(!showCycleAndRide || (showCycleAndRide && item.luasStopIsCycleAndRide === "1")) &&
|
(!showCycleAndRide || (showCycleAndRide && item.luasStopIsCycleAndRide === "1")) &&
|
||||||
(!showParkAndRide || (showParkAndRide && item.luasStopIsParkAndRide === "1")) &&
|
(!showParkAndRide || (showParkAndRide && item.luasStopIsParkAndRide === "1")) &&
|
||||||
(numberInputValue && userLocationAvailable ? haversineDistance(userLocation, [item.latitude, item.longitude]) < numberInputValue : true) &&
|
(numberInputValue && userLocationAvailable
|
||||||
(showFaovouritesOnly ? favourites.LuasStop.includes(item.luasStopID) : true)
|
? haversineDistance(userLocation, [item.latitude, item.longitude]) < numberInputValue
|
||||||
);
|
: true) &&
|
||||||
|
(showFaovouritesOnly ? favourites.LuasStop.includes(item.luasStopID) : true);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -305,15 +357,17 @@ function App() {
|
|||||||
markerText = `Unknown Object Type: ${item.objectType}`;
|
markerText = `Unknown Object Type: ${item.objectType}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
if (display) {
|
||||||
coords: [item.latitude, item.longitude],
|
newMarkers.push({
|
||||||
popup: popupContent,
|
coords: [item.latitude, item.longitude],
|
||||||
icon: icon,
|
popup: popupContent,
|
||||||
markerText: markerText.toLowerCase(),
|
icon: icon,
|
||||||
display: display
|
markerText: markerText.toLowerCase(),
|
||||||
};
|
display: true
|
||||||
})
|
});
|
||||||
.filter((marker) => marker.display);
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
setNumMarkers(newMarkers.length);
|
setNumMarkers(newMarkers.length);
|
||||||
setMarkers(newMarkers);
|
setMarkers(newMarkers);
|
||||||
@ -323,6 +377,7 @@ function App() {
|
|||||||
setLoading(false);
|
setLoading(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const memoizedFilteredMarkers = useMemo(() => {
|
const memoizedFilteredMarkers = useMemo(() => {
|
||||||
return markers.filter(marker =>
|
return markers.filter(marker =>
|
||||||
marker.markerText.includes(searchTerm.toLowerCase())
|
marker.markerText.includes(searchTerm.toLowerCase())
|
||||||
|
Reference in New Issue
Block a user