-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
85 lines (76 loc) · 2.43 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Foursquare API Info
const clientId = "C1NCEBSJDKPW2EXAG4152XQPHSDCLSWEIUBCRF3CJUYP1PNH";
const clientSecret = "VZEBW1BGWX4W5ZCRHHHY5YNWK3UPD0WN255F2CP4P2HDTRWE";
const url = "https://api.foursquare.com/v2/venues/explore?near=";
// OpenWeather Info
const openWeatherKey = "f1d89467089089975b11ef861fb9386b";
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=${clientSecret}&v=20200525`;
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 () => {
try{
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) => {
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) => {
const weatherContent = createWeatherHTML(day);
$weatherDiv.append(weatherContent);
};
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);