local-weather/components/WeatherNow.tsx

28 lines
789 B
TypeScript
Raw Normal View History

import Temperature from "./Temperature";
import { Forecast, LocationType } from "@/types/types";
2024-05-04 21:04:30 +00:00
async function getForecast(location: LocationType): Promise<Forecast> {
const { latitude, longtitude } = location;
const appId = "546911d860cb81f16585f7973b394b70";
const res = await fetch(
`https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longtitude}&appid=${appId}`
);
if (!res.ok) {
throw new Error(`Failed to fetch the weather data`);
2024-05-04 21:04:30 +00:00
}
return res.json();
}
export default async function WeatherNow(props: { location: LocationType }) {
const location = props.location;
const weather = await getForecast(location);
return (
<main>
<h1>Forecast</h1>
<Temperature tempInfo={weather.main}/>
2024-05-04 21:04:30 +00:00
<p></p>
</main>
);
}