v1.02 - Location Saving

This commit is contained in:
fraxle
2026-05-16 12:20:13 +01:00
parent 5d371008e5
commit 6dab145e90
2 changed files with 39 additions and 8 deletions
+15 -8
View File
@@ -40,15 +40,22 @@ export function UTCIForecast() {
// Each useState() pairs a value with a setter. Calling the setter
// re-renders the page with the new value.
// The location we're forecasting for. Change the values below to set
// a different starting location for new visitors.
const [location, setLocation] = useState({
name: 'Pangbourne, Berkshire',
lat: 51.4839,
lon: -1.0725,
country: 'GB',
// The location we're forecasting for. Restored from localStorage if the
// user has visited before, otherwise defaults to Pangbourne, Berkshire.
const [location, setLocation] = useState(() => {
try {
const saved = localStorage.getItem('sunscope_last_location');
if (saved) return JSON.parse(saved);
} catch (e) { /* ignore */ }
return { name: 'Pangbourne, Berkshire', lat: 51.4839, lon: -1.0725, country: 'GB' };
});
// Wrapper that persists the location before updating state.
const setLocationAndSave = (loc) => {
try { localStorage.setItem('sunscope_last_location', JSON.stringify(loc)); } catch (e) { /* ignore */ }
setLocation(loc);
};
const [forecast, setForecast] = useState(null); // raw Open-Meteo response
const [loading, setLoading] = useState(false); // true while fetching
const [error, setError] = useState(null); // fetch error message
@@ -750,7 +757,7 @@ export function UTCIForecast() {
key=${`${r.id}-${r.latitude}`}
class="utci-result"
onClick=${() => {
setLocation({
setLocationAndSave({
name: `${r.name}${r.admin1 ? ', ' + r.admin1 : ''}`,
lat: r.latitude,
lon: r.longitude,