From 7096a04ad5d13fc71d6943a6d77f0baaf353e657 Mon Sep 17 00:00:00 2001 From: Yinsheng Zhou Date: Sun, 6 Jul 2025 21:58:32 -0700 Subject: [PATCH] "feat: enable fahrenheit/celsius temperature toggle on landing page" --- src/components/WeatherWidget.tsx | 42 +++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/src/components/WeatherWidget.tsx b/src/components/WeatherWidget.tsx index d308bb4..2037d79 100644 --- a/src/components/WeatherWidget.tsx +++ b/src/components/WeatherWidget.tsx @@ -11,6 +11,18 @@ const WeatherWidget = () => { icon: '', }); const [loading, setLoading] = useState(true); + const [unit, setUnit] = useState<'C' | 'F'>('C'); + + const convertTemperature = (tempCelsius: number, targetUnit: 'C' | 'F') => { + if (targetUnit === 'F') { + return ((tempCelsius * 9) / 5 + 32).toFixed(1); + } + return tempCelsius.toFixed(1); + }; + + const toggleUnit = () => { + setUnit((prev) => (prev === 'C' ? 'F' : 'C')); + }; useEffect(() => { const getApproxLocation = async () => { @@ -124,9 +136,33 @@ const WeatherWidget = () => { alt={data.condition} className="h-10 w-auto" /> - - {data.temperature}°C - +
+ + {convertTemperature(data.temperature, unit)}° + +
+ + +
+