Skip to content

Commit

Permalink
api call to get current location and related info now happens in the …
Browse files Browse the repository at this point in the history
…app component and is going to be passed down as props. Api call is working and the changes have not broken the app.
  • Loading branch information
VanceMcGrady committed Aug 18, 2022
1 parent 223472d commit d5b1165
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
49 changes: 47 additions & 2 deletions client/components/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Routes>
<Route path="/" element={
<main>
<Navbar user={user} setUser={setUser} />
<LocalWeather />
<Searchbar user={user} setUser={setUser} />
<LocalWeather localInfo = {localInfo}/>
<Searchbar localInfo = {localInfo} user={user} setUser={setUser} />
</main>
} />
<Route path="/signup" element={<Signup user={user} setUser={setUser} />} />
Expand Down
8 changes: 4 additions & 4 deletions client/components/LocalWeather.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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')}
Expand Down
11 changes: 11 additions & 0 deletions client/components/Searchbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit d5b1165

Please sign in to comment.