calculateTotalPrice
This commit is contained in:
parent
389612db73
commit
5f350b43c4
18
alkatorapi/migrations/0022_product_hidden.py
Normal file
18
alkatorapi/migrations/0022_product_hidden.py
Normal file
@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.12 on 2024-10-14 07:11
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('alkatorapi', '0021_alter_cart_user'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='product',
|
||||
name='hidden',
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
]
|
@ -69,6 +69,7 @@ class Product(models.Model):
|
||||
description = models.TextField()
|
||||
img = models.ImageField(upload_to="photos")
|
||||
price = models.IntegerField()
|
||||
hidden = models.BooleanField(default=False)
|
||||
quantity = models.IntegerField()
|
||||
|
||||
def __str__(self):
|
||||
|
@ -343,13 +343,15 @@ def products(request):
|
||||
'price': product.price,
|
||||
'quantity': product.quantity,
|
||||
}
|
||||
for product in Product.objects.all()[1:] if product.quantity > 0
|
||||
for product in Product.objects.all() if product.quantity > 0 and not product.hidden
|
||||
]), content_type='application/json')
|
||||
|
||||
|
||||
def cart_add(request):
|
||||
_id = request.GET['id']
|
||||
user = request.user
|
||||
if not user.is_authenticated:
|
||||
return HttpResponse('{"status": "failed", "reason": "Pro využivání Košíku je třeba se přihlásit!"}')
|
||||
try:
|
||||
cart = user.cart
|
||||
except AttributeError:
|
||||
|
@ -112,6 +112,9 @@ class Main extends Component {
|
||||
}
|
||||
})
|
||||
}
|
||||
calculateTotalPrice(){
|
||||
return this.state.cart.reduce((acc, cur) => acc + cur.price * cur.quantity, 0)
|
||||
}
|
||||
decreaseInCart(product){
|
||||
fetch(addr_prefix + "/api/cart/decrease?id=" + product.id).then(resp => resp.json()).then(json=>{
|
||||
if(json.status == "failed"){
|
||||
@ -286,7 +289,7 @@ class Main extends Component {
|
||||
<div class="nav-link">Přihlášen {this.state.login_status.first_name} {this.state.login_status.last_name} <a href="/api/logout">Odhlásit</a></div>
|
||||
</li>
|
||||
}
|
||||
{Object.keys(this.state.login_status).length > 0 && this.state.products.length > 0 &&
|
||||
{this.state.products.length > 0 &&
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="#eshop">Eshop</a>
|
||||
</li>
|
||||
@ -331,6 +334,12 @@ class Main extends Component {
|
||||
<td>{product.price} Kč</td>
|
||||
<td><button type="button" class="btn btn-light" onClick={(e) => {this.deleteProduct(product)}}>Smazat</button></td>
|
||||
</tr>)}
|
||||
<tr>
|
||||
<td>Celkem</td>
|
||||
<td></td>
|
||||
<td>{this.calculateTotalPrice()} Kč</td>
|
||||
<td></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>}
|
||||
|
Loading…
x
Reference in New Issue
Block a user