From bbf8163a504b7e22cf78717f1490dfe6aa44ede2 Mon Sep 17 00:00:00 2001 From: elpatron Date: Thu, 28 May 2026 16:21:34 +0200 Subject: [PATCH] feat(weather): allow fetching OpenWeatherMap weather by location name and auto-populating GPS coordinates --- client/src/components/LogEntryEditor.tsx | 42 ++++++++++++++++++++---- client/src/i18n/locales/de.json | 4 ++- client/src/i18n/locales/en.json | 4 ++- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/client/src/components/LogEntryEditor.tsx b/client/src/components/LogEntryEditor.tsx index 058f6e1..9c14807 100644 --- a/client/src/components/LogEntryEditor.tsx +++ b/client/src/components/LogEntryEditor.tsx @@ -92,6 +92,7 @@ export default function LogEntryEditor({ entryId, logbookId, onBack }: LogEntryE const [evGpsLat, setEvGpsLat] = useState('') const [evGpsLng, setEvGpsLng] = useState('') const [evRemarks, setEvRemarks] = useState('') + const [evLocationName, setEvLocationName] = useState('') const [loading, setLoading] = useState(false) const [saving, setSaving] = useState(false) @@ -382,7 +383,10 @@ export default function LogEntryEditor({ entryId, logbookId, onBack }: LogEntryE } const handleFetchWeather = async () => { - if (!evGpsLat || !evGpsLng) { + const hasGps = evGpsLat && evGpsLng + const hasLocation = evLocationName.trim() + + if (!hasGps && !hasLocation) { showAlert(t('settings.gps_error')) return } @@ -395,14 +399,25 @@ export default function LogEntryEditor({ entryId, logbookId, onBack }: LogEntryE setWeatherLoading(true) try { - const res = await fetch( - `https://api.openweathermap.org/data/2.5/weather?lat=${evGpsLat}&lon=${evGpsLng}&appid=${apiKey}&units=metric` - ) + let url = '' + if (hasLocation) { + url = `https://api.openweathermap.org/data/2.5/weather?q=${encodeURIComponent(evLocationName.trim())}&appid=${apiKey}&units=metric` + } else { + url = `https://api.openweathermap.org/data/2.5/weather?lat=${evGpsLat}&lon=${evGpsLng}&appid=${apiKey}&units=metric` + } + + const res = await fetch(url) if (!res.ok) throw new Error('Weather API rejected the request') const data = await res.json() + // If fetched by location, automatically pre-fill GPS coordinates + if (hasLocation && data.coord) { + setEvGpsLat(Number(data.coord.lat).toFixed(6)) + setEvGpsLng(Number(data.coord.lon).toFixed(6)) + } + // Convert wind speed m/s to Beaufort scale const mps = data.wind.speed || 0 let bft = 0 @@ -513,6 +528,7 @@ export default function LogEntryEditor({ entryId, logbookId, onBack }: LogEntryE setEvGpsLat('') setEvGpsLng('') setEvRemarks('') + setEvLocationName('') } const handleDeleteEvent = (index: number) => { @@ -943,6 +959,18 @@ export default function LogEntryEditor({ entryId, logbookId, onBack }: LogEntryE
+
+ + setEvLocationName(e.target.value)} + disabled={saving} + /> +
+
@@ -978,13 +1006,15 @@ export default function LogEntryEditor({ entryId, logbookId, onBack }: LogEntryE onClick={handleFetchWeather} title={t('logs.weather_btn')} style={{ width: 'auto', padding: '12px' }} - disabled={saving || weatherLoading || !evGpsLat || !evGpsLng} + disabled={saving || weatherLoading || (!evLocationName.trim() && (!evGpsLat || !evGpsLng))} >
+
+
-
-