Skip to content

Commit

Permalink
Improve empty basket and tray price management
Browse files Browse the repository at this point in the history
  • Loading branch information
klmp200 committed Dec 22, 2024
1 parent 7071553 commit 372470b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
12 changes: 7 additions & 5 deletions counter/static/bundled/counter/counter-click-index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { exportToHtml } from "#core:utils/globals";

const quantityForTrayPrice = 6;

interface InitialFormData {
/* Used to refill the form when the backend raises an error */
id?: string;
id?: Pick<Product, "id">;
quantity?: number;
errors?: string[];
}
Expand All @@ -23,11 +21,13 @@ interface Product {
name: string;
price: number;
hasTrayPrice: boolean;
quantityForTrayPrice: number;
}

class BasketItem {
quantity: number;
product: Product;
quantityForTrayPrice: number;
errors: string[];

constructor(product: Product, quantity: number) {
Expand All @@ -40,7 +40,7 @@ class BasketItem {
if (!this.product.hasTrayPrice) {
return 0;
}
return Math.floor(this.quantity / quantityForTrayPrice);
return Math.floor(this.quantity / this.product.quantityForTrayPrice);
}

sum(): number {
Expand Down Expand Up @@ -127,7 +127,9 @@ exportToHtml("loadCounter", (config: CounterConfig) => {
},

finish() {
this.$refs.basketForm.submit();
if (this.getBasketSize() > 0) {
this.$refs.basketForm.submit();
}
},

cancel() {
Expand Down
8 changes: 5 additions & 3 deletions counter/templates/counter/counter_click.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@
</div>
{% endfor %}
<p>{% trans %}Basket: {% endtrans %}</p>
<form method="post" action="" x-ref="basketForm">
<form x-cloak method="post" action="" x-ref="basketForm">
{% csrf_token %}
<div x-ref="basketManagementForm">
{{ form.management_form }}
</div>
<ul x-show="getBasketSize() === 0">{% trans %}This basket is empty{% endtrans %}</ul>
<template x-for="(item, index) in Object.values(basket)">
<ul>
<template x-for="error in item.errors">
Expand All @@ -97,7 +98,7 @@

<span x-text="item.product.name"></span> :
<span x-text="item.sum().toLocaleString(undefined, { minimumFractionDigits: 2 })">€</span>
<span x-cloak x-show="item.getBonusQuantity() > 0" x-text="`${item.getBonusQuantity()} x P`"></span>
<span x-show="item.getBonusQuantity() > 0" x-text="`${item.getBonusQuantity()} x P`"></span>
<button @click.prevent="removeFromBasket(item.product.id)"><i class="fa fa-trash-can delete-action"></i></button>

<input type="hidden" :value="item.quantity" :id="`id_form-${index}-quantity`" :name="`form-${index}-quantity`" required readonly>
Expand All @@ -112,7 +113,7 @@
</p>

<div class="row">
<input class="btn btn-blue" type="submit" @click.prevent="finish" value="{% trans %}Finish{% endtrans %}"/>
<input class="btn btn-blue" type="submit" @click.prevent="finish" :disabled="getBasketSize() == 0" value="{% trans %}Finish{% endtrans %}"/>
<input class="btn btn-grey" type="submit" @click.prevent="cancel" value="{% trans %}Cancel{% endtrans %}"/>
</div>
</form>
Expand Down Expand Up @@ -188,6 +189,7 @@
name: "{{ product.name }}",
price: {{ product.price }},
hasTrayPrice: {{ product.tray | tojson }},
quantityForTrayPrice: {{ product.QUANTITY_FOR_TRAY_PRICE }},
},
{%- endfor -%}
};
Expand Down
12 changes: 5 additions & 7 deletions counter/views/click.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,8 @@ def clean(self):
self._check_enough_money(self[0].counter, self[0].customer)

def _check_forms_have_errors(self):
for form in self:
if len(form.errors):
raise ValidationError(_("Submmited basket is invalid"))
if any(len(form.errors) > 0 for form in self):
raise ValidationError(_("Submmited basket is invalid"))

def _check_enough_money(self, counter: Counter, customer: Customer):
self.total_price = sum([data["total_price"] for data in self.cleaned_data])
Expand Down Expand Up @@ -272,10 +271,9 @@ def get_context_data(self, **kwargs):
kwargs["cancel_url"] = self.get_success_url()

# To get all forms errors to the javascript, we create a list of error list
kwargs["form_errors"] = []
for field_errors in kwargs["form"].errors:
kwargs["form_errors"].append(list(field_errors.values()))

kwargs["form_errors"] = [
list(field_error.values()) for field_error in kwargs["form"].errors
]
if self.object.type == "BAR":
kwargs["student_card_fragment"] = StudentCardFormView.get_template_data(
self.customer
Expand Down

0 comments on commit 372470b

Please sign in to comment.