-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd_to_cart.js
37 lines (33 loc) · 1.3 KB
/
add_to_cart.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
const carts = document.getElementsByClassName("add_cart")
async function add_to_cart(dishId) {
return fetch('action_add_to_cart.php', {
method: 'post',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: encodeForAjax({dishId:dishId})
})
}
for(const cart of carts){
cart.addEventListener('click',function(event){
const dishId = cart.classList[0]
const paragraph = document.createElement("p")
console.log("✅💾 Adding Dish to cart...")
add_to_cart(dishId)
.catch(() => console.error('Network Error'))
.then(response => response.json())
.catch(() => console.error('Error parsing JSON'))
.then(json =>{ paragraph.textContent = json
paragraph.classList.add("add_to_cart_response")
if(json=="Success!")
paragraph.classList.add("success")
else
paragraph.classList.add("error")
document.body.appendChild(paragraph)
setTimeout(function(){
document.body.removeChild(paragraph)
},3000
)
})
})
}