From 55b82e5e5ddd2d764e816ea67599546f2e26c359 Mon Sep 17 00:00:00 2001 From: ChrQR Date: Sun, 5 May 2024 09:51:29 +0200 Subject: [PATCH] did some stuff with LocationSearch component and moved types to separate file. --- app/page.tsx | 3 ++ components/LocationSearch.tsx | 12 ++++- components/Temperature.tsx | 26 ++++++++++ components/Weather.tsx | 91 ----------------------------------- components/WeatherNow.tsx | 62 ++---------------------- types/types.ts | 52 ++++++++++++++++++++ 6 files changed, 95 insertions(+), 151 deletions(-) delete mode 100644 components/Weather.tsx create mode 100644 types/types.ts diff --git a/app/page.tsx b/app/page.tsx index 52de684..07ffbae 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -6,6 +6,9 @@ const location = { longtitude: '12.549600' } + + + export default function Home() { return (
diff --git a/components/LocationSearch.tsx b/components/LocationSearch.tsx index 33fbda2..77f78dc 100644 --- a/components/LocationSearch.tsx +++ b/components/LocationSearch.tsx @@ -1,7 +1,15 @@ +'use client' +import { createContext } from "react"; + + + +export default function LocationSearch() { -export default async function LocationSearch() { return ( -

yes yes yes

+ <> + + + ); } diff --git a/components/Temperature.tsx b/components/Temperature.tsx index e69de29..87e0d5a 100644 --- a/components/Temperature.tsx +++ b/components/Temperature.tsx @@ -0,0 +1,26 @@ +interface TempInfo { + tempInfo: { + temp: number; + feels_like: number; + temp_min: number; + temp_max: number; + pressure: number; + humidity: number; + sea_level: number; + grnd_level: number; + }; +}; + +export default function Temperature(props: 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); + + return ( +

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

+ ) +} + diff --git a/components/Weather.tsx b/components/Weather.tsx deleted file mode 100644 index 1bffc63..0000000 --- a/components/Weather.tsx +++ /dev/null @@ -1,91 +0,0 @@ -import { error } from "console"; -import { useEffect, useState } from "react"; -import { json } from "stream/consumers"; - -interface Forecast { - coord: { - lon: number; - lat: number; - }, - weather: - { - id: number, - main: string, - description: string, - icon: string; - }[] - , - base: string; - main: { - temp: number; - feels_like: number; - temp_min: number; - temp_max: number; - pressure: number; - humidity: number; - sea_level: number; - grnd_level: number; - }, - visibility: number; - wind: { - speed: number; - deg: number; - gust: number; - }, - rain: { - onehour: number - }, - clouds: { - all: number - }, - dt: number, - sys: { - type: number, - id: number, - country: string, - sunrise: number, - sunset: number; - }, - timezone: number; - id: number; - name: string; - cod: number; -}; - -interface LocationType { - latitude: string; - longtitude: string; -} -const kelvin = 273.15; - -async function getForecast(location: LocationType): Promise { - 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(`This is not great ${error}`); - } - return res.json(); -} - -export default async function Weather(props: {location: LocationType}) { - const [weather, setWeather] = useState({}); - const location = props.location - const appId = '546911d860cb81f16585f7973b394b70'; - // const weather = await getForecast(location) - // const temp = weather.main.temp - kelvin - useEffect(() => { - fetch(`https://api.openweathermap.org/data/2.5/weather?lat=${location.latitude}&lon=${location.longtitude}&appid=${appId}`) - .then(response => response.json()) - .then(json => setWeather(json)) - .catch(error => console.log(error)) - }) - return ( -
-

Forecast

-
- { weather ?
{JSON.stringify(weather, null, 2)}
: 'loading...'} -
-
- ); - } \ No newline at end of file diff --git a/components/WeatherNow.tsx b/components/WeatherNow.tsx index ef11ea1..126a320 100644 --- a/components/WeatherNow.tsx +++ b/components/WeatherNow.tsx @@ -1,57 +1,5 @@ -import { error } from "console"; - -interface Forecast { - coord: { - lon: number; - lat: number; - }; - weather: { - id: number; - main: string; - description: string; - icon: string; - }[]; - base: string; - main: { - temp: number; - feels_like: number; - temp_min: number; - temp_max: number; - pressure: number; - humidity: number; - sea_level: number; - grnd_level: number; - }; - visibility: number; - wind: { - speed: number; - deg: number; - gust: number; - }; - rain: { - onehour: number; - }; - clouds: { - all: number; - }; - dt: number; - sys: { - type: number; - id: number; - country: string; - sunrise: number; - sunset: number; - }; - timezone: number; - id: number; - name: string; - cod: number; -} - -interface LocationType { - latitude: string; - longtitude: string; -} +import Temperature from "./Temperature"; +import { Forecast, LocationType } from "@/types/types"; async function getForecast(location: LocationType): Promise { @@ -61,20 +9,18 @@ async function getForecast(location: LocationType): Promise { `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longtitude}&appid=${appId}` ); if (!res.ok) { - throw new Error(`This is not great ${error}`); + throw new Error(`Failed to fetch the weather data`); } return res.json(); } export default async function WeatherNow(props: { location: LocationType }) { - const kelvin = 273.15; const location = props.location; const weather = await getForecast(location); - const temp = weather.main.temp - kelvin; return (

Forecast

-

{temp}

+

); diff --git a/types/types.ts b/types/types.ts new file mode 100644 index 0000000..7c5b426 --- /dev/null +++ b/types/types.ts @@ -0,0 +1,52 @@ +export interface Forecast { + coord: { + lon: number; + lat: number; + }; + weather: { + id: number; + main: string; + description: string; + icon: string; + }[]; + base: string; + main: { + temp: number; + feels_like: number; + temp_min: number; + temp_max: number; + pressure: number; + humidity: number; + sea_level: number; + grnd_level: number; + }; + visibility: number; + wind: { + speed: number; + deg: number; + gust: number; + }; + rain: { + onehour: number; + }; + clouds: { + all: number; + }; + dt: number; + sys: { + type: number; + id: number; + country: string; + sunrise: number; + sunset: number; + }; + timezone: number; + id: number; + name: string; + cod: number; + } + +export interface LocationType { + latitude: string; + longtitude: string; + } \ No newline at end of file