feat: update cart with custom trigger

This commit is contained in:
2024-11-27 17:28:09 +01:00
parent fa1d904db5
commit d16d194ae4
5 changed files with 4 additions and 61 deletions
+1 -1
View File
@@ -7,7 +7,7 @@
<title>{{ title }}</title>
{% include "theme/seo.html" %}
<link rel="shortcut icon" type="image/png" href="{% static 'favicon.ico' %}"/>
<script src="https://unpkg.com/htmx.org@2.0.3"></script>
{% tailwind_css %}
Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

@@ -1,57 +0,0 @@
{% load i18n %}
<div class="grid grid-cols-2">
{% if cart.items.count == 0 %}
<h2 class="text-sm font-medium text-gray-900 dark:text-white">No hay nada en el carrito</h2>
{% endif %}
{% for cart_item in cart.items.all %}
<div>
<a href="#"
class="truncate text-sm font-semibold leading-none text-gray-900 dark:text-white hover:underline">{{ cart_item.product.name }}</a>
<p
class="mt-0.5 truncate text-sm font-normal text-gray-500 dark:text-gray-400">{{ cart_item.product.price.price_with_tax }}</p>
</div>
<div class="flex items-center justify-end gap-6">
<p
class="text-sm font-normal leading-none text-gray-500 dark:text-gray-400">{% translate 'Cantidad' %}: {{ cart_item.quantity }}</p>
<form hx-post="{% url 'web:delete_cart_item' pk=cart_item.pk %}"
hx-swap="none">
<button data-tooltip-target="tooltipRemoveItem1a" type="submit"
class="text-red-600 hover:text-red-700 dark:text-red-500 dark:hover:text-red-600"
>
{% csrf_token %}
<span class="sr-only">{% translate 'Quitar' %}</span>
<svg class="h-4 w-4" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor"
viewBox="0 0 24 24">
<path fill-rule="evenodd"
d="M2 12a10 10 0 1 1 20 0 10 10 0 0 1-20 0Zm7.7-3.7a1 1 0 0 0-1.4 1.4l2.3 2.3-2.3 2.3a1 1 0 1 0 1.4 1.4l2.3-2.3 2.3 2.3a1 1 0 0 0 1.4-1.4L13.4 12l2.3-2.3a1 1 0 0 0-1.4-1.4L12 10.6 9.7 8.3Z"
clip-rule="evenodd"/>
</svg>
</button>
</form>
<div id="tooltipRemoveItem1a" role="tooltip"
class="tooltip invisible absolute z-10 inline-block rounded-lg bg-gray-900 px-3 py-2 text-sm font-medium text-white opacity-0 shadow-sm transition-opacity duration-300 dark:bg-gray-700"
>
Remove item
<div class="tooltip-arrow" data-popper-arrow></div>
</div>
</div>
{% endfor %}
</div>
{% if cart.items.count > 0 %}
<a href="#" title=""
class="mb-2 me-2 inline-flex w-full items-center justify-center rounded-lg bg-primary-700 px-5 py-2.5 text-sm
font-medium text-white hover:bg-primary-800 focus:outline-none focus:ring-4 focus:ring-primary-300
dark:bg-primary-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800"
role="button"> {% translate 'Ir a pagar' %}
</a>
{% endif %}
+1 -1
View File
@@ -46,7 +46,7 @@
{% include 'components/theme-switch.html' %}
<!-- Show/Hide cart -->
<div id="navbar-cart-dropdown" hx-get="{% url 'web:cart_dropdown' %}" hx-trigger="load"></div>
<div id="navbar-cart-dropdown" hx-get="{% url 'web:cart_dropdown' %}" hx-trigger="load, updated-cart from:body"></div>
{% if request.user.is_authenticated %}
<!-- Show/Hide user account dropdown -->
+2 -2
View File
@@ -38,7 +38,7 @@ class ProductDetail(TemplateView):
)
def add_cart_item(request, *args, **kwargs):
product = get_object_or_404(Product, pk=request.POST.get("product"))
response = HttpResponse(status=201)
response = HttpResponse(status=201, headers={'HX-Trigger': 'updated-cart'})
if not request.user.is_authenticated:
cart = Cart.objects.create(
@@ -72,7 +72,7 @@ def delete_cart_item(request, pk, *args, **kwargs):
cart = get_cart(request)
cart_item = get_object_or_404(CartItem, pk=pk, cart=cart)
cart_item.delete()
return HttpResponse(status=204)
return HttpResponse(status=204, headers={'HX-Trigger': 'updated-cart'})
index = IndexView.as_view()