Skip to content
Permalink
4411061874
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
67 lines (54 sloc) 1.67 KB
var updateBtns = document.getElementsByClassName('update-cart')
for (var i =0; i < updateBtns.length; i++){
updateBtns[i].addEventListener('click', function()
{
var productId = this.dataset.product
var action= this.dataset.action
console.log('productId:', productId, 'action:', action)
console.log('USER:', user)
if (user === 'AnonymousUser'){
addCookieItem(productId, action)
}else{
updateUserOrder(productId, action)
}
})
}
function addCookieItem(productId, action){
console.log('Not logged in!....')
if (action == 'add'){
if(cart[productId] == undefined){
cart[productId] = {'quantity':1}
}else{
cart[productId]['quantity'] += 1
}
}
if (action == 'remove'){
cart[productId]['quantity'] -= 1
if(cart[productId]['quantity']<= 0){
console.log('Remove Item')
delete cart[productId]
}
}
console.log('Cart:', cart)
document.cookie = 'cart=' + JSON.stringify(cart) + ";domain=;path=/"
location.reload()
}
function updateUserOrder(productId, action){
console.log('logged in. Sending Data')
var url ='/update_item/'
fetch(url,{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrftoken
},
body:JSON.stringify({ 'productId':productId, 'action':action})
})
.then((response) =>{
return response.json()
})
.then((data) =>{
console.log('data:', data)
location.reload()
})
}