diff --git a/src/purchase/forms.py b/src/purchase/forms.py index e3f513b..142905f 100644 --- a/src/purchase/forms.py +++ b/src/purchase/forms.py @@ -19,9 +19,6 @@ class BasketForm(forms.ModelForm): fields = ["payment_method"] widgets = {"payment_method": forms.RadioSelect} - class Media: - js = ["purchase/js/basket_form.js"] - def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.helper = FormHelper() diff --git a/src/purchase/static/purchase/js/basket_form.js b/src/purchase/static/purchase/js/basket_form.js index 02890c4..6c8a715 100644 --- a/src/purchase/static/purchase/js/basket_form.js +++ b/src/purchase/static/purchase/js/basket_form.js @@ -23,3 +23,28 @@ window.dispatchChanged = function (element) { const event = new Event("change", { bubbles: true }); element.dispatchEvent(event); }; + +window.onUpdateQuantity = function (event) { + const { target } = event; + const parent = target.closest(".card"); + const classes = ["bg-success", "text-white"]; + if (target.value > 0) { + parent.classList.add(...classes); + } else { + parent.classList.remove(...classes); + } +}; + +window.setupEventsListener = function () { + const cards = document.querySelectorAll(".card input"); + cards.forEach((item) => { + item.addEventListener("change", window.onUpdateQuantity); + item.addEventListener("keyup", window.onUpdateQuantity); + }); +}; + +document.addEventListener("newUnpriced", function () { + window.setupEventsListener(); +}); + +window.setupEventsListener(); diff --git a/src/purchase/templates/purchase/basket_form.html b/src/purchase/templates/purchase/basket_form.html index 913d4d0..287f1b2 100644 --- a/src/purchase/templates/purchase/basket_form.html +++ b/src/purchase/templates/purchase/basket_form.html @@ -64,4 +64,5 @@ {% block extrascript %} {% django_htmx_script %} + {% endblock %} diff --git a/src/purchase/templates/purchase/snippets/basket_unpriced_item.html b/src/purchase/templates/purchase/snippets/basket_unpriced_item.html index f728830..e4b6b4a 100644 --- a/src/purchase/templates/purchase/snippets/basket_unpriced_item.html +++ b/src/purchase/templates/purchase/snippets/basket_unpriced_item.html @@ -1,6 +1,6 @@ {% load crispy_forms_field %}
-
+
{% if product.image %} {% else %} diff --git a/src/purchase/views/basket.py b/src/purchase/views/basket.py index 78742a2..f03b45c 100644 --- a/src/purchase/views/basket.py +++ b/src/purchase/views/basket.py @@ -90,11 +90,12 @@ def additional_unpriced_product(request: WSGIRequest) -> HttpResponse: value = request.GET.get("value", 0) product = get_object_or_404(Product.objects.with_no_fixed_price(), pk=product_id) context = {"product": product, "value": value} - return render( + res = render( request, "purchase/snippets/basket_unpriced_item.html", context, ) + return trigger_client_event(res, "newUnpriced", after="settle") @permission_required("purchase.view_basket")