From f0a6ba7bc1dac04a92c35bc352e6b3a9a48ed996 Mon Sep 17 00:00:00 2001 From: Juthatip McDevitt Date: Thu, 21 Mar 2024 23:00:19 -0500 Subject: [PATCH] added uv index component --- .../components/airPollution/AirPollution.tsx | 4 +- weather_app/app/components/uvIndex/UVI.tsx | 58 ++++++++++++++++++- weather_app/app/context/globalContext.js | 17 ++++-- 3 files changed, 71 insertions(+), 8 deletions(-) diff --git a/weather_app/app/components/airPollution/AirPollution.tsx b/weather_app/app/components/airPollution/AirPollution.tsx index 6a7fca0..f81a410 100644 --- a/weather_app/app/components/airPollution/AirPollution.tsx +++ b/weather_app/app/components/airPollution/AirPollution.tsx @@ -12,8 +12,8 @@ const AirPollution = () => { return } const airQualityIndex = airPollution.list[0].main.aqi * 10; - console.log(airQualityIndex) - //filter aqi + //console.log(airQualityIndex) + //filter api const filterAQI = air_QualityIndex.find((item) => { return item.rating === airQualityIndex; }) diff --git a/weather_app/app/components/uvIndex/UVI.tsx b/weather_app/app/components/uvIndex/UVI.tsx index 402258c..886c60e 100644 --- a/weather_app/app/components/uvIndex/UVI.tsx +++ b/weather_app/app/components/uvIndex/UVI.tsx @@ -1,10 +1,64 @@ "use client" +import { useGlobalContext } from '@/app/context/globalContext' +import { Skeleton } from '@/components/ui/skeleton'; +import { Progress } from '@/components/ui/progress'; import React from 'react' +import { TbUvIndex } from "react-icons/tb"; const UVI = () => { + const {uvIndex} = useGlobalContext(); + if(!uvIndex || !uvIndex.daily){ + return + } + const {daily} = uvIndex; + const {uv_index_clearsky_max, uv_index_max} = daily; + const uvIndexMax = uv_index_max[0].toFixed(0); + const uvIndexScale = (uvIndex: number) => { + if(uvIndex <= 2){ + return{ + text: "Low", + desc: "No protection needed" + }; + } + else if(uvIndex <= 5){ + return{ + text: "Moderate", + desc: "Some protection is required" + }; + } + else if(uvIndex <= 7){ + return{ + text: "High", + desc: "Protection essential" + }; + } + else if(uvIndex <= 10){ + return{ + text: "Very High", + desc: "Extra protection is needed" + }; + } + else{ + return{ + text: "Extreme", + desc: "Stay inside" + }; + } + } + const leftProgress = (uvIndexMax / 12) * 100; + + return ( -
- UVI +
+
+

UV Index

+
+

{uvIndexMax}

+ ({uvIndexScale(uvIndexMax).text}) +
+ +
+

{uvIndexScale(uvIndexMax).desc}

) } diff --git a/weather_app/app/context/globalContext.js b/weather_app/app/context/globalContext.js index 7cb92ab..8df2db0 100644 --- a/weather_app/app/context/globalContext.js +++ b/weather_app/app/context/globalContext.js @@ -3,8 +3,6 @@ import axios from "axios"; import React, { createContext, useContext, useEffect, useState } from "react" - - const GlobalContext = createContext(); const GlobalContextUpdate = createContext(); @@ -13,6 +11,7 @@ export const GlobalContextProvider = ({children}) => { const [forecast, setForecast] = useState({}); const [airPollution, setAirPollution] = useState({}); const [dailyForecast, setDailyForecast] = useState({}); + const [uvIndex, setUvIndex] = useState({}); const fetchForecast = async() => { try { @@ -36,7 +35,16 @@ export const GlobalContextProvider = ({children}) => { const res = await axios.get("api/dailyForecast") setDailyForecast(res.data) } catch (error) { - console.log("Error fetching air pollution data:", error.message) + console.log("Error fetching air forcast data:", error.message) + } + }; + const fetchUVIndex = async() => { + try { + const res = await axios.get("api/uvIndex") + setUvIndex(res.data) + + } catch (error) { + console.log("Error fetching air UVI data:", error.message) } } @@ -44,10 +52,11 @@ export const GlobalContextProvider = ({children}) => { fetchForecast(); fetchAirPollution(); fetchDailyForecast(); + fetchUVIndex(); }, []); return( - + {children} )