calculateTotalPrice

This commit is contained in:
Martin Quarda 2024-10-14 09:12:42 +02:00
parent 389612db73
commit 5f350b43c4
4 changed files with 32 additions and 2 deletions

View 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),
),
]

View File

@ -69,6 +69,7 @@ class Product(models.Model):
description = models.TextField() description = models.TextField()
img = models.ImageField(upload_to="photos") img = models.ImageField(upload_to="photos")
price = models.IntegerField() price = models.IntegerField()
hidden = models.BooleanField(default=False)
quantity = models.IntegerField() quantity = models.IntegerField()
def __str__(self): def __str__(self):

View File

@ -343,13 +343,15 @@ def products(request):
'price': product.price, 'price': product.price,
'quantity': product.quantity, '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') ]), content_type='application/json')
def cart_add(request): def cart_add(request):
_id = request.GET['id'] _id = request.GET['id']
user = request.user 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: try:
cart = user.cart cart = user.cart
except AttributeError: except AttributeError:

View File

@ -112,6 +112,9 @@ class Main extends Component {
} }
}) })
} }
calculateTotalPrice(){
return this.state.cart.reduce((acc, cur) => acc + cur.price * cur.quantity, 0)
}
decreaseInCart(product){ decreaseInCart(product){
fetch(addr_prefix + "/api/cart/decrease?id=" + product.id).then(resp => resp.json()).then(json=>{ fetch(addr_prefix + "/api/cart/decrease?id=" + product.id).then(resp => resp.json()).then(json=>{
if(json.status == "failed"){ 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> <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> </li>
} }
{Object.keys(this.state.login_status).length > 0 && this.state.products.length > 0 && {this.state.products.length > 0 &&
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="#eshop">Eshop</a> <a class="nav-link" href="#eshop">Eshop</a>
</li> </li>
@ -331,6 +334,12 @@ class Main extends Component {
<td>{product.price} </td> <td>{product.price} </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>
</tr>)} </tr>)}
<tr>
<td>Celkem</td>
<td></td>
<td>{this.calculateTotalPrice()} </td>
<td></td>
</tr>
</tbody> </tbody>
</table> </table>
</div>} </div>}