diff --git a/alkator/urls.py b/alkator/urls.py index 03dbf15..2b3b177 100644 --- a/alkator/urls.py +++ b/alkator/urls.py @@ -23,7 +23,7 @@ from alkatorapi.views import ( payment_result, payment_state, invoice, upload_files, login_status, change_racer, - products, cart_add + products, cart_add, cart ) urlpatterns = [ @@ -43,4 +43,5 @@ urlpatterns = [ path('api/upload_files', upload_files), path('api/products', products), path('api/cart/add', cart_add), + path('api/cart', cart), ] diff --git a/alkatorapi/views.py b/alkatorapi/views.py index d2c12c3..c5ac7fd 100644 --- a/alkatorapi/views.py +++ b/alkatorapi/views.py @@ -355,18 +355,40 @@ def cart_add(request): except AttributeError: cart = Cart(user=user) cart.save() + product = Product.objects.get(id=_id) try: cart_product = CartProduct( - product=Product.objects.get(id=_id), + product=product, cart=cart, quantity=1, ) cart_product.save() except IntegrityError as e: - return HttpResponse('{"status":"failed", "reason":"Předmět už v košíku je!"}', status=400, content_type='application/json') + cart_product = CartProduct.objects.get(cart=cart, product=product) + cart_product.quantity += 1 + cart_product.save() return HttpResponse('{"status":"success", "reason":"Úspěšně přidáno do košíku."}', status=200) +def cart(request): + user = request.user + if not user.is_authenticated: + return HttpResponse("[]") + cart = [] + try: + for product in CartProduct.objects.filter(user.cart): + cart.append({ + "id": product.product.id, + "name": product.product.name, + "quantity": product.quantity, + "price": product.price, + "img": product.product.img.url, + }) + except AttributeError: + return HttpResponse("[]") + return HttpResponse(json.dumps(cart)) + + def results(request): results = [] n = 1 diff --git a/frontend/src/scripts/index.js b/frontend/src/scripts/index.js index 6f8babc..24d8e38 100644 --- a/frontend/src/scripts/index.js +++ b/frontend/src/scripts/index.js @@ -42,6 +42,9 @@ class Main extends Component { fetch(addr_prefix + "/api/products").then(resp => resp.json()).then(json => { this.setState({products: json}) }) + fetch(addr_prefix + "/api/cart").then(resp => resp.json()).then(json => { + this.setState({cart: json}) + }) fetch(addr_prefix + "/api/login_status").then(resp => resp.json()).then(json => { this.setState({login_status: json}) }) @@ -79,19 +82,33 @@ class Main extends Component { showRacerModalWindow: true, }) } - addToCart = (id) =>{ - fetch(addr_prefix + "/api/cart/add?id=" + id).then(resp => resp.json()).then(json=>{ + addToCart = (product) =>{ + fetch(addr_prefix + "/api/cart/add?id=" + product.id).then(resp => resp.json()).then(json=>{ if(json.status == "failed"){ this.setState({ status_text: json.reason, status: "failed", }) }else{ - this.setState({ - status_text: json.reason, - status: "success", - cart: [...this.state.cart, id], - }); + if(this.state.cart.filter(iter_product => { + if(iter_product.id == product.id){ + iter_product.quantity += 1 + return true + }else{ + return false + } + }).length == 0){ + this.setState({ + status_text: json.reason, + status: "success", + cart: [...this.state.cart, {...product, quantity: 1}], + }) + }else { + this.setState({ + status_text: json.reason, + status: "success", + }) + } } }) } @@ -122,6 +139,9 @@ class Main extends Component { fetch(addr_prefix + "/api/login_status").then(resp => resp.json()).then(json => { this.setState({login_status: json}) }) + fetch(addr_prefix + "/api/cart").then(resp => resp.json()).then(json => { + this.setState({cart: json}) + }) this.setState({ showRacerModalWindow: false, status_text: data.success, @@ -249,7 +269,7 @@ class Main extends Component {
{product.description}
- {product.price} Kč {e.preventDefault();this.addToCart(product.id)}} class="btn btn-primary">Přidat do košíku + {product.price} Kč {e.preventDefault();this.addToCart(product)}} class="btn btn-primary">Přidat do košíku