Skip to content

Commit

Permalink
Merge pull request #89 from wmo-raf/dev
Browse files Browse the repository at this point in the history
Catch fetch errors
  • Loading branch information
erick-otenyo authored Apr 2, 2024
2 parents 6c4d9df + 8f09d0d commit b78c391
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 65 deletions.
11 changes: 9 additions & 2 deletions nmhs_cms/templates/navigation/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,22 @@

if (latestActiveAlertUrl) {
fetch(latestActiveAlertUrl)
.then(response => response.text())
.then(response => {
if (!response.ok) {
throw new Error('Error fetching navbar latest active alert');
}
return response.text();
})
.then(data => {
const latestActiveAlert = document.querySelectorAll('.latest-active-alert')
if (data) {
latestActiveAlert.forEach((alertContainer) => {
alertContainer.innerHTML = data;
});
}
})
}).catch(error => {
console.error("NAVBAR_LATEST_ACTIVE_ALERT_ERROR:", error);
});
}

});
Expand Down
14 changes: 8 additions & 6 deletions pages/cap/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,21 @@ def get_latest_active_alert(request):
alerts = CapAlertPage.objects.all().live().filter(status="Actual")
active_alert_infos = []

context = {}

for alert in alerts:
for alert_info in alert.infos:
info = alert_info.get("info")
if info.value.get('expires') > timezone.localtime():
active_alert_infos.append(alert_info)

if len(active_alert_infos) == 0:
return {
context.update({
'latest_active_alert': None
}

context = {
'latest_active_alert': active_alert_infos[0]
}
})
else:
context.update({
'latest_active_alert': active_alert_infos[0]
})

return render(request, "cap/active_alert.html", context)
118 changes: 64 additions & 54 deletions pages/home/static/js/weather_watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,71 +101,81 @@ $((async function () {
}


fetch(homeMapAlertsUrl).then(response => response.text()).then(alertsHTML => {
const $alerts = $(alertsHTML)
fetch(homeMapAlertsUrl)
.then(response => {
if (!response.ok) {
throw new Error('Error fetching alerts');
}
return response.text();
})
.then(alertsHTML => {
const $alerts = $(alertsHTML)

if ($alerts.length) {
$alerts.appendTo("#alerts-container")
$("#alerts-legend").show()
if ($alerts.length) {
$alerts.appendTo("#alerts-container")
$("#alerts-legend").show()


// add cap alerts layer
map.addSource("alert-areas", {
type: "geojson",
data: capGeojsonUrl,
});
// add cap alerts layer
map.addSource("alert-areas", {
type: "geojson",
data: capGeojsonUrl,
});

map.addLayer({
id: "alert-areas-layer",
type: "fill",
source: "alert-areas",
paint: {
"fill-color": [
"case",
["==", ["get", "severity"], "Extreme"],
"#d72f2a",
["==", ["get", "severity"], "Severe"],
"#f89904",
["==", ["get", "severity"], "Moderate"],
"#e4e616",
["==", ["get", "severity"], "Minor"],
"#53ffff",
["==", ["get", "severity"], "Unknown"],
"#3366ff",
"black",
],
"fill-opacity": 0.7,
"fill-outline-color": "#000",
},
});
map.addLayer({
id: "alert-areas-layer",
type: "fill",
source: "alert-areas",
paint: {
"fill-color": [
"case",
["==", ["get", "severity"], "Extreme"],
"#d72f2a",
["==", ["get", "severity"], "Severe"],
"#f89904",
["==", ["get", "severity"], "Moderate"],
"#e4e616",
["==", ["get", "severity"], "Minor"],
"#53ffff",
["==", ["get", "severity"], "Unknown"],
"#3366ff",
"black",
],
"fill-opacity": 0.7,
"fill-outline-color": "#000",
},
});

// CAP alerts layer on click
map.on("click", "alert-areas-layer", (e) => {
// Copy coordinates array.
// CAP alerts layer on click
map.on("click", "alert-areas-layer", (e) => {
// Copy coordinates array.

const description = e.features[0].properties.areaDesc;
const severity = e.features[0].properties.severity;
const event = e.features[0].properties.event;
const description = e.features[0].properties.areaDesc;
const severity = e.features[0].properties.severity;
const event = e.features[0].properties.event;

new maplibregl.Popup()
.setLngLat(e.lngLat)
.setHTML(`<div class="block" style="margin:10px"><h2 class="title" style="font-size:15px;">${description}</h2> <h2 class="subtitle" style="font-size:14px;">${event}</h2> <hr> <p>${severity} severity</p> </a></div>`)
.addTo(map);
new maplibregl.Popup()
.setLngLat(e.lngLat)
.setHTML(`<div class="block" style="margin:10px"><h2 class="title" style="font-size:15px;">${description}</h2> <h2 class="subtitle" style="font-size:14px;">${event}</h2> <hr> <p>${severity} severity</p> </a></div>`)
.addTo(map);


});
});

// Change the cursor to a pointer when the mouse is over the alerts layer.
map.on("mouseenter", "alert-areas-layer", () => {
map.getCanvas().style.cursor = "pointer";
});
// Change the cursor to a pointer when the mouse is over the alerts layer.
map.on("mouseenter", "alert-areas-layer", () => {
map.getCanvas().style.cursor = "pointer";
});

// Change it back to a pointer when it leaves.
map.on("mouseleave", "alert-areas-layer", () => {
map.getCanvas().style.cursor = "";
});
}
})
// Change it back to a pointer when it leaves.
map.on("mouseleave", "alert-areas-layer", () => {
map.getCanvas().style.cursor = "";
});
}
})
.catch(error => {
console.error("HOME_MAP_ALERTS_ERROR:", error)
})


// Create a popup object
Expand Down
10 changes: 7 additions & 3 deletions pages/home/templates/home/home_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,12 @@ <h2 class="section-title has-text-centered"> {% translate "Our Partners" %}</h2>

if (weatherWidgetWrapper && homeWeatherWidgetUrl) {
fetch(homeWeatherWidgetUrl)
.then(response => response.text())
.then(response => {
if (!response.ok) {
throw new Error('Error fetching weather widget data');
}
return response.text();
})
.then(data => {
weatherWidgetWrapper.html(data)

Expand Down Expand Up @@ -168,9 +173,8 @@ <h2 class="section-title has-text-centered"> {% translate "Our Partners" %}</h2>
}
})
.catch(error => {
console.error('Error:', error);
console.error('HOME_WEATHER_WIDGET_ERROR:', error);
});

}


Expand Down

0 comments on commit b78c391

Please sign in to comment.