Add auto basket invalidation

This commit is contained in:
Antoine Bartuccio 2025-04-15 23:33:13 +02:00
parent 2ae9baa82f
commit f352b89fc0
3 changed files with 27 additions and 4 deletions

View File

@ -9,7 +9,7 @@ interface BasketItem {
} }
document.addEventListener("alpine:init", () => { document.addEventListener("alpine:init", () => {
Alpine.data("basket", () => ({ Alpine.data("basket", (lastPurchaseTime?: number) => ({
basket: [] as BasketItem[], basket: [] as BasketItem[],
init() { init() {
@ -18,6 +18,16 @@ document.addEventListener("alpine:init", () => {
this.saveBasket(); this.saveBasket();
}); });
// Invalidate basket if a purchase was made
if (lastPurchaseTime !== null && localStorage.basketTimestamp !== undefined) {
if (
new Date(lastPurchaseTime) >=
new Date(Number.parseInt(localStorage.basketTimestamp))
) {
this.basket = [];
}
}
// It's quite tricky to manually apply attributes to the management part // It's quite tricky to manually apply attributes to the management part
// of a formset so we dynamically apply it here // of a formset so we dynamically apply it here
this.$refs.basketManagementForm this.$refs.basketManagementForm
@ -38,6 +48,7 @@ document.addEventListener("alpine:init", () => {
saveBasket() { saveBasket() {
localStorage.basket = JSON.stringify(this.basket); localStorage.basket = JSON.stringify(this.basket);
localStorage.basketTimestamp = Date.now();
}, },
/** /**

View File

@ -21,7 +21,7 @@
{% block content %} {% block content %}
<h1 id="eboutic-title">{% trans %}Eboutic{% endtrans %}</h1> <h1 id="eboutic-title">{% trans %}Eboutic{% endtrans %}</h1>
<div id="eboutic" x-data="basket"> <div id="eboutic" x-data="basket({{ last_purchase_time }})">
<div id="basket"> <div id="basket">
<h3>Panier</h3> <h3>Panier</h3>
<form method="post" action=""> <form method="post" action="">

View File

@ -48,7 +48,7 @@ from django_countries.fields import Country
from core.auth.mixins import CanViewMixin, IsSubscriberMixin from core.auth.mixins import CanViewMixin, IsSubscriberMixin
from core.views.mixins import FragmentMixin, UseFragmentsMixin from core.views.mixins import FragmentMixin, UseFragmentsMixin
from counter.forms import BaseBasketForm, BillingInfoForm, ProductForm from counter.forms import BaseBasketForm, BillingInfoForm, ProductForm
from counter.models import BillingInfo, Counter, Customer, Product from counter.models import BillingInfo, Counter, Customer, Product, Selling
from eboutic.models import ( from eboutic.models import (
Basket, Basket,
BasketItem, BasketItem,
@ -89,7 +89,7 @@ class EbouticMainView(LoginRequiredMixin, FormView):
def get_form_kwargs(self): def get_form_kwargs(self):
kwargs = super().get_form_kwargs() kwargs = super().get_form_kwargs()
kwargs["form_kwargs"] = { kwargs["form_kwargs"] = {
"customer": Customer.get_or_create(self.request.user)[0], "customer": self.customer,
"counter": Counter.objects.get(type="EBOUTIC"), "counter": Counter.objects.get(type="EBOUTIC"),
"allowed_products": {product.id: product for product in self.products}, "allowed_products": {product.id: product for product in self.products},
} }
@ -116,10 +116,22 @@ class EbouticMainView(LoginRequiredMixin, FormView):
def products(self) -> list[Product]: def products(self) -> list[Product]:
return get_eboutic_products(self.request.user) return get_eboutic_products(self.request.user)
@cached_property
def customer(self) -> Customer:
return Customer.get_or_create(self.request.user)[0]
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context["products"] = self.products context["products"] = self.products
context["customer_amount"] = self.request.user.account_balance context["customer_amount"] = self.request.user.account_balance
last_purchase: Selling | None = (
self.customer.buyings.filter(counter__type="EBOUTIC")
.order_by("-date")
.first()
)
context["last_purchase_time"] = (
int(last_purchase.date.timestamp() * 1000) if last_purchase else "null"
)
return context return context