diff --git a/src/components/NewsArticleWidget.tsx b/src/components/NewsArticleWidget.tsx index eca6940..44e7290 100644 --- a/src/components/NewsArticleWidget.tsx +++ b/src/components/NewsArticleWidget.tsx @@ -27,7 +27,7 @@ const NewsArticleWidget = () => { }, []); return ( -
+
{loading ? ( <>
@@ -43,22 +43,24 @@ const NewsArticleWidget = () => { ) : article ? ( - {article.title} -
-
+
+ {article.title} +
+
+
{article.title}
-

+

{article.content}

diff --git a/src/components/WeatherWidget.tsx b/src/components/WeatherWidget.tsx index 8eaf871..5703ab2 100644 --- a/src/components/WeatherWidget.tsx +++ b/src/components/WeatherWidget.tsx @@ -15,61 +15,61 @@ const WeatherWidget = () => { const [loading, setLoading] = useState(true); - useEffect(() => { - const getApproxLocation = async () => { - const res = await fetch('https://ipwhois.app/json/'); - const data = await res.json(); + const getApproxLocation = async () => { + const res = await fetch('https://ipwhois.app/json/'); + const data = await res.json(); - return { - latitude: data.latitude, - longitude: data.longitude, - city: data.city, - }; + return { + latitude: data.latitude, + longitude: data.longitude, + city: data.city, }; + }; - const getLocation = async ( - callback: (location: { - latitude: number; - longitude: number; - city: string; - }) => void, - ) => { - if (navigator.geolocation) { - const result = await navigator.permissions.query({ - name: 'geolocation', - }); + const getLocation = async ( + callback: (location: { + latitude: number; + longitude: number; + city: string; + }) => void, + ) => { + if (navigator.geolocation) { + const result = await navigator.permissions.query({ + name: 'geolocation', + }); - if (result.state === 'granted') { - navigator.geolocation.getCurrentPosition(async (position) => { - const res = await fetch( - `https://api-bdc.io/data/reverse-geocode-client?latitude=${position.coords.latitude}&longitude=${position.coords.longitude}&localityLanguage=en`, - { - method: 'GET', - headers: { - 'Content-Type': 'application/json', - }, + if (result.state === 'granted') { + navigator.geolocation.getCurrentPosition(async (position) => { + const res = await fetch( + `https://api-bdc.io/data/reverse-geocode-client?latitude=${position.coords.latitude}&longitude=${position.coords.longitude}&localityLanguage=en`, + { + method: 'GET', + headers: { + 'Content-Type': 'application/json', }, - ); + }, + ); - const data = await res.json(); + const data = await res.json(); - callback({ - latitude: position.coords.latitude, - longitude: position.coords.longitude, - city: data.locality, - }); + callback({ + latitude: position.coords.latitude, + longitude: position.coords.longitude, + city: data.locality, }); - } else if (result.state === 'prompt') { - callback(await getApproxLocation()); - navigator.geolocation.getCurrentPosition((position) => {}); - } else if (result.state === 'denied') { - callback(await getApproxLocation()); - } - } else { + }); + } else if (result.state === 'prompt') { + callback(await getApproxLocation()); + navigator.geolocation.getCurrentPosition((position) => {}); + } else if (result.state === 'denied') { callback(await getApproxLocation()); } - }; + } else { + callback(await getApproxLocation()); + } + }; + const updateWeather = async () => { getLocation(async (location) => { const res = await fetch(`/api/weather`, { method: 'POST', @@ -100,10 +100,16 @@ const WeatherWidget = () => { }); setLoading(false); }); + }; + + useEffect(() => { + updateWeather(); + const intervalId = setInterval(updateWeather, 2 * 60 * 1000); + return () => clearInterval(intervalId); }, []); return ( -
+
{loading ? ( <>
@@ -134,22 +140,24 @@ const WeatherWidget = () => { {data.temperature}°{data.temperatureUnit}
-
+
- + {data.location} - + {data.windSpeed} {data.windSpeedUnit}
- + {data.condition} -
- Humidity: {data.humidity}% - Now +
+ Humidity {data.humidity}% + + Now +