Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created November 23, 2020 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/c6c0e2f3729642e7a25b42ba3fa4d0a3 to your computer and use it in GitHub Desktop.
Save codecademydev/c6c0e2f3729642e7a25b42ba3fa4d0a3 to your computer and use it in GitHub Desktop.
Codecademy export
const createVenueHTML = (name, location, iconSource) => {
return `<h2>${name}</h2>
<img class="venueimage" src="${iconSource}"/>
<h3>Address:</h3>
<p>${location.address}</p>
<p>${location.city}</p>
<p>${location.country}</p>`;
}
const createWeatherHTML = (currentDay) => {
console.log(currentDay)
return `<h2>${weekDays[(new Date()).getDay()]}</h2>
<h2>Temperature: ${kelvinToFahrenheit(currentDay.main.temp)}&deg;F</h2>
<h2>Condition: ${currentDay.weather[0].description}</h2>
<img src="https://openweathermap.org/img/wn/${currentDay.weather[0].icon}@2x.png">`;
}
const kelvinToFahrenheit = k => ((k - 273.15) * 9 / 5 + 32).toFixed(0);
// Foursquare API Info
const clientId = 'UEWC1RQGNBSTEFOOTTG0FE04T2WWVSDSXOM0V1L1JFSZVHIN';
const clientSecret = '2SQHEONBDN5FW0JSAPDNCX12UTQFQQ5PMCOSK5ODAFQPWWT5';
const url = 'https://api.foursquare.com/v2/venues/explore?near=';
// OpenWeather Info
const openWeatherKey = 'be51f0c8aea8f26d3785a79f811c2b58';
const weatherUrl = 'https://api.openweathermap.org/data/2.5/weather';
// Page Elements
const $input = $('#city');
const $submit = $('#button');
const $destination = $('#destination');
const $container = $('.container');
const $venueDivs = [$("#venue1"), $("#venue2"), $("#venue3"), $("#venue4")];
const $weatherDiv = $("#weather1");
const weekDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
// Add AJAX functions here:
const getVenues = async() => {
const city = $input.val();
const urlToFetch = url+city+'&limit=10&client_id='+clientId+'&client_secret='+clientsSecret+'&v=20201123'
try{
const response = await fetch(urlToFetch);
if(response.ok){
const jsonResponse = await response.json();
const venues = jsonResponse.response.groups[0].items.map(item => item.venue);
console.log(venues);
return venues;
}
}
catch(error) {
console.log(error);
}
};
const getForecast = async() => {
const urlToFetch=weatherURL+ '?&q=' + $input.val()+'&APPID='+openWeatherKey;
const response = await fetch(urlToFetch);
if (response.ok){
const jsonResponse = await response.json();
return jsonResponse;
}
catch(error) {
console.log(error);
}
}
// Render functions
const renderVenues = (venues) => {
$venueDivs.forEach(($venue, index) => {
// Add your code here:
const venue = venues[index];
const venueIcon = venue.categories[0].icon;
const venueImgSrc = `${venueIcon.prefix}bg_64${venueIcon.suffix}`;
let venueContent = createVenueHTML(venue.name, venue.location, venueImgSrc);
$venue.append(venueContent);
});
$destination.append(`<h2>${venues[0].location.city}</h2>`);
};
const renderForecast = (day) => {
// Add your code here:
const weatherContent = createWeatherHTML(day);
$weatherDiv.append(weatherContent);
//const weatherContent = $weatherDiv.append(day); }
};
const executeSearch = () => {
$venueDivs.forEach(venue => venue.empty());
$weatherDiv.empty();
$destination.empty();
$container.css("visibility", "visible");
getVenues().then(venues=>renderVenues(venues);
getForecast().then(forecast=>renderForecast(forecast));
return false;
};
$submit.click(executeSearch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment