[frontend]: Refactor into separate components
This commit is contained in:
@ -1,47 +1,14 @@
|
||||
import React, { useState } from "react";
|
||||
import { MapContainer, TileLayer, Marker, Popup } from "react-leaflet";
|
||||
import "leaflet/dist/leaflet.css";
|
||||
import MarkerClusterGroup from "react-leaflet-markercluster";
|
||||
import L, { Icon } from "leaflet";
|
||||
|
||||
import "leaflet.markercluster/dist/MarkerCluster.css";
|
||||
import "leaflet.markercluster/dist/MarkerCluster.Default.css";
|
||||
|
||||
// Icons
|
||||
import trainStationIconURL from "../src/assets/icons/train-station.png";
|
||||
import trainIconURL from "../src/assets/icons/train.png";
|
||||
|
||||
// Fix marker icon issue with Leaflet
|
||||
delete L.Icon.Default.prototype._getIconUrl;
|
||||
L.Icon.Default.mergeOptions({
|
||||
iconUrl: "https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-icon.png",
|
||||
iconRetinaUrl: "https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-icon-2x.png",
|
||||
shadowUrl: "https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-shadow.png",
|
||||
});
|
||||
|
||||
const icons = new Map();
|
||||
icons.set(
|
||||
"IrishRailStation",
|
||||
new Icon({
|
||||
iconUrl: trainStationIconURL,
|
||||
iconSize: [24, 24],
|
||||
})
|
||||
);
|
||||
|
||||
icons.set(
|
||||
"IrishRailTrain",
|
||||
new Icon({
|
||||
iconUrl: trainIconURL,
|
||||
iconSize: [24, 24],
|
||||
})
|
||||
);
|
||||
import Sidebar from "./components/Sidebar";
|
||||
import MapComponent from "./components/MapComponent";
|
||||
import LoadingOverlay from "./components/LoadingOverlay";
|
||||
|
||||
const TRANSIENT_DATA_API = "https://281bc6mcm5.execute-api.us-east-1.amazonaws.com/transient_data";
|
||||
const PERMANENT_DATA_API = "https://a6y312dpuj.execute-api.us-east-1.amazonaws.com/permanent_data";
|
||||
|
||||
const dataSources = [
|
||||
{ id: "IrishRailTrains", name: "Irish Rail Trains", url: TRANSIENT_DATA_API + "?objectType=IrishRailTrain" },
|
||||
{ id: "IrishRailStations", name: "Irish Rail Stations", url: PERMANENT_DATA_API + "?objectType=IrishRailStation" },
|
||||
{ id: "IrishRailTrains", name: "Irish Rail Trains", url: `${TRANSIENT_DATA_API}?objectType=IrishRailTrain` },
|
||||
{ id: "IrishRailStations", name: "Irish Rail Stations", url: `${PERMANENT_DATA_API}?objectType=IrishRailStation` },
|
||||
];
|
||||
|
||||
function App() {
|
||||
@ -50,95 +17,37 @@ function App() {
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [clusteringEnabled, setClusteringEnabled] = useState(true);
|
||||
|
||||
const handleCheckboxChange = (id) => {
|
||||
setSelectedSources((prev) =>
|
||||
prev.includes(id) ? prev.filter((source) => source !== id) : [...prev, id]
|
||||
);
|
||||
};
|
||||
|
||||
const fetchData = async () => {
|
||||
setLoading(true);
|
||||
const newMarkers = [];
|
||||
for (const source of dataSources) {
|
||||
if (selectedSources.includes(source.id)) {
|
||||
try {
|
||||
const response = await fetch(source.url);
|
||||
const data = await response.json();
|
||||
data.forEach((item) => {
|
||||
newMarkers.push({
|
||||
coords: [item.latitude, item.longitude],
|
||||
popup: item.objectType,
|
||||
icon: item.objectType,
|
||||
});
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(`Error fetching data from ${source.name}:`, error);
|
||||
}
|
||||
}
|
||||
try {
|
||||
const newMarkers = (await Promise.all(
|
||||
dataSources
|
||||
.filter(({ id }) => selectedSources.includes(id))
|
||||
.map(({ url }) => fetch(url).then((res) => res.json()))
|
||||
)).flat().map(({ latitude, longitude, objectType }) => ({
|
||||
coords: [latitude, longitude],
|
||||
popup: objectType,
|
||||
icon: objectType,
|
||||
}));
|
||||
setMarkers(newMarkers);
|
||||
} catch (error) {
|
||||
console.error("Error fetching data:", error);
|
||||
}
|
||||
setMarkers(newMarkers);
|
||||
setLoading(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<div style={{ height: "100vh", width: "100vw", display: "flex", position: "relative" }}>
|
||||
{loading && (
|
||||
<div style={{
|
||||
position: "absolute", top: 0, left: 0, width: "100%", height: "100%",
|
||||
background: "rgba(0, 0, 0, 0.5)", display: "flex",
|
||||
alignItems: "center", justifyContent: "center",
|
||||
color: "white", fontSize: "20px", fontWeight: "bold",
|
||||
zIndex: 1000
|
||||
}}>
|
||||
Loading data...
|
||||
</div>
|
||||
)}
|
||||
<div style={{ width: "250px", padding: "10px", background: "#000000", color: "white" }}>
|
||||
<h3>Select Data Sources</h3>
|
||||
{dataSources.map((source) => (
|
||||
<div key={source.id}>
|
||||
<input
|
||||
type="checkbox"
|
||||
id={source.id}
|
||||
checked={selectedSources.includes(source.id)}
|
||||
onChange={() => handleCheckboxChange(source.id)}
|
||||
/>
|
||||
<label htmlFor={source.id}>{source.name}</label>
|
||||
</div>
|
||||
))}
|
||||
<div style={{ marginTop: "10px" }}>
|
||||
<input
|
||||
type="checkbox"
|
||||
id="toggleClustering"
|
||||
checked={clusteringEnabled}
|
||||
onChange={() => setClusteringEnabled(!clusteringEnabled)}
|
||||
/>
|
||||
<label htmlFor="toggleClustering">Cluster overlapping icons</label>
|
||||
</div>
|
||||
<button onClick={fetchData} style={{ marginTop: "10px" }}>Submit</button>
|
||||
</div>
|
||||
{loading && <LoadingOverlay />}
|
||||
<Sidebar
|
||||
selectedSources={selectedSources}
|
||||
setSelectedSources={setSelectedSources}
|
||||
clusteringEnabled={clusteringEnabled}
|
||||
setClusteringEnabled={setClusteringEnabled}
|
||||
fetchData={fetchData}
|
||||
/>
|
||||
<div style={{ flex: 1 }}>
|
||||
<MapContainer center={[53.4494762, -7.5029786]} zoom={7} minZoom={4} style={{ height: "100%", width: "100%" }}>
|
||||
<TileLayer
|
||||
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
|
||||
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
|
||||
/>
|
||||
{clusteringEnabled ? (
|
||||
<MarkerClusterGroup>
|
||||
{markers.map((marker, index) => (
|
||||
<Marker key={index} position={marker.coords} icon={icons.get(marker.icon)}>
|
||||
<Popup>{marker.popup}</Popup>
|
||||
</Marker>
|
||||
))}
|
||||
</MarkerClusterGroup>
|
||||
) : (
|
||||
markers.map((marker, index) => (
|
||||
<Marker key={index} position={marker.coords} icon={icons.get(marker.icon)}>
|
||||
<Popup>{marker.popup}</Popup>
|
||||
</Marker>
|
||||
))
|
||||
)}
|
||||
</MapContainer>
|
||||
<MapComponent markers={markers} clusteringEnabled={clusteringEnabled} />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
15
frontend/src/components/LoadingOverlay.jsx
Normal file
15
frontend/src/components/LoadingOverlay.jsx
Normal file
@ -0,0 +1,15 @@
|
||||
import React from "react";
|
||||
|
||||
const LoadingOverlay = () => (
|
||||
<div style={{
|
||||
position: "absolute", top: 0, left: 0, width: "100%", height: "100%",
|
||||
background: "rgba(0, 0, 0, 0.5)", display: "flex",
|
||||
alignItems: "center", justifyContent: "center",
|
||||
color: "white", fontSize: "20px", fontWeight: "bold",
|
||||
zIndex: 1000
|
||||
}}>
|
||||
Loading data...
|
||||
</div>
|
||||
);
|
||||
|
||||
export default LoadingOverlay;
|
51
frontend/src/components/MapComponent.jsx
Normal file
51
frontend/src/components/MapComponent.jsx
Normal file
@ -0,0 +1,51 @@
|
||||
import React from "react";
|
||||
import { MapContainer, TileLayer, Marker, Popup } from "react-leaflet";
|
||||
import MarkerClusterGroup from "react-leaflet-markercluster";
|
||||
import L, { Icon } from "leaflet";
|
||||
import "leaflet/dist/leaflet.css";
|
||||
import "leaflet.markercluster/dist/MarkerCluster.css";
|
||||
import "leaflet.markercluster/dist/MarkerCluster.Default.css";
|
||||
|
||||
import trainStationIconURL from "../assets/icons/train-station.png";
|
||||
import trainIconURL from "../assets/icons/train.png";
|
||||
|
||||
// Fix Leaflet marker icon issue
|
||||
delete L.Icon.Default.prototype._getIconUrl;
|
||||
L.Icon.Default.mergeOptions({
|
||||
iconUrl: "https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-icon.png",
|
||||
iconRetinaUrl: "https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-icon-2x.png",
|
||||
shadowUrl: "https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-shadow.png",
|
||||
});
|
||||
|
||||
const icons = new Map([
|
||||
["IrishRailStation", new Icon({ iconUrl: trainStationIconURL, iconSize: [24, 24] })],
|
||||
["IrishRailTrain", new Icon({ iconUrl: trainIconURL, iconSize: [24, 24] })],
|
||||
]);
|
||||
|
||||
const MapComponent = ({ markers, clusteringEnabled }) => {
|
||||
return (
|
||||
<MapContainer center={[53.4494762, -7.5029786]} zoom={7} minZoom={4} style={{ height: "100%", width: "100%" }}>
|
||||
<TileLayer
|
||||
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
|
||||
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
|
||||
/>
|
||||
{clusteringEnabled ? (
|
||||
<MarkerClusterGroup>
|
||||
{markers.map(({ coords, popup, icon }, index) => (
|
||||
<Marker key={index} position={coords} icon={icons.get(icon)}>
|
||||
<Popup>{popup}</Popup>
|
||||
</Marker>
|
||||
))}
|
||||
</MarkerClusterGroup>
|
||||
) : (
|
||||
markers.map(({ coords, popup, icon }, index) => (
|
||||
<Marker key={index} position={coords} icon={icons.get(icon)}>
|
||||
<Popup>{popup}</Popup>
|
||||
</Marker>
|
||||
))
|
||||
)}
|
||||
</MapContainer>
|
||||
);
|
||||
};
|
||||
|
||||
export default MapComponent;
|
59
frontend/src/components/Sidebar.jsx
Normal file
59
frontend/src/components/Sidebar.jsx
Normal file
@ -0,0 +1,59 @@
|
||||
import React, { useState } from "react";
|
||||
|
||||
const Sidebar = ({ selectedSources, setSelectedSources, clusteringEnabled, setClusteringEnabled, fetchData }) => {
|
||||
const [isOpen, setIsOpen] = useState(true);
|
||||
const dataSources = [
|
||||
{ id: "IrishRailTrains", name: "Irish Rail Trains" },
|
||||
{ id: "IrishRailStations", name: "Irish Rail Stations" },
|
||||
];
|
||||
|
||||
return (
|
||||
<div style={{
|
||||
position: "absolute", top: "10px", right: "10px",
|
||||
width: "250px", minWidth: "50px",
|
||||
padding: isOpen ? "10px" : "5px 10px", background: "rgba(255, 255, 255, 0.9)", color: "black",
|
||||
borderRadius: "10px", transition: "height 0.2s ease-in-out, padding 0.2s ease-in-out",
|
||||
height: isOpen ? "auto" : "40px", display: "flex", flexDirection: "column",
|
||||
alignItems: "center", zIndex: 1000, overflow: "hidden", justifyContent: "center"
|
||||
}}>
|
||||
<button onClick={() => setIsOpen(!isOpen)} style={{
|
||||
background: "none", border: "none", color: "black",
|
||||
fontSize: "16px", cursor: "pointer", display: "flex",
|
||||
alignItems: "center", width: "100%", justifyContent: "center",
|
||||
padding: "8px 10px", fontWeight: "bold"
|
||||
}}>
|
||||
{isOpen ? "▼ Filters" : "▶ Filters"}
|
||||
</button>
|
||||
{isOpen && (
|
||||
<div style={{ display: "flex", flexDirection: "column", alignItems: "flex-start", width: "100%" }}>
|
||||
<h3>Select Data Sources</h3>
|
||||
{dataSources.map(({ id, name }) => (
|
||||
<div key={id} style={{ display: "flex", alignItems: "center", gap: "8px" }}>
|
||||
<input
|
||||
type="checkbox"
|
||||
id={id}
|
||||
checked={selectedSources.includes(id)}
|
||||
onChange={() =>
|
||||
setSelectedSources((prev) => prev.includes(id) ? prev.filter((s) => s !== id) : [...prev, id])
|
||||
}
|
||||
/>
|
||||
<label htmlFor={id}>{name}</label>
|
||||
</div>
|
||||
))}
|
||||
<div style={{ marginTop: "10px", display: "flex", alignItems: "center", gap: "8px" }}>
|
||||
<input
|
||||
type="checkbox"
|
||||
id="toggleClustering"
|
||||
checked={clusteringEnabled}
|
||||
onChange={() => setClusteringEnabled(!clusteringEnabled)}
|
||||
/>
|
||||
<label htmlFor="toggleClustering">Cluster overlapping icons</label>
|
||||
</div>
|
||||
<button onClick={fetchData} style={{ marginTop: "10px" }}>Submit</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default Sidebar;
|
Reference in New Issue
Block a user