- button
This commit is contained in:
parent
9e084428d9
commit
389612db73
@ -23,7 +23,8 @@ from alkatorapi.views import (
|
|||||||
payment_result, payment_state,
|
payment_result, payment_state,
|
||||||
invoice, upload_files,
|
invoice, upload_files,
|
||||||
login_status, change_racer,
|
login_status, change_racer,
|
||||||
products, cart_add, cart, cart_delete
|
products, cart_add, cart, cart_delete,
|
||||||
|
cart_decrease,
|
||||||
)
|
)
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
@ -45,4 +46,5 @@ urlpatterns = [
|
|||||||
path('api/cart/add', cart_add),
|
path('api/cart/add', cart_add),
|
||||||
path('api/cart', cart),
|
path('api/cart', cart),
|
||||||
path('api/cart/delete', cart_delete),
|
path('api/cart/delete', cart_delete),
|
||||||
|
path('api/cart/decrease', cart_decrease),
|
||||||
]
|
]
|
||||||
|
@ -382,6 +382,20 @@ def cart_delete(request):
|
|||||||
return HttpResponse('{"status":"success", "reason":"Úspěšně odstraněno z košíku."}')
|
return HttpResponse('{"status":"success", "reason":"Úspěšně odstraněno z košíku."}')
|
||||||
|
|
||||||
|
|
||||||
|
def cart_decrease(request):
|
||||||
|
_id = request.GET['id']
|
||||||
|
user = request.user
|
||||||
|
cart = user.cart
|
||||||
|
product = Product.objects.get(id=_id)
|
||||||
|
cart_product = CartProduct.objects.get(cart=cart, product=product)
|
||||||
|
cart_product.quantity -= 1
|
||||||
|
if cart_product.quantity == 0:
|
||||||
|
cart_product.delete()
|
||||||
|
else:
|
||||||
|
cart_product.save()
|
||||||
|
return HttpResponse('{"status":"success", "reason":"Úspěšně sníženo množství v košíku."}')
|
||||||
|
|
||||||
|
|
||||||
def cart(request):
|
def cart(request):
|
||||||
user = request.user
|
user = request.user
|
||||||
if not user.is_authenticated:
|
if not user.is_authenticated:
|
||||||
|
@ -112,6 +112,31 @@ class Main extends Component {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
decreaseInCart(product){
|
||||||
|
fetch(addr_prefix + "/api/cart/decrease?id=" + product.id).then(resp => resp.json()).then(json=>{
|
||||||
|
if(json.status == "failed"){
|
||||||
|
this.setState({
|
||||||
|
status_text: json.reason,
|
||||||
|
status: "failed",
|
||||||
|
})
|
||||||
|
}else{
|
||||||
|
this.setState({
|
||||||
|
cart: this.state.cart.filter(iter_product => {
|
||||||
|
if(iter_product.id == product.id){
|
||||||
|
iter_product.quantity -= 1
|
||||||
|
if(iter_product.quantity == 0)
|
||||||
|
return false;
|
||||||
|
return true
|
||||||
|
}else{
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
status_text: json.reason,
|
||||||
|
status: "success",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
onSubmit(event){
|
onSubmit(event){
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
let form = document.getElementById("form");
|
let form = document.getElementById("form");
|
||||||
@ -301,6 +326,7 @@ class Main extends Component {
|
|||||||
<td>
|
<td>
|
||||||
<button type="button" class="btn btn-light" onClick={(e) => {this.addToCart(product)}}>+</button>
|
<button type="button" class="btn btn-light" onClick={(e) => {this.addToCart(product)}}>+</button>
|
||||||
{product.quantity}
|
{product.quantity}
|
||||||
|
<button type="button" class="btn btn-light" onClick={(e) => {this.decreaseInCart(product)}}>-</button>
|
||||||
</td>
|
</td>
|
||||||
<td>{product.price} Kč</td>
|
<td>{product.price} Kč</td>
|
||||||
<td><button type="button" class="btn btn-light" onClick={(e) => {this.deleteProduct(product)}}>Smazat</button></td>
|
<td><button type="button" class="btn btn-light" onClick={(e) => {this.deleteProduct(product)}}>Smazat</button></td>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user