Skip to content

Commit

Permalink
Take care of weather api problem
Browse files Browse the repository at this point in the history
  • Loading branch information
MoriaHamami committed Aug 1, 2024
1 parent d05f186 commit 7375f2b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
1 change: 0 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ app.use("/tickets", require("./routes/tickets")); // Tickets page
app.use("/manager", require("./routes/manager")); // Manager page
app.use("/clients", require("./routes/clients")); // Clients page
app.use("/cart", require("./routes/cart")); // Cart page
app.use("/", require("./routes/home")); // Home page (again)

// Start server
const port = process.env.PORT || 8084; // Set port
Expand Down
8 changes: 7 additions & 1 deletion controllers/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ function getHomePage(req, res) {
res.render('home.ejs', {})
}

function getWeatherApiKey(req, res) {
const key = process.env.WEATHER_API
res.json(key)
}

// Exports function
module.exports = {
getHomePage
getHomePage,
getWeatherApiKey
}
55 changes: 38 additions & 17 deletions public/js/home.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
const weatherApiKey = '54e5fa763dd39a887729e98f2ca75202'; // API key for OpenWeatherMap
const city = 'Madrid'; // The city to display weather for
const weatherApiUrl = `https://api.openweathermap.org/data/2.5/forecast?q=${city}&appid=${weatherApiKey}&units=metric`;
let city = 'Madrid'; // The city to display weather for
let weatherApiKey = ''; // API key for OpenWeatherMap
let weatherApiUrl = '';

// Fetch the weather data when the document is ready
$(document).ready(async function() {
const res = await getWeatherApi()
if(res){
$.ajax({
url: weatherApiUrl,
method: 'GET',
dataType: 'json',
success: function(data) {
displayWeather(data); // Display the weather data on success
},
error: function(error) {
console.log('An error occurred while fetching data from the OpenWeatherMap API:', error);
}
});
}
});

async function getWeatherApi(){
try{
const key = await $.ajax({
url: '/weatherAPI',
method: 'GET',
contentType: 'application/json'
})
weatherApiKey = key
weatherApiUrl = `https://api.openweathermap.org/data/2.5/forecast?q=${city}&appid=${weatherApiKey}&units=metric`
return true
} catch(error){
console.error('Error:', error);
return false
}
}

function getShopPage() {
window.location.assign('/products'); // Redirect to the products page
Expand Down Expand Up @@ -41,17 +75,4 @@ function findWeatherForDate(weatherList, date) {
return weatherList.find(weather => weather.dt_txt.includes(targetDate)); // Find the weather data for the specific date
}

// Fetch the weather data when the document is ready
$(document).ready(function() {
$.ajax({
url: weatherApiUrl,
method: 'GET',
dataType: 'json',
success: function(data) {
displayWeather(data); // Display the weather data on success
},
error: function(error) {
console.log('An error occurred while fetching data from the OpenWeatherMap API:', error);
}
});
});

2 changes: 2 additions & 0 deletions routes/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const homeController = require("../controllers/home")
// Define the route for the home page
router.route('/')
.get(homeController.getHomePage) // GET requests getHomePage from the home controller
router.route('/weatherAPI')
.get(homeController.getWeatherApiKey)

// Export the router
module.exports = router

0 comments on commit 7375f2b

Please sign in to comment.