diff --git a/src/pages/Weather.jsx b/src/pages/Weather.jsx index 18c3b43..3f10a80 100644 --- a/src/pages/Weather.jsx +++ b/src/pages/Weather.jsx @@ -2,27 +2,29 @@ * WEATHER DASHBOARD TODOs * ----------------------- * Easy: - * - [ ] Add °C / °F toggle - * - [ ] Show weather icon (current + forecast) - * - [ ] Show feels-like temperature & wind speed - * - [ ] Add loading skeleton instead of plain text - * - [ ] Style forecast cards with condition color badges + * - [x] Extract API call into /src/services/weather.js and add caching + * - [ ] Add °C / °F toggle + * - [ ] Show weather icon (current + forecast) + * - [ ] Show feels-like temperature & wind speed + * - [ ] Add loading skeleton instead of plain text + * - [ ] Style forecast cards with condition color badges * Medium: - * - [ ] Dynamic background / gradient based on condition (sunny, rain, snow) - * - [ ] Input debounced search (on stop typing) - * - [ ] Persist last searched city (localStorage) - * - [ ] Add error retry button component - * - [ ] Add favorites list (pin cities) + * - [ ] Dynamic background / gradient based on condition (sunny, rain, snow) + * - [ ] Input debounced search (on stop typing) + * - [ ] Persist last searched city (localStorage) + * - [ ] Add error retry button component + * - [ ] Add favorites list (pin cities) * Advanced: - * - [ ] Hourly forecast visualization (line / area chart) - * - [ ] Animate background transitions - * - [ ] Add geolocation: auto-detect user city (with permission) - * - [ ] Extract API call into /src/services/weather.js and add caching + * - [ ] Hourly forecast visualization (line / area chart) + * - [ ] 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'; import Card from '../components/Card.jsx'; +import { getWeatherData, clearWeatherCache, getCacheStats } from '../services/weather.js'; export default function Weather() { const [city, setCity] = useState('London'); @@ -33,15 +35,14 @@ export default function Weather() { useEffect(() => { fetchWeather(city); - // eslint-disable-next-line react-hooks/exhaustive-deps + // eslint-disable-next-line react-hooks/exhaustive-deps }, []); async function fetchWeather(c) { try { - setLoading(true); setError(null); - const res = await fetch(`https://wttr.in/${encodeURIComponent(c)}?format=j1`); - if (!res.ok) throw new Error('Failed to fetch'); - const json = await res.json(); + setLoading(true); + setError(null); + const json = await getWeatherData(c); // Using the service instead of direct fetch setData(json); } catch (e) { setError(e); @@ -50,6 +51,19 @@ export default function Weather() { } } + // Helper function to clear cache and refetch (for testing) + const handleClearCache = () => { + clearWeatherCache(); + fetchWeather(city); + }; + + // Helper function to show cache stats (for development) + const handleShowCacheStats = () => { + const stats = getCacheStats(); + console.log('Cache Statistics:', stats); + alert(`Cache has ${stats.size} entries. Check console for details.`); + }; + const current = data?.current_condition?.[0]; const forecast = data?.weather?.slice(0,3) || []; @@ -57,40 +71,56 @@ export default function Weather() { const displayTemp = (c) => unit === 'C' ? c : Math.round((c * 9/5) + 32); return ( -
Temperature: {displayTemp(Number(current.temp_C))}°{unit}
-Humidity: {current.humidity}%
-Desc: {current.weatherDesc?.[0]?.value}
-Avg Temp: {displayTemp(Number(day.avgtempC))}°{unit}
-Sunrise: {day.astronomy?.[0]?.sunrise}
+ {data && !loading && ( +Temperature: {displayTemp(Number(current.temp_C))}°{unit}
+Humidity: {current.humidity}%
+Desc: {current.weatherDesc?.[0]?.value}
Avg Temp: {displayTemp(Number(day.avgtempC))}°{unit}
+Sunrise: {day.astronomy?.[0]?.sunrise}
+Sunset: {day.astronomy?.[0]?.sunset}
+