improve weather widget error state

pull/3/head
Ben Phelps 2 years ago
parent 89d0478443
commit 5711b22b4e

@ -3,7 +3,5 @@ import mapIcon from "utils/condition-map";
export default function Icon({ condition, timeOfDay }) { export default function Icon({ condition, timeOfDay }) {
const Icon = mapIcon(condition, timeOfDay); const Icon = mapIcon(condition, timeOfDay);
return ( return <Icon className="w-10 h-10 text-theme-800 dark:text-theme-200"></Icon>;
<Icon className="mt-2 w-10 h-10 text-theme-800 dark:text-theme-200"></Icon>
);
} }

@ -1,40 +1,41 @@
import useSWR from "swr"; import useSWR from "swr";
import { BiError } from "react-icons/bi";
import Icon from "./icon"; import Icon from "./icon";
export default function Weather({ options }) { export default function Weather({ options }) {
const { data, error } = useSWR( const { data, error } = useSWR(
`/api/widgets/weather?lat=${options.latitude}&lon=${options.longitude}&apiKey=${options.apiKey}&duration=${options.cache}`, `/api/widgets/weather?lat=${options.latitude}&lon=${options.longitude}&apiKey=${options.apiKey}&duration=${options.cache}`
{
revalidateOnFocus: false,
revalidateOnReconnect: false,
}
); );
if (error) { if (error) {
return <div>failed to load</div>; return (
<div className="order-last grow flex-none flex flex-row items-center justify-end">
<BiError className="w-8 h-8 text-theme-800 dark:text-theme-200" />
<div className="flex flex-col ml-3 text-left">
<span className="text-theme-800 dark:text-theme-200 text-sm">API</span>
<span className="text-theme-800 dark:text-theme-200 text-xs">Error</span>
</div>
</div>
);
} }
if (!data) { if (!data) {
return <div className="flex flex-col items-center justify-center"></div>; return <div className="order-last grow flex-none flex flex-row items-center justify-end"></div>;
} }
if (data.error) { if (data.error) {
return <div className="flex flex-col items-center justify-center"></div>; return <div className="order-last grow flex-none flex flex-row items-center justify-end"></div>;
} }
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 <Icon condition={data.current.condition.code} timeOfDay={data.current.is_day ? "day" : "night"} />
condition={data.current.condition.code}
timeOfDay={data.current.is_day ? "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">
{Math.round(data.current.temp_f)}&deg; {options.units === "metric" ? data.current.temp_c : data.current.temp_f}&deg;
</span>
<span className="text-theme-800 dark:text-theme-200 text-xs">
{data.current.condition.text}
</span> </span>
<span className="text-theme-800 dark:text-theme-200 text-xs">{data.current.condition.text}</span>
</div> </div>
</div> </div>
); );

Loading…
Cancel
Save