2024-05-06 21:22:57 +00:00
|
|
|
"use client";
|
2024-05-04 21:04:30 +00:00
|
|
|
|
2024-05-09 23:10:37 +00:00
|
|
|
import { getHourlyForecast } from "@/app/actions";
|
2024-05-06 21:22:57 +00:00
|
|
|
import Temperature from "./Temperature";
|
2024-05-12 05:06:44 +00:00
|
|
|
import { Forecast, WeatherContextType } from "@/types/types";
|
2024-05-09 23:00:26 +00:00
|
|
|
import { createContext, useContext, useEffect, useState } from "react";
|
2024-05-09 23:10:37 +00:00
|
|
|
import { defaultHourlyForecast } from "@/app/defaultState";
|
2024-05-10 23:21:34 +00:00
|
|
|
import { LocationContext } from "@/context/LocationContext";
|
2024-05-10 12:50:01 +00:00
|
|
|
import WeatherHero from "./WeatherHero";
|
2024-05-12 05:06:44 +00:00
|
|
|
import DailyCard from "./DailyCard/DailyCard";
|
2024-05-12 19:14:36 +00:00
|
|
|
import CardContainer from "./DailyCard/CardContainer";
|
2024-05-04 21:04:30 +00:00
|
|
|
|
2024-05-09 23:10:37 +00:00
|
|
|
export const WeatherContext = createContext<WeatherContextType>({
|
2024-05-09 23:00:26 +00:00
|
|
|
weather: defaultHourlyForecast,
|
2024-05-10 12:50:01 +00:00
|
|
|
setWeather: () => {}, // Default function, does nothing
|
2024-05-09 23:00:26 +00:00
|
|
|
});
|
2024-05-09 21:40:14 +00:00
|
|
|
|
2024-05-10 17:23:02 +00:00
|
|
|
|
2024-05-09 21:40:14 +00:00
|
|
|
export default function WeatherNow() {
|
2024-05-10 12:50:01 +00:00
|
|
|
const { geoLocation } = useContext(LocationContext);
|
2024-05-12 05:06:44 +00:00
|
|
|
const [weather, setWeather] = useState<Forecast>(defaultHourlyForecast);
|
2024-05-09 23:00:26 +00:00
|
|
|
const contextValue: WeatherContextType = {
|
|
|
|
weather,
|
2024-05-10 12:50:01 +00:00
|
|
|
setWeather,
|
2024-05-09 23:00:26 +00:00
|
|
|
};
|
2024-05-10 17:23:02 +00:00
|
|
|
|
2024-05-06 21:22:57 +00:00
|
|
|
useEffect(() => {
|
|
|
|
let mounted = true;
|
2024-05-09 23:00:26 +00:00
|
|
|
getHourlyForecast(geoLocation).then((data) => {
|
2024-05-06 21:22:57 +00:00
|
|
|
if (mounted) {
|
|
|
|
setWeather(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return () => {
|
|
|
|
mounted = false;
|
|
|
|
};
|
2024-05-09 21:40:14 +00:00
|
|
|
}, [geoLocation]);
|
2024-05-04 21:04:30 +00:00
|
|
|
return (
|
2024-05-10 22:45:03 +00:00
|
|
|
<div className="text-center">
|
2024-05-10 12:50:01 +00:00
|
|
|
<h1 className="my-4 text-2xl">
|
2024-05-12 05:15:21 +00:00
|
|
|
Here is the weather today in {geoLocation.name}
|
2024-05-10 12:50:01 +00:00
|
|
|
</h1>
|
2024-05-09 23:10:37 +00:00
|
|
|
<WeatherContext.Provider value={contextValue}>
|
2024-05-10 12:50:01 +00:00
|
|
|
<WeatherHero />
|
2024-05-09 23:10:37 +00:00
|
|
|
<Temperature />
|
2024-05-12 19:14:36 +00:00
|
|
|
<h2 className="mt-2 text-xl">And the forecast for the coming week</h2>
|
|
|
|
<CardContainer weather={weather}/>
|
2024-05-09 23:10:37 +00:00
|
|
|
</WeatherContext.Provider>
|
2024-05-10 22:45:03 +00:00
|
|
|
</div>
|
2024-05-04 21:04:30 +00:00
|
|
|
);
|
|
|
|
}
|