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