From c855f98dd6cb26f73b0e9ed48e5dd7728f77fb4b Mon Sep 17 00:00:00 2001 From: Arghyadeep Date: Mon, 13 Oct 2025 19:52:43 +0530 Subject: [PATCH] feat: persist last searched city in localStorage and refactor form submission --- src/pages/Weather.jsx | 73 +++++++++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 23 deletions(-) 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 (

🌤️ Weather Dashboard

-
{e.preventDefault(); fetchWeather(city)}}> - + setCity(e.target.value)} - placeholder="Enter city name..." + placeholder="Enter city name..." />
- + {/* Development tools - you can remove these later */} -
- - -
@@ -106,17 +120,29 @@ export default function Weather() { {/* Current Weather */}

{data.nearest_area?.[0]?.areaName?.[0]?.value || city}

-

Temperature: {displayTemp(Number(current.temp_C))}°{unit}

-

Humidity: {current.humidity}%

-

Desc: {current.weatherDesc?.[0]?.value}

+

+ Temperature: {displayTemp(Number(current.temp_C))}°{unit} +

+

+ Humidity: {current.humidity}% +

+

+ Desc: {current.weatherDesc?.[0]?.value} +

{/* 3-Day Forecast */} {forecast.map((day, i) => ( - -

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} +

))}
@@ -124,3 +150,4 @@ export default function Weather() {
); } +