local-weather/components/WeatherNow.tsx
ChrQR 83b337db03
All checks were successful
Docker Build & Publish / Build Docker (push) Successful in 1m9s
Fixed for mobile?
2024-05-12 21:14:36 +02:00

52 lines
1.5 KiB
TypeScript

"use client";
import { getHourlyForecast } from "@/app/actions";
import Temperature from "./Temperature";
import { Forecast, WeatherContextType } from "@/types/types";
import { createContext, useContext, useEffect, useState } from "react";
import { defaultHourlyForecast } from "@/app/defaultState";
import { LocationContext } from "@/context/LocationContext";
import WeatherHero from "./WeatherHero";
import DailyCard from "./DailyCard/DailyCard";
import CardContainer from "./DailyCard/CardContainer";
export const WeatherContext = createContext<WeatherContextType>({
weather: defaultHourlyForecast,
setWeather: () => {}, // Default function, does nothing
});
export default function WeatherNow() {
const { geoLocation } = useContext(LocationContext);
const [weather, setWeather] = useState<Forecast>(defaultHourlyForecast);
const contextValue: WeatherContextType = {
weather,
setWeather,
};
useEffect(() => {
let mounted = true;
getHourlyForecast(geoLocation).then((data) => {
if (mounted) {
setWeather(data);
}
});
return () => {
mounted = false;
};
}, [geoLocation]);
return (
<div className="text-center">
<h1 className="my-4 text-2xl">
Here is the weather today in {geoLocation.name}
</h1>
<WeatherContext.Provider value={contextValue}>
<WeatherHero />
<Temperature />
<h2 className="mt-2 text-xl">And the forecast for the coming week</h2>
<CardContainer weather={weather}/>
</WeatherContext.Provider>
</div>
);
}