forked from acorbridg/Project1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
186 lines (160 loc) · 7.49 KB
/
script.js
File metadata and controls
186 lines (160 loc) · 7.49 KB
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
// global variables
var foodButton = document.getElementById("foodButton");
var restaurantDiv = document.getElementById("restaurant-div");
var favButtons = [];
var dropdown = document.querySelector('.dropdown');
var storedFavorites = JSON.parse(localStorage.getItem("storedFavorites")) || [];
var button = document.getElementById("get-location");
// create the necessary elements from the locally stored favorites list and print them to the screen
function renderFavorites() {
$("#favSection").empty();
// display the section title
var favDivTitle = document.createElement("h2");
favDivTitle.classList.add("subtitle");
favDivTitle.textContent = "Favorites";
$("#favSection").append(favDivTitle);
// loop through locally stored favorites list and create elements to display them
console.log(storedFavorites);
for (let i = 0; i < storedFavorites.length; i++) {
// create a container div, a div for the favorite, and a div for the delete button
var favDiv = document.createElement("div");
var favDiv1 = document.createElement("div");
var favDiv2 = document.createElement("div");
favDiv.classList.add("subtitle","is-size-6","columns", "is-mobile" );
favDiv.setAttribute("id", `favOption${i}`);
favDiv1.classList.add("column","is-10");
favDiv2.classList.add("column", "is-2");
// populate the favorite information
var name = document.createElement("a");
var price = document.createElement("p");
var phone = document.createElement("p");
var genre = document.createElement("p");
name.textContent = storedFavorites[i].name;
name.setAttribute("href", storedFavorites[i].link);
name.setAttribute("target", "_blank");
name.style.left = '1.5em';
price.textContent = storedFavorites[i].price;
phone.textContent = storedFavorites[i].phone;
genre.textContent = storedFavorites[i].genre;
// add the delete button to the right
var removeFavButton = document.createElement("button");
removeFavButton.innerHTML = "✕";
removeFavButton.classList.add("button","is-small","is-danger");
removeFavButton.style.fontSize = '5px';
removeFavButton.style.display = 'inline';
removeFavButton.style.top = '1em';
removeFavButton.style.right = '4em';
removeFavButton.setAttribute("id", `removeFavButton${i}`);
removeFavButton.setAttribute("onClick", `removeFavorite(${i})`);
// put everything together and add it to the page to display
favDiv2.append(removeFavButton);
favDiv1.append(name);
favDiv1.append(genre);
favDiv1.append(phone);
favDiv1.append(price);
favDiv.append(favDiv1);
favDiv.append(favDiv2);
$("#favSection").append(favDiv);
};
};
// remove a favorite using the id sent by the button click
function removeFavorite(favID) {
// remove the favorite from the list
storedFavorites.splice(favID, 1);
// update the local storage
localStorage.setItem("storedFavorites", JSON.stringify(storedFavorites));
// update the display
renderFavorites();
};
// add a favorite using the id sent by the button click
function addFavorite(favButtonID) {
var selectedFavButton = document.getElementById(favButtonID);
// grab the information in the restaurant card containing the favorite button that was clicked
var thisOption = selectedFavButton.parentElement;
// create a favorite object with all the pertinent information
var storageObject = {
link: thisOption.children[0].href,
name: thisOption.children[0].textContent,
genre: thisOption.children[1].textContent,
phone: thisOption.children[2].textContent,
price: thisOption.children[3].textContent
};
// add the favorite object to local storage and update the display
storedFavorites.push(storageObject);
localStorage.setItem("storedFavorites", JSON.stringify(storedFavorites));
renderFavorites();
};
// take care of dropdown menu functionality
dropdown.addEventListener('click', function (event) {
event.stopPropagation();
dropdown.classList.toggle('is-active');
});
// handle user selection of a food category from the dropdown menu
function foodOptions() {
var cat = $(this).attr("id");
$("#restaurant-div").empty();
// get the user position from their device gps
navigator.geolocation.getCurrentPosition(function (position) {
var lat = position.coords.latitude;
var long = position.coords.longitude;
// yelp url uses the herokuapp to generate temporary access through "cors" security, goes through the category search functionality of the API,
// and uses the selected category and latitude and longtitude to pull up 6 relevant results in a 5 mile radius that are open at the time of search
var yelp = "https://bootcamp-cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/search?categories=" + cat + "&limit=6&open_now=true&latitude=" + lat + "&longitude=" + long + "&radius=3220";
// make a call to the yelp api using an api key and a header that yelp requires
$.ajax({
url: yelp,
headers: {
"Authorization": 'Bearer JK2o6xaFthzRfO--_lKdin6AtHopMHSKQogItiUUqiuKs6cv5S9fl4gvHEt0mqDPLLDHDekwyNM5HeI9Oc82S6EiUSSY9wszqG8nYpX13JSTHYGpbVF_qi-veRjaYnYx',
"accept": "application/json",
"x-requested-with": "xmlhttprequest",
"Access-Control-Allow-Origin": "*",
}
}).then(function (data) {
// create a card for each restaurant result
for (let i = 0; i < data.businesses.length; i++) {
// add a div to hold the result and set bulma classes to make it a card
var optionDiv = document.createElement("div");
optionDiv.classList.add("subtitle","card", "is-size-6");
optionDiv.setAttribute("id", `option${i}`);
// add div for content of card
var cardContent = document.createElement("div");
cardContent.classList.add("card-content");
var name = document.createElement("a");
var price = document.createElement("p");
var phone = document.createElement("p");
var genre = document.createElement("p");
var favButton = document.createElement("button");
// add the card content based on the info retrieved from the API
name.textContent = data.businesses[i].name;
name.setAttribute("href", data.businesses[i].url);
name.setAttribute("target", "_blank");
name.classList.add("content","button-result");
price.textContent = data.businesses[i].price;
price.classList.add("content");
phone.textContent = data.businesses[i].display_phone;
phone.classList.add("content");
genre.textContent = data.businesses[i].categories[0].title;
genre.classList.add("content");
// add the button to save this restaurant to local storage as a favorite
favButton.textContent = "Add to Favorites";
favButton.classList.add("button", "is-warning", "is-small", "is-size-6");
favButton.setAttribute("id", `favButton${i}`);
// set up the favorite button to run the "addFavorites" button with the id as the argument
favButton.setAttribute('onClick', 'addFavorite(this.id)');
// put the card content together
cardContent.append(name);
cardContent.append(genre);
cardContent.append(phone);
cardContent.append(price);
cardContent.append(favButton);
// add the content to the card
optionDiv.append(cardContent);
// add the card to the restaurant div of the page
restaurantDiv.append(optionDiv);
};
});
});
renderFavorites();
};
$(".dropdown-menu").on("click", "button", foodOptions);
renderFavorites();