local-weather/components/WeatherNow.tsx

27 lines
795 B
TypeScript
Raw Normal View History

import Temperature from "./Temperature";
2024-05-05 20:55:51 +00:00
import { Forecast, LocationType, coordType } from "@/types/types";
2024-05-04 21:04:30 +00:00
async function getForecast(location: LocationType): Promise<Forecast> {
2024-05-05 20:55:51 +00:00
const { lat, lng } = location.results[0].geometry.location;
2024-05-04 21:04:30 +00:00
const appId = "546911d860cb81f16585f7973b394b70";
const res = await fetch(
2024-05-05 20:55:51 +00:00
`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lng}&appid=${appId}`
2024-05-04 21:04:30 +00:00
);
if (!res.ok) {
throw new Error(`Failed to fetch the weather data`);
2024-05-04 21:04:30 +00:00
}
return res.json();
}
2024-05-05 20:55:51 +00:00
export default async function WeatherNow(props: { location: any }) {
2024-05-04 21:04:30 +00:00
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>
);
}