-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD] Fetch weather Data using Navigator Geolocation
- Loading branch information
Showing
10 changed files
with
244 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
**/.vscode/ | ||
**/node_modules/ | ||
**/npm-debug.log* | ||
server/api/.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,15 @@ | ||
import { useState } from 'react' | ||
import reactLogo from './assets/react.svg' | ||
import viteLogo from '/vite.svg' | ||
import './App.css' | ||
// src/App.jsx | ||
// eslint-disable-next-line no-unused-vars | ||
import React from 'react'; | ||
import LocationComponent from './LocationComponent'; // Import the LocationComponent | ||
|
||
function App() { | ||
const [count, setCount] = useState(0) | ||
|
||
return ( | ||
<> | ||
<div> | ||
<a href="https://vitejs.dev" target="_blank"> | ||
<img src={viteLogo} className="logo" alt="Vite logo" /> | ||
</a> | ||
<a href="https://react.dev" target="_blank"> | ||
<img src={reactLogo} className="logo react" alt="React logo" /> | ||
</a> | ||
</div> | ||
<h1>Vite + React</h1> | ||
<div className="card"> | ||
<button onClick={() => setCount((count) => count + 1)}> | ||
count is {count} | ||
</button> | ||
<p> | ||
Edit <code>src/App.jsx</code> and save to test HMR | ||
</p> | ||
</div> | ||
<p className="read-the-docs"> | ||
Click on the Vite and React logos to learn more | ||
</p> | ||
</> | ||
) | ||
return ( | ||
<div className="App"> | ||
<h1>Welcome to CliNema</h1> | ||
<LocationComponent /> {/* Render the LocationComponent */} | ||
</div> | ||
); | ||
} | ||
|
||
export default App | ||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useEffect, useState } from 'react'; | ||
import axios from 'axios'; | ||
|
||
const LocationComponent = () => { | ||
// eslint-disable-next-line no-unused-vars | ||
const [latitude, setLatitude] = useState(null); | ||
// eslint-disable-next-line no-unused-vars | ||
const [longitude, setLongitude] = useState(null); | ||
const [weatherData, setWeatherData] = useState(null); | ||
const [error, setError] = useState(null); | ||
|
||
useEffect(() => { | ||
// Check if Geolocation API is available | ||
if (navigator.geolocation) { | ||
navigator.geolocation.getCurrentPosition( | ||
(position) => { | ||
const { latitude, longitude } = position.coords; | ||
setLatitude(latitude); | ||
setLongitude(longitude); | ||
console.log(`Latitude: ${latitude}, Longitude: ${longitude}`); | ||
|
||
// Send latitude and longitude to the Flask backend | ||
axios.get(`http://localhost:5000/weather?lat=${latitude}&lon=${longitude}`) | ||
.then(response => { | ||
setWeatherData(response.data); // Set weather data in state | ||
console.log("Weather data:", response.data); // Log the response data | ||
}) | ||
.catch(err => { | ||
console.error("Error fetching weather data:", err); | ||
setError("Error fetching weather data"); | ||
}); | ||
}, | ||
(error) => { | ||
console.error("Error getting location:", error); | ||
setError("Error getting location"); | ||
} | ||
); | ||
} else { | ||
console.log("Geolocation is not supported by this browser."); | ||
setError("Geolocation is not supported by this browser."); | ||
} | ||
}, []); // The empty array ensures this runs once when the component mounts | ||
|
||
return ( | ||
<div> | ||
<h2>Your Location Weather</h2> | ||
{weatherData ? ( | ||
<div> | ||
<p>Temperature: {weatherData.main.temp}°C</p> | ||
<p>Weather: {weatherData.weather[0]?.description}</p> | ||
<p>Location: {weatherData.name}</p> | ||
</div> | ||
) : ( | ||
<p>{error ? error : "Fetching weather..."}</p> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default LocationComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
import { defineConfig } from 'vite' | ||
import react from '@vitejs/plugin-react' | ||
// vite.config.js | ||
import { defineConfig } from 'vite'; | ||
import react from '@vitejs/plugin-react'; | ||
|
||
// https://vitejs.dev/config/ | ||
export default defineConfig({ | ||
plugins: [react()], | ||
}) | ||
server: { | ||
proxy: { | ||
'/weather': { | ||
target: 'http://localhost:5000', // Your Flask server URL | ||
changeOrigin: true, | ||
rewrite: (path) => path.replace(/^\/weather/, '/weather'), | ||
}, | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
MOVIE_API_HOST=0.0.0.0 | ||
MOVIE_API_PORT=5000 | ||
OPENWEATHER_API_KEY=9291dbdcabf7c26431129cb4438c30e1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.