-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyelpAPI.js
380 lines (286 loc) · 11.2 KB
/
yelpAPI.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Initialize Firebase
var config = {
apiKey: "AIzaSyCLn3kgPLJpalQ9LC4DR2lkQvEohkAIe3k",
authDomain: "blazeit-22443.firebaseapp.com",
databaseURL: "https://blazeit-22443.firebaseio.com",
projectId: "blazeit-22443",
storageBucket: "blazeit-22443.appspot.com",
messagingSenderId: "784716485493"
};
firebase.initializeApp(config);
var database = firebase.database();
//YELP KEY
// Client ID
// mSosLSg7eDfpp54l0C7VhA
// API Key
// 93-sQptypfHJJ-zn2q1fOKSujEsPzhm_gVzq-5g7q_P1G4TIsuM7G126ydE37pmuFcd4o2t-_a8pkiBHZV6Rt1eRkcfzMmOJ5OIFZwsFPvrkjFHdcNEYo1_JBiMAXHYx
$(document).ready(function () {
// Some APIs will give us a cross-origin (CORS) error. This small function is a fix for that error.
jQuery.ajaxPrefilter(function (options) {
if (options.crossDomain && jQuery.support.cors) {
options.url = 'https://cors-anywhere.herokuapp.com/' + options.url;
}
});
var activeCity
jQuery.getJSON(
"https://www.zipcodeapi.com/rest/xurGnDV0QceRvNrjKVIyqzPBYRvMVasNhgr9H5j7lAkKLIl8ys7TsN19XoSD13ID/radius.json/94122/65/mile",
function (zipData) {
var zipDataArray = zipData.zip_codes;
var cityList = convertCityArr(zipDataArray); //originally this line was : var cityList = convertCityArr(response)
var unique = function (value, index, self) {
return self.indexOf(value) === index;
}
var uniqueCities = cityList.filter(unique);
getRandomCity(uniqueCities);
function convertCityArr(array) {
var cities = [];
for (var i = 0; i < array.length; i++) {
var cityName = array[i].city;
cities.push(cityName)
}
return cities;
}
function getRandomCity(uniqueCities) {
var activeCityIndex = uniqueCities[Math.floor(Math.random() * uniqueCities.length)];
activeCity = activeCityIndex;
}
//Display the city selected on the page
$("#city-selected-display").text(activeCity)
var query = "http://api.openweathermap.org/data/2.5/forecast?appid=f54f78656d096d76ff850ad75c4be18e&q=" + activeCity + ",us"
$.get(query)
.then(function (data) {
var kelvin = data.list[0].main.temp;
var fahrenheit = Math.floor(kelvin * 9 / 5 - 459.67);
$("#weather-results").append("<h1>The weather in " + data.city.name + " is " + fahrenheit + "\u{000B0} F</h1>")
})
.catch(function (error) {
console.log(error);
})
//our key for yelp API
var yelpKey = "93-sQptypfHJJ-zn2q1fOKSujEsPzhm_gVzq-5g7q_P1G4TIsuM7G126ydE37pmuFcd4o2t-_a8pkiBHZV6Rt1eRkcfzMmOJ5OIFZwsFPvrkjFHdcNEYo1_JBiMAXHYx";
var where = activeCity;
//search terms
var query = "events"
// sort_by string (options best_match, rating, review_count, OR distance)
var sorted = "rating"
var yelpQueryURL = "https://api.yelp.com/v3/businesses/search?&location="
+ where +
"&term=" + query +
"&sort_by=" + sorted +
"&open_now=true" +
"&attribute=hot_and_new";
$.ajax({
url: yelpQueryURL,
headers: {
'Authorization': "Bearer " + yelpKey,
},
method: "GET",
dataType: 'json',
}).then(function (response) {
console.log(response)
//make the owl carousel
var carousel = $('<div id="owl-demo" class="owl-carousel owl-theme">');
for (var i = 0; i < response.businesses.length; i++) {
//Event ID to track events internally
//Events from this API will be a number starting at 1000. Another API could be 2000+, another 3000+ and so on.
var eventID = (10000 + parseInt([i]))
//Event Title
var title = response.businesses[i].name;
//image
var imageURL = response.businesses[i].image_url;
//event URL
var link = response.businesses[i].url
// rating;
var rating = response.businesses[i].rating
//cost
// TODO
// address - this response is an array, where each element is a 'line' of an address (usually). Let's leave it for now, but later we will need to interpret it. possibly use a for loop to store it on the data div?
var address = [];
address = response.businesses[i].location.display_address
// console.log(address)
//Create a div to store all the data in one place
var dataDiv = $("<data class='data-storage'>");
dataDiv.attr("data-ID", eventID);
dataDiv.attr("data-title", title);
dataDiv.attr("data-image-URL", imageURL);
dataDiv.attr("data-link-URL", link);
dataDiv.attr("data-rating", rating);
// Create a div to display all that sweet data we got
var displayDiv = $('<div class="card-item">');
var displayTitle = $("<div>" + title + "</div>")
displayTitle.addClass("title")
displayDiv.append(displayTitle, "<br>")
var displayImage = $("<img>")
displayImage.attr("src", imageURL)
displayImage.attr("alt", title)
displayImage.addClass("event-image")
displayDiv.append(displayImage);
var displayLink = $("<a>");
displayLink.attr("href", link)
displayLink.text("Link: " + title)
displayDiv.append(displayLink, "<br>")
var displayRating = $("<p>");
displayRating.text("Rated " + rating + " stars on Yelp!")
displayDiv.append(displayRating, "<br>")
displayDiv.append(dataDiv)
//populate the Owl carousel with the display div we created
carousel.append(displayDiv);
//end of activty for loop
}
//owl be back!
$("#carousel-container").html(carousel);
$("#owl-demo").owlCarousel({
responsiveClass:true,
responsive:{
0:{
items:1,
nav:true,
dots: false
},
600:{
items:3,
nav:true,
dots: false
},
1000:{
items:3,
nav:true,
dots: false
}
}
});
// on click of event, display that event has been selected and grab it's data
$(".owl-item").on("click", function () {
// Bypassing the toggle functionality
// $(this).toggleClass('checked');
//check if the activity card has been clicked
if ($(this).hasClass("checked")) {
$(this).removeClass('checked');
// To remove the entry for checkmark unchecked (by ZOE & Lyle)
var query = database.ref();
//grab the event ID
var eventIDremove = $(this).find("data").attr("data-id");
query.once("value", (function () {
database.ref().child(eventIDremove).remove();
}));
var removeItinerary = "#itinerary-" + eventIDremove;
console.log(removeItinerary)
$(removeItinerary).empty()
//end of owl item click "IF" statement
} else {
$(this).addClass('checked');
//grab the info from the checked activity
//title
var titleFB = $(this).find("data").attr("data-title");
// console.log(titleFB)
//image URL
var imageFB = $(this).find("data").attr("data-image-URL");
// console.log(imageFB)
//link to activity
var linkFB = $(this).find("data").attr("data-link-URL");
// console.log(linkFB)
//cost
//rating
var ratingFB = $(this).find("data").attr("data-rating");
// console.log(ratingFB)
//address
// var addressFB $(this).find("a").attr("href")
var eventIDFB = $(this).find("data").attr("data-id");
// console.log(eventIDFB)
database.ref().child(eventIDFB).push({
eventID: eventIDFB,
title: titleFB,
image: imageFB,
link: linkFB,
// cost: costFB,
rating: ratingFB,
// address: addressFB
});
};
//end of click activity
})
//end of ajax response
});
//end of zipcodeAPI
});
//end of document ready
});
//empty the itinerary div
// $("#itinerary-display").empty()
database.ref().on("child_added", function (childSnapshot) {
//mad props to VIVIAN!!
childSnapshot.forEach(function (child) {
var obj = child.val();
// console.log(obj);
// console.log(obj.title);
var fireTitle = obj.title;
var fireImageURL = obj.image;
var fireLink = obj.link;
var fireRating = obj.rating;
var fireID = obj.eventID
var itineraryDiv = $('<div class="itinerary-activity">');
var itineraryID = "itinerary-" + fireID
itineraryDiv.attr("id", itineraryID)
var itineraryTitle = $("<div>" + fireTitle + "</div>")
itineraryTitle.attr("id", "fireTitle")
itineraryDiv.append(fireTitle, "<br>")
var itineraryImage = $("<img>")
itineraryImage.attr("src", fireImageURL)
itineraryImage.attr("alt", fireTitle)
itineraryImage.addClass("itinerary-image")
itineraryDiv.append(itineraryImage);
var itineraryLink = $("<a>");
itineraryLink.attr("href", fireLink)
itineraryLink.text("Link: " + fireTitle)
itineraryDiv.append(itineraryLink)
var itineraryRating = $("<p>");
itineraryRating.text("Rated " + fireRating + " stars on Yelp!")
itineraryRating.attr("data-fire-rating", fireRating)
itineraryDiv.append(itineraryRating, "<br>")
$("#itinerary-display").append(itineraryDiv)
})
// Handle the errors
}, function (errorObject) {
console.log("Errors handled: " + errorObject.code);
});
// Lindsey's testimonials
var name = "";
var review = "";
$(document).on("click", "#submit", function (event) {
event.preventDefault();
// Grabbed values from text boxes
name = $("#fname").val().trim();
review = $("#subject").val().trim();
$("#reviews-display").text(" \" " + review + " \" " + " - " + name);
});
// // Lindsey's FB
// // Initialize Firebase
// var config = {
// apiKey: "AIzaSyAXIOcZoTp4y2t9tYtyiN1JA6HRZoIvT28",
// authDomain: "dayventurebylolz.firebaseapp.com",
// databaseURL: "https://dayventurebylolz.firebaseio.com",
// projectId: "dayventurebylolz",
// storageBucket: "dayventurebylolz.appspot.com",
// messagingSenderId: "858404665634"
// };
// firebase.initializeApp(config);
// var database = firebase.database();
var review = "DAYVENTURE IS AWESOME. FIVE STARS";
var name = "Harry Potter";
database.ref().on("value", function (snapshot) {
if (snapshot.child("review").exists() && snapshot.child("name").exists()) {
review = snapshot.val().review;
name = snapshot.val().name;
}
$("#reviews-display").text(" \" " + review + " \" " + " -" + name);
});
$(document).on("click", "#submit", function (event) {
// event.preventDefault();
// Grabbed values from text boxes
var reviewerName = $("#fname").val().trim();
var reviewerReview = $("#subject").val().trim();
database.ref().set({
name: reviewerName,
review: reviewerReview
});
});