From d5b116567a8f2d011510cb74eeee6f0697e9a05f Mon Sep 17 00:00:00 2001 From: Vance M Mcgrady <68658084+VanceMcGrady@users.noreply.github.com> Date: Wed, 17 Aug 2022 19:31:06 -0700 Subject: [PATCH] api call to get current location and related info now happens in the app component and is going to be passed down as props. Api call is working and the changes have not broken the app. --- client/components/App.jsx | 49 ++++++++++++++++++++++++++++-- client/components/LocalWeather.jsx | 8 ++--- client/components/Searchbar.jsx | 11 +++++++ 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/client/components/App.jsx b/client/components/App.jsx index 1979bcd..d176f11 100644 --- a/client/components/App.jsx +++ b/client/components/App.jsx @@ -13,14 +13,59 @@ import { Routes, Route } from "react-router-dom"; const App = props => { const [user, setUser] = useState(null); + const [localInfo, setLocalInfo] = useState({}); + + useEffect(()=>{ + + + const currentLocationInfo = (() => { + + + let api = "https://api.openweathermap.org/data/2.5/weather"; + let apiKey = "3865d44fbc4bfd45572787afb8fa06b2"; + + navigator.geolocation.getCurrentPosition(success, error); + + function success(position){ + let latitude = position.coords.latitude; + let longitude = position.coords.longitude; + let url = `${api}?lat=${latitude}&lon=${longitude}&appid=${apiKey}&units=imperial`; + + let localInfo = {} + + fetch(url) + .then(res => res.json()) + .then(data => { + localInfo.temp = data.main.temp; + localInfo.currentLocation = data.name + localInfo.weatherDescription = data.weather[0].main; + localInfo.latitude = latitude + localInfo.longitude = longitude + + + setLocalInfo(localInfo) + }) + }; + + function error() { + console.log('error finding location'); + } + })(); + + + }, []) + + + + return ( - - + + } /> } /> diff --git a/client/components/LocalWeather.jsx b/client/components/LocalWeather.jsx index 481e901..1192f39 100644 --- a/client/components/LocalWeather.jsx +++ b/client/components/LocalWeather.jsx @@ -37,10 +37,10 @@ const getWeather = () => { description.innerHTML = data.weather[0].main; }) - // console.log('position ', position); - // console.log('latitude: ', latitude); - // console.log('longitude: ', longitude); - // console.log('url: ', url) + console.log('position ', position); + console.log('latitude: ', latitude); + console.log('longitude: ', longitude); + }; function error(){console.log('error finding location')} diff --git a/client/components/Searchbar.jsx b/client/components/Searchbar.jsx index e91a6d3..b7571a1 100644 --- a/client/components/Searchbar.jsx +++ b/client/components/Searchbar.jsx @@ -16,6 +16,17 @@ const Searchbar = props => { //like componentDidMount triggers on change useEffect(() => { + + const getCurrentLocation = () =>{ + + + navigator.geolocation.getCurrentPosition(success, error) + + + } + + + axios({ method: 'POST', url: 'http://localhost:3000/search',