-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_ajaxSearch.js
157 lines (121 loc) · 5.18 KB
/
script_ajaxSearch.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
const searchRestaurant = document.querySelector('#searchrestaurant')
// Cria o div com o restaurante quando o custumer procura por um restaurant
function createRestaurantDiv(rest){
const restaurantDiv = document.createElement('div')
restaurantDiv.classList.add("restaurantBox")
//section com o link e a imagem dentro
const insideSection = document.createElement('section')
insideSection.classList.add("aspect-ratio-box")
const restaurantLink = document.createElement('a')
const restaurantImg = document.createElement('img')
restaurantImg.src = 'images/restaurants/originals/' + rest.id + ".jpg"
restaurantImg.style.width = '8.17em'
restaurantImg.style.height = '7em'
restaurantLink.href = 'restaurantPage.php?id=' + rest.id
const restaurantName = document.createElement('p')
restaurantName.textContent = rest.name
restaurantLink.appendChild(restaurantImg)
restaurantDiv.appendChild(restaurantLink)
restaurantDiv.appendChild(restaurantName)
return restaurantDiv
}
// Cria o div com o restaurante quando o custumer procura por um prato
function createRestaurantDishDiv(dish){
const restaurantDiv = document.createElement('div')
restaurantDiv.classList.add("restaurantBox")
//section com o link e a imagem dentro
const insideSection = document.createElement('section')
insideSection.classList.add("aspect-ratio-box")
const restaurantLink = document.createElement('a')
const restaurantImg = document.createElement('img')
restaurantImg.src = 'images/restaurants/originals/' + dish.restaurant + ".jpg"
restaurantImg.style.width = '8.17em'
restaurantImg.style.height = '7em'
restaurantLink.href = 'restaurantPage.php?id=' + dish.restaurant
const restaurantName = document.createElement('p')
restaurantName.textContent = dish.restaurantName
restaurantLink.appendChild(restaurantImg)
restaurantDiv.appendChild(restaurantLink)
restaurantDiv.appendChild(restaurantName)
return restaurantDiv
}
function getRestaurantAPI(){
searchRestaurant.addEventListener('input', async function() {
const section = document.querySelector('#mainBody')
const sectionCategory = document.querySelectorAll('#mainBody > section')
const categoryName = document.querySelectorAll('#mainBody > .categoryName')
const restaurantCategory = document.createElement('section')
restaurantCategory.classList.add("category")
const restaurantSection = document.createElement('section')
restaurantSection.classList.add("restaurants")
// Limpar o body todo quando se começa a pesquisa
//============
const restaurantResponse = await fetch('api_restaurants.php?search=' + this.value)
const restaurants = await restaurantResponse.json()
console.log(restaurants)
for (cat of categoryName){
cat.innerHTML = ''
}
for (sect of sectionCategory){
sect.innerHTML = ''
}
for (const rest of restaurants) {
const restaurantDiv = createRestaurantDiv(rest)
restaurantSection.appendChild(restaurantDiv)
restaurantCategory.appendChild(restaurantSection)
section.appendChild(restaurantSection)
}
if (searchRestaurant.value.length == 0){
location.reload();
}
})
}
function getDishAPI(){
searchRestaurant.addEventListener('input', async function() {
const section = document.querySelector('#mainBody')
const sectionCategory = document.querySelectorAll('#mainBody > section')
const categoryName = document.querySelectorAll('#mainBody > .categoryName')
const restaurantCategory = document.createElement('section')
restaurantCategory.classList.add("category")
const restaurantSection = document.createElement('section')
restaurantSection.classList.add("restaurants")
// Limpar o body todo quando se começa a pesquisa
//============
const dishResponse = await fetch('api_dishes.php?search=' + this.value)
const dishes = await dishResponse.json()
for (cat of categoryName){
cat.innerHTML = ''
}
for (sect of sectionCategory){
sect.innerHTML = ''
}
for (const dish of dishes){
const restaurantDishDiv = createRestaurantDishDiv(dish)
restaurantSection.appendChild(restaurantDishDiv)
restaurantCategory.appendChild(restaurantSection)
section.appendChild(restaurantSection)
}
if (searchRestaurant.value.length == 0){
location.reload();
}
})
}
function searchLogic(){
const selectBox = document.querySelector('[name="searchOptions"]')
const footer = document.querySelector("#mainBody > main > footer > p")//TODO fazer com que o footer
footer.remove() //nao desapareça quando se faz search na home
selectBox.addEventListener('change', function(e){
// Verifica qual é o input selecionado
const selectedOption = selectBox.options[selectBox.selectedIndex].text
if (selectedOption == 'Restaurant name'){
searchRestaurant.disabled = false
getRestaurantAPI()
}else if(selectedOption == 'Dish name'){
searchRestaurant.disabled = false
getDishAPI()
}
})
}
if (window.location.pathname == "/home.php"){
searchLogic();
}