diff --git a/src/pages/Weather.jsx b/src/pages/Weather.jsx index 56f378d..80a866b 100644 --- a/src/pages/Weather.jsx +++ b/src/pages/Weather.jsx @@ -19,36 +19,40 @@ * - [ ] 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'; +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(() => { - return localStorage.getItem('lastCity') || 'London'; + return localStorage.getItem("lastCity") || "London"; }); const [data, setData] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); - const [unit, setUnit] = useState('C'); // °C by default + const [unit, setUnit] = useState("C"); // °C by default 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); + localStorage.setItem("lastCity", c); } catch (e) { setError(e); } finally { @@ -71,7 +75,7 @@ export default function Weather() { // Helper function to show cache stats (for development) const handleShowCacheStats = () => { const stats = getCacheStats(); - console.log('Cache Statistics:', stats); + console.log("Cache Statistics:", stats); alert(`Cache has ${stats.size} entries. Check console for details.`); }; @@ -79,7 +83,20 @@ export default function Weather() { 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)); + + const getBadgeStyle = (condition) => { + if (!condition) return { color: "#E0E0E0", label: "Clear 🌤️" }; + + const desc = condition.toLowerCase(); + if (desc.includes("sun")) return { color: "#FFD54F", label: "Sunny ☀️" }; + if (desc.includes("rain")) return { color: "#4FC3F7", label: "Rainy 🌧️" }; + if (desc.includes("snow")) return { color: "#81D4FA", label: "Snowy ❄️" }; + if (desc.includes("cloud")) return { color: "#B0BEC5", label: "Cloudy ☁️" }; + if (desc.includes("storm") || desc.includes("thunder")) + return { color: "#9575CD", label: "Storm ⛈️" }; + return { color: "#E0E0E0", label: "Clear 🌤️" }; + }; return (
- Temperature: {displayTemp(Number(current.temp_C))}°{unit} + Temperature:{" "} + {displayTemp(Number(current.temp_C))}°{unit}
Humidity: {current.humidity}% @@ -132,22 +155,44 @@ export default function Weather() {
- Avg Temp: {displayTemp(Number(day.avgtempC))}°{unit} -
-- Sunrise: {day.astronomy?.[0]?.sunrise} -
-- Sunset: {day.astronomy?.[0]?.sunset} -
-+ Avg Temp: {displayTemp(Number(day.avgtempC))} + °{unit} +
++ Sunrise: {day.astronomy?.[0]?.sunrise} +
++ Sunset: {day.astronomy?.[0]?.sunset} +
+