From 2faa78163a575ae507c5d2521c2c190a553efac8 Mon Sep 17 00:00:00 2001 From: AlexFullmoon Date: Sat, 27 Aug 2022 14:28:47 +0300 Subject: [PATCH] Cleaned up some OpenWeatherMap logic. --- src/components/widgets/openweathermap/weather.jsx | 9 +++------ src/pages/api/widgets/openweathermap.js | 4 ++-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/components/widgets/openweathermap/weather.jsx b/src/components/widgets/openweathermap/weather.jsx index 9f0a61ac5..43edc0918 100644 --- a/src/components/widgets/openweathermap/weather.jsx +++ b/src/components/widgets/openweathermap/weather.jsx @@ -5,7 +5,7 @@ import Icon from "./icon"; export default function OpenWeatherMap({ options }) { const { data, error } = useSWR( - `/api/widgets/openweathermap?lat=${options.latitude}&lon=${options.longitude}&apiKey=${options.apiKey}&duration=${options.cache}` + `/api/widgets/openweathermap?lat=${options.latitude}&lon=${options.longitude}&apiKey=${options.apiKey}&duration=${options.cache}&units=${options.units}` ); if (error) { @@ -27,17 +27,14 @@ export default function OpenWeatherMap({ options }) { if (data.error) { return
; } - // OpenWeatherMap returns temperature in Kelvins - var temp_c = data.main.temp - 273.15; - var temp_f = temp_c * 9 / 5 + 32; return (
data.sys.sunrise) && (data.dt < data.sys.sundown) ? "day" : "night"} />
- {options.units === "metric" ? temp_c.toFixed(0) : temp_f.toFixed(0)}° + {data.main.temp.toFixed(1)}° - {data.weather[0].description} + {data.weather[0].description.charAt(0).toUpperCase() + data.weather[0].description.slice(1)}
); diff --git a/src/pages/api/widgets/openweathermap.js b/src/pages/api/widgets/openweathermap.js index 471f69df9..07dc91399 100644 --- a/src/pages/api/widgets/openweathermap.js +++ b/src/pages/api/widgets/openweathermap.js @@ -1,9 +1,9 @@ import cachedFetch from "utils/cached-fetch"; export default async function handler(req, res) { - const { lat, lon, apiKey, duration } = req.query; + const { lat, lon, apiKey, duration, units } = req.query; - const api_url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${apiKey}`; + const api_url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${apiKey}&units=${units}`; res.send(await cachedFetch(api_url, duration)); }