| 240 | // Fetch weather + AQI + soil moisture |
| 241 | useEffect(() => { |
| 242 | const fetchAll = async () => { |
| 243 | setLoading(true); |
| 244 | const { lat, lng } = fieldCenter; |
| 245 | try { |
| 246 | const [weatherRes, aqiRes, soilMoistRes] = await Promise.all([ |
| 247 | fetch(`https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lng}¤t=temperature_2m,relative_humidity_2m,wind_speed_10m,weather_code`), |
| 248 | fetch(`https://air-quality-api.open-meteo.com/v1/air-quality?latitude=${lat}&longitude=${lng}¤t=pm2_5,pm10,european_aqi,us_aqi`), |
| 249 | fetch(`https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lng}&hourly=soil_moisture_0_to_7cm&forecast_days=1`), |
| 250 | ]); |
| 251 | const [wData, aqData, smData] = await Promise.all([weatherRes.json(), aqiRes.json(), soilMoistRes.json()]); |
| 252 | setWeather(wData.current || null); |
| 253 | if (aqData.current) setAqiData(aqData.current); |
| 254 | if (smData.hourly?.soil_moisture_0_to_7cm) { |
| 255 | const vals = smData.hourly.soil_moisture_0_to_7cm.filter((v: any) => v != null); |
| 256 | if (vals.length) setSoilMoisture(Math.round(vals[vals.length - 1] * 1000) / 10); |
| 257 | } |
| 258 | } catch { setWeather(null); } |
| 259 | finally { setLoading(false); } |
| 260 | }; |
| 261 | fetchAll(); |
| 262 | }, [field, fieldCenter]); |
| 263 | |