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 %}