From 480b5188ba5539773ca9a1736422a1cc6456e4ed Mon Sep 17 00:00:00 2001 From: "theis.gaedigk" Date: Thu, 31 Jul 2025 15:58:38 +0200 Subject: [PATCH] Added weather data component to display weather data - also added function to display data --- frontend/src/components/WeatherCard.tsx | 39 +++++++++++++++++++------ frontend/src/components/WeatherData.tsx | 11 +++++++ frontend/src/utils/apiFunc.ts | 6 ++-- 3 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 frontend/src/components/WeatherData.tsx diff --git a/frontend/src/components/WeatherCard.tsx b/frontend/src/components/WeatherCard.tsx index 1b39dc1..ba822e4 100644 --- a/frontend/src/components/WeatherCard.tsx +++ b/frontend/src/components/WeatherCard.tsx @@ -3,11 +3,14 @@ import { useState } from "react"; import { fetchWeather } from "../utils/apiFunc"; import Cookies from "js-cookie"; import { toast } from "react-toastify"; +import WeatherData from "./WeatherData"; +import { useEffect } from "react"; const WeatherCard: React.FC = () => { const [city, setCity] = useState(""); const [loading, setLoading] = useState(false); - console.log(loading); + const [weatherData, setWeatherData] = useState(false); + console.log(loading); // only for better reading because a syntax error would appear const getAPIKey = () => Cookies.get("apiKey") || ""; const handleCityChange = (event: React.ChangeEvent) => { @@ -17,16 +20,33 @@ const WeatherCard: React.FC = () => { const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); setLoading(true); - toast.promise(fetchWeather(city, getAPIKey()), { - pending: "Fetching weather data...", - success: "Weather data loaded successfully!", - error: "Error loading weather data! (Check console for details)", - }); - setLoading(false); + toast + .promise(fetchWeather(city, getAPIKey()), { + pending: "Fetching weather data...", + success: "Weather data loaded successfully!", + error: "Error loading weather data! (Check console for details)", + }) + .then(() => { + if (localStorage.getItem("weather")) { + setWeatherData(true); + } else { + setWeatherData(false); + } + setLoading(false); + }); }; + // Check if weather data is already in localStorage - when entering the page via URL/reload + useEffect(() => { + if (localStorage.getItem("weather")) { + setWeatherData(true); + } else { + setWeatherData(false); + } + }, []); + return ( -
+ <>

Weather Card

Current Weather will be displayed here

@@ -42,7 +62,8 @@ const WeatherCard: React.FC = () => { />
-
+ {weatherData && } + ); }; diff --git a/frontend/src/components/WeatherData.tsx b/frontend/src/components/WeatherData.tsx new file mode 100644 index 0000000..81a46d3 --- /dev/null +++ b/frontend/src/components/WeatherData.tsx @@ -0,0 +1,11 @@ +import React from "react"; + +const WeatherData: React.FC = () => { + return ( + <> +

Weather

+ + ); +}; + +export default WeatherData; diff --git a/frontend/src/utils/apiFunc.ts b/frontend/src/utils/apiFunc.ts index 62d3bb4..6d2e1c5 100644 --- a/frontend/src/utils/apiFunc.ts +++ b/frontend/src/utils/apiFunc.ts @@ -4,7 +4,9 @@ export const fetchWeather = async (city: string, apiKey: string) => { `http://api.openweathermap.org/geo/1.0/direct?q=${city}&appid=${apiKey}` ).then((response) => { if (response.status === 401) { - console.error("You are not authorized to access this resource. Please check your API key."); + console.error( + "You are not authorized to access this resource. Please check your API key." + ); } else if (response.ok) { return response.json(); } else { @@ -18,5 +20,5 @@ export const fetchWeather = async (city: string, apiKey: string) => { const weather = await fetch( `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${apiKey}` ).then((response) => response.json()); - console.log(weather); + localStorage.setItem("weather", JSON.stringify(weather)); };