updated globalContext
This commit is contained in:
parent
b1a34bfb6b
commit
84bb179392
1 changed files with 58 additions and 0 deletions
58
weather_app/app/context/globalContext.js
Normal file
58
weather_app/app/context/globalContext.js
Normal file
|
@ -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(
|
||||
<GlobalContext.Provider value={{forecast, airPollution, dailyForecast}}>
|
||||
<GlobalContextUpdate.Provider>{children}</GlobalContextUpdate.Provider>
|
||||
</GlobalContext.Provider>
|
||||
)
|
||||
};
|
||||
|
||||
|
||||
export const useGlobalContext = () => useContext(GlobalContext);
|
||||
export const useGlobalContextUpdate = () => useContext(GlobalContextUpdate);
|
Loading…
Add table
Reference in a new issue