Cleaned up some OpenWeatherMap logic.

pull/25/head
AlexFullmoon 2 years ago
parent 6a97d392c9
commit 2faa78163a

@ -5,7 +5,7 @@ import Icon from "./icon";
export default function OpenWeatherMap({ options }) { export default function OpenWeatherMap({ options }) {
const { data, error } = useSWR( 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) { if (error) {
@ -27,17 +27,14 @@ export default function OpenWeatherMap({ options }) {
if (data.error) { if (data.error) {
return <div className="order-last grow flex-none flex flex-row items-center justify-end"></div>; return <div className="order-last grow flex-none flex flex-row items-center justify-end"></div>;
} }
// OpenWeatherMap returns temperature in Kelvins
var temp_c = data.main.temp - 273.15;
var temp_f = temp_c * 9 / 5 + 32;
return ( return (
<div className="order-last grow flex-none flex flex-row items-center justify-end"> <div className="order-last grow flex-none flex flex-row items-center justify-end">
<Icon condition={data.weather[0].id} timeOfDay={(data.dt > data.sys.sunrise) && (data.dt < data.sys.sundown) ? "day" : "night"} /> <Icon condition={data.weather[0].id} timeOfDay={(data.dt > data.sys.sunrise) && (data.dt < data.sys.sundown) ? "day" : "night"} />
<div className="flex flex-col ml-3 text-left"> <div className="flex flex-col ml-3 text-left">
<span className="text-theme-800 dark:text-theme-200 text-sm"> <span className="text-theme-800 dark:text-theme-200 text-sm">
{options.units === "metric" ? temp_c.toFixed(0) : temp_f.toFixed(0)}&deg; {data.main.temp.toFixed(1)}&deg;
</span> </span>
<span className="text-theme-800 dark:text-theme-200 text-xs">{data.weather[0].description}</span> <span className="text-theme-800 dark:text-theme-200 text-xs">{data.weather[0].description.charAt(0).toUpperCase() + data.weather[0].description.slice(1)}</span>
</div> </div>
</div> </div>
); );

@ -1,9 +1,9 @@
import cachedFetch from "utils/cached-fetch"; import cachedFetch from "utils/cached-fetch";
export default async function handler(req, res) { 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)); res.send(await cachedFetch(api_url, duration));
} }

Loading…
Cancel
Save