From b41d64dc9c4f5127a8564d25addc74f80bda907f Mon Sep 17 00:00:00 2001 From: ujwalkarippalichandran Date: Tue, 15 Apr 2025 22:10:29 -0600 Subject: [PATCH] Auto update news every 30 seconds --- src/MapComponent.js | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/src/MapComponent.js b/src/MapComponent.js index 64da216..05c11c0 100644 --- a/src/MapComponent.js +++ b/src/MapComponent.js @@ -29,19 +29,41 @@ class HeatMap extends Component { } componentDidMount() { - fetch("https://fastapi-service-34404463322.us-central1.run.app/geojson") - .then((response) => response.json()) - .then((data) => { - const jitteredFeatures = this.jitterOverlappingPoints(data.features); - const jitteredData = { - type: "FeatureCollection", - features: jitteredFeatures, - }; - this.setState({ geoJsonData: jitteredData }); - }) - .catch((error) => console.error("Error loading GeoJSON from API:", error)); + this.fetchGeoJsonData(); // 🔁 initial load + + this.updateInterval = setInterval(() => { + this.fetchGeoJsonData(); + }, 30000); } + componentWillUnmount() { + clearInterval(this.updateInterval); // 🚫 clean up interval when unmounted + } + + fetchGeoJsonData = () => { + fetch("https://fastapi-service-34404463322.us-central1.run.app/geojson") + .then((response) => response.json()) + .then((data) => { + const jitteredFeatures = this.jitterOverlappingPoints(data.features); + const jitteredData = { + type: "FeatureCollection", + features: jitteredFeatures, + }; + this.setState({ geoJsonData: jitteredData }); + + + if (this.state.selectedNews) { + const stillExists = jitteredData.features.find( + (f) => f.properties.link === this.state.selectedNews.properties.link + ); + if (!stillExists) { + this.setState({ selectedNews: null, showPopup: false }); + } + } + }) + .catch((error) => console.error("Error loading GeoJSON from API:", error)); + }; + jitterOverlappingPoints = (features) => { const jittered = []; const seenCoords = new Set();