From 84bb179392ffcc26e4e741b6fced6e5d51491d96 Mon Sep 17 00:00:00 2001 From: Juthatip McDevitt Date: Thu, 21 Mar 2024 21:34:03 -0500 Subject: [PATCH] updated globalContext --- weather_app/app/context/globalContext.js | 58 ++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 weather_app/app/context/globalContext.js diff --git a/weather_app/app/context/globalContext.js b/weather_app/app/context/globalContext.js new file mode 100644 index 0000000..7cb92ab --- /dev/null +++ b/weather_app/app/context/globalContext.js @@ -0,0 +1,58 @@ +"use Client" +import axios from "axios"; +import React, { createContext, useContext, useEffect, useState } from "react" + + + + +const GlobalContext = createContext(); +const GlobalContextUpdate = createContext(); + +export const GlobalContextProvider = ({children}) => { + const [state, setState] = useState(); + const [forecast, setForecast] = useState({}); + const [airPollution, setAirPollution] = useState({}); + const [dailyForecast, setDailyForecast] = useState({}); + + const fetchForecast = async() => { + try { + const res = await axios.get("api/weather") + setForecast(res.data) + + } catch (error) { + console.log("Error fetching forecast data:", error.message) + } + }; + const fetchAirPollution = async() => { + try { + const res = await axios.get("api/pollution") + setAirPollution(res.data) + } catch (error) { + console.log("Error fetching air pollution data:", error.message) + } + }; + const fetchDailyForecast = async() => { + try { + const res = await axios.get("api/dailyForecast") + setDailyForecast(res.data) + } catch (error) { + console.log("Error fetching air pollution data:", error.message) + } + } + + useEffect(() => { + fetchForecast(); + fetchAirPollution(); + fetchDailyForecast(); + }, []); + + return( + + {children} + + ) +}; + + +export const useGlobalContext = () => useContext(GlobalContext); +export const useGlobalContextUpdate = () => useContext(GlobalContextUpdate);