Cleaned up some OpenWeatherMap logic.

This commit is contained in:
AlexFullmoon 2022-08-27 14:28:47 +03:00
parent 6a97d392c9
commit 2faa78163a
2 changed files with 5 additions and 8 deletions

View file

@ -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 <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 (
<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"} />
<div className="flex flex-col ml-3 text-left">
<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 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>
);