45 lines
1.6 KiB
JavaScript
45 lines
1.6 KiB
JavaScript
import './styles/app.scss';
|
|
import htmx from 'htmx.org';
|
|
|
|
window.htmx = htmx;
|
|
document.body.addEventListener('htmx:beforeRequest', function () {
|
|
document.getElementById('request-failed').hidden = true;
|
|
});
|
|
|
|
document.body.addEventListener('htmx:responseError', function () {
|
|
document.getElementById('request-failed').hidden = false;
|
|
});
|
|
|
|
document.body.addEventListener('htmx:sendError', function () {
|
|
document.getElementById('request-failed').hidden = false;
|
|
});
|
|
document.addEventListener('click', (event) => {
|
|
const button = event.target.closest('[data-weather-location]');
|
|
if (!button) return;
|
|
const status = document.querySelector('[data-weather-location-status]');
|
|
if (!navigator.geolocation) {
|
|
if (status) status.textContent = 'Your browser does not support location services.';
|
|
return;
|
|
}
|
|
|
|
navigator.geolocation.getCurrentPosition(async ({coords}) => {
|
|
const payload = new URLSearchParams({
|
|
_token: button.dataset.locationToken,
|
|
latitude: String(coords.latitude),
|
|
longitude: String(coords.longitude),
|
|
});
|
|
const response = await fetch(button.dataset.locationUrl, {
|
|
method: 'POST',
|
|
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
|
|
body: payload,
|
|
});
|
|
if (!response.ok) {
|
|
if (status) status.textContent = 'Location could not be saved.';
|
|
return;
|
|
}
|
|
window.location.reload();
|
|
}, () => {
|
|
if (status) status.textContent = 'Location permission was not granted.';
|
|
});
|
|
});
|