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);