forked from Technigo/project-food-turquoise-a
-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
146 lines (121 loc) · 5.75 KB
/
script.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
// You should now have data from at least 10 restaurants from the /search endpoint on your page, and include the following data:
// The restaurant name
// The average cost for a dinner there
// The address of the restaurant
// An image (you choose which image you'd like to display from the response)
// Either the aggregate_rating or the rating_text for that restaurant
const apiKey = "11530b7a501b483bed8062f8defd1254";
const cityId = 257; //Rome
const cuisineId = 25; //Chinese
const resultQuantity = 20;
const theRestaurantSection = document.getElementById("restaurant-section")
const url = `https://developers.zomato.com/api/v2.1/search?entity_id=${cityId}&entity_type=city&count=${resultQuantity}&cuisines=${cuisineId}`
const urlSortOnPrice = `https://developers.zomato.com/api/v2.1/search?entity_id=${cityId}&entity_type=city&count=${resultQuantity}&cuisines=${cuisineId}&sort=cost&order=asc`
const urlSortOnRating = `https://developers.zomato.com/api/v2.1/search?entity_id=${cityId}&entity_type=city&count=${resultQuantity}&cuisines=${cuisineId}&sort=rating&order=desc`
// SCROLL HEADER SECTION
window.onscroll = function () { scrollFunction() };
const header = document.getElementById("scrollHeader");
const sticky = header.offsetTop;
function scrollFunction() {
if (window.pageYOffset > sticky) {
header.classList.add("sticky");
} else {
header.classList.remove("sticky");
}
}
renderImage = (restaurant) => {
if (restaurant.photos && restaurant.photos.length > 0) {
return `<img src=${restaurant.photos[0].photo.url} width="400px">`
} else {
return `<img src="noimage.png" height=250px>`
}
}
rangeToDollar = (restaurant) => {
if (restaurant.price_range === 1 || restaurant.price_range === 2) {
return "$"
} else if (restaurant.price_range === 3 || restaurant.price_range === 4) {
return "$$"
} else if (restaurant.price_range === 5) {
return "$$$"
}
}
//filter if the restaurant has online delivery.
//Make it so your users can choose to only show resturants
//which have delivery (has_online_delivery) or can be booked in advance (has_table_booking).
showDeliveryRestaurants = (restaurant) => {
if (restaurant.has_online_delivery > 0) {
return "Has"
} else {
return "No"
}
}
showBookingRestaurants = (restaurant) => {
if (restaurant.has_table_booking > 0) {
return "Has"
} else {
return "No"
}
}
fetch(url, { headers: { "user-key": apiKey } })
.then(response => response.json())
.then(json => {
console.log(json);
json.restaurants.forEach(chineseRestaurant => {
theRestaurantSection.innerHTML += `<div class="card">
<li>${renderImage(chineseRestaurant.restaurant)}</li>
<li>Name: ${chineseRestaurant.restaurant.name}</li>
<li>Average cost for two: ${chineseRestaurant.restaurant.average_cost_for_two} ${chineseRestaurant.restaurant.currency}</li>
<li>Address: ${chineseRestaurant.restaurant.location.address}</li>
<li>Rating: ${chineseRestaurant.restaurant.user_rating.aggregate_rating}/5 "${chineseRestaurant.restaurant.user_rating.rating_text}"</li>
<li> Average price-range: ${rangeToDollar(chineseRestaurant.restaurant)}</li>
<li> ${showDeliveryRestaurants(chineseRestaurant.restaurant)} online delivery</li>
<li> ${showBookingRestaurants(chineseRestaurant.restaurant)} table booking</li></div>
`
})
})
fetch(urlSortOnPrice, { headers: { "user-key": apiKey } })
.then(response => response.json())
.then(json => {
document.getElementById("sortOnPrice").addEventListener('click', () => {
theRestaurantSection.innerHTML = ''
json.restaurants.forEach(chineseRestaurant => {
theRestaurantSection.innerHTML += `
<div class="card">
<li> ${renderImage(chineseRestaurant.restaurant)}</li>
<li>Name: ${chineseRestaurant.restaurant.name}</li>
<li>Average cost for two: ${chineseRestaurant.restaurant.average_cost_for_two} ${chineseRestaurant.restaurant.currency}</li>
<li>Address: ${chineseRestaurant.restaurant.location.address}</li>
<li>Rating: ${chineseRestaurant.restaurant.user_rating.aggregate_rating}/5 "${chineseRestaurant.restaurant.user_rating.rating_text}"</li>
<li> Average price-range: ${rangeToDollar(chineseRestaurant.restaurant)}</li>
<li> ${showDeliveryRestaurants(chineseRestaurant.restaurant)} online delivery</li>
<li> ${showBookingRestaurants(chineseRestaurant.restaurant)} table booking</li>
</div>`
})
})
})
fetch(urlSortOnRating, { headers: { "user-key": apiKey } })
.then(response => response.json())
.then(json => {
document.getElementById("sortOnRating").addEventListener('click', () => {
theRestaurantSection.innerHTML = ''
json.restaurants.forEach(chineseRestaurant => {
theRestaurantSection.innerHTML += `
<div class="card">
<li> ${renderImage(chineseRestaurant.restaurant)}</li>
<li>Name: ${chineseRestaurant.restaurant.name}</li>
<li>Average cost for two: ${chineseRestaurant.restaurant.average_cost_for_two} ${chineseRestaurant.restaurant.currency}</li>
<li>Address: ${chineseRestaurant.restaurant.location.address}</li>
<li>Rating: ${chineseRestaurant.restaurant.user_rating.aggregate_rating}/5 "${chineseRestaurant.restaurant.user_rating.rating_text}"</li>
<li> Average price-range: ${rangeToDollar(chineseRestaurant.restaurant)}</li>
<li> ${showDeliveryRestaurants(chineseRestaurant.restaurant)} online delivery</li>
<li> ${showBookingRestaurants(chineseRestaurant.restaurant)} table booking</li>
</div>
`
})
})
})
//FUNCTION TO REFRESH THE PAGE
const refresh = () => {
const reload = document.getElementById("reload")
reload = location = location
}