diff --git a/src/pages/Weather.jsx b/src/pages/Weather.jsx index 3f10a80..56f378d 100644 --- a/src/pages/Weather.jsx +++ b/src/pages/Weather.jsx @@ -19,7 +19,6 @@ * - [ ] Animate background transitions * - [ ] Add geolocation: auto-detect user city (with permission) */ - import { useEffect, useState } from 'react'; import Loading from '../components/Loading.jsx'; import ErrorMessage from '../components/ErrorMessage.jsx'; @@ -27,7 +26,9 @@ import Card from '../components/Card.jsx'; import { getWeatherData, clearWeatherCache, getCacheStats } from '../services/weather.js'; export default function Weather() { - const [city, setCity] = useState('London'); + const [city, setCity] = useState(() => { + return localStorage.getItem('lastCity') || 'London'; + }); const [data, setData] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); @@ -36,14 +37,18 @@ export default function Weather() { useEffect(() => { fetchWeather(city); // eslint-disable-next-line react-hooks/exhaustive-deps - }, []); + }, []); async function fetchWeather(c) { try { setLoading(true); setError(null); - const json = await getWeatherData(c); // Using the service instead of direct fetch + + const json = await getWeatherData(c);// Using the service instead of direct fetch setData(json); + + // Save the searched city to localStorage + localStorage.setItem('lastCity', c); } catch (e) { setError(e); } finally { @@ -51,6 +56,12 @@ export default function Weather() { } } + const handleSubmit = (e) => { + e.preventDefault(); + if (!city.trim()) return; + fetchWeather(city); + }; + // Helper function to clear cache and refetch (for testing) const handleClearCache = () => { clearWeatherCache(); @@ -65,34 +76,37 @@ export default function Weather() { }; const current = data?.current_condition?.[0]; - const forecast = data?.weather?.slice(0,3) || []; + const forecast = data?.weather?.slice(0, 3) || []; // Helper to convert °C to °F - const displayTemp = (c) => unit === 'C' ? c : Math.round((c * 9/5) + 32); + const displayTemp = (c) => (unit === 'C' ? c : Math.round((c * 9) / 5 + 32)); return (