From a21c44185f15adf5276a22776f4f3e85646b1cdc Mon Sep 17 00:00:00 2001 From: ChrQR Date: Fri, 10 May 2024 01:10:37 +0200 Subject: [PATCH] added weather to context provided by weatherNow. The application structure and naming should take in to account the weathernow component will be the container --- app/actions.ts | 14 +------------- components/Temperature.tsx | 15 +++++++-------- components/WeatherNow.tsx | 11 +++++++---- 3 files changed, 15 insertions(+), 25 deletions(-) diff --git a/app/actions.ts b/app/actions.ts index 50756c0..5ea339a 100644 --- a/app/actions.ts +++ b/app/actions.ts @@ -16,19 +16,7 @@ export async function getLocation(searchLocation: string): Promise{ } } -// takes coordinates in coordType format and returns json with weather forecast. -export async function getForecast(geoLocation: coordType): Promise { - const { lat, lng } = geoLocation.geo; - const appId = process.env.WEATHER_API; - const res = await fetch( - `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lng}&appid=${appId}` - ); - if (!res.ok) { - throw new Error(`Failed to fetch the weather data`); - } - const data: Forecast = await res.json(); - return data; - } + export async function getHourlyForecast(geoLocation: coordType): Promise { const { lat, lng } = geoLocation.geo; diff --git a/components/Temperature.tsx b/components/Temperature.tsx index 4400113..56df4c4 100644 --- a/components/Temperature.tsx +++ b/components/Temperature.tsx @@ -1,15 +1,14 @@ import { TempInfo } from "@/types/types"; +import { useContext } from "react"; +import { WeatherContext } from "./WeatherNow"; -export default function Temperature(props: {tempInfo:TempInfo}) { - const kelvin = 273.15; - const tempInfo = props.tempInfo; - const fullTemp = tempInfo.temp - kelvin; - const temperature = fullTemp.toFixed(2); - const fullFeels = tempInfo.feels_like - kelvin; - const feelsLike = fullFeels.toFixed(2); + +export default function Temperature() { + const { weather } = useContext(WeatherContext) + const feels = weather.current.apparent_temperature; return ( -

The temperature right now is {temperature}°C and feels like {feelsLike}°C

+

{feels}°C

) } diff --git a/components/WeatherNow.tsx b/components/WeatherNow.tsx index c92589b..440f595 100644 --- a/components/WeatherNow.tsx +++ b/components/WeatherNow.tsx @@ -1,13 +1,13 @@ "use client"; -import { getForecast, getHourlyForecast } from "@/app/actions"; +import { getHourlyForecast } from "@/app/actions"; import Temperature from "./Temperature"; -import { Forecast, HourlyForecast, WeatherContextType, coordType } from "@/types/types"; +import { HourlyForecast, WeatherContextType} from "@/types/types"; import { createContext, useContext, useEffect, useState } from "react"; -import { defaultForecast, defaultHourlyForecast } from "@/app/defaultState"; +import { defaultHourlyForecast } from "@/app/defaultState"; import { LocationContext } from "@/app/page"; -export const weatherContext = createContext({ +export const WeatherContext = createContext({ weather: defaultHourlyForecast, setWeather: () => {} // Default function, does nothing }); @@ -34,6 +34,9 @@ export default function WeatherNow() { return ( <>

Here is the current weather in {geoLocation.name}

+ + + ); }