From 64032f79f7e9fef5f0277d8c086a973b557b78ed Mon Sep 17 00:00:00 2001 From: imperosol Date: Fri, 22 May 2026 14:46:40 +0200 Subject: [PATCH] clean invalid items from eboutic baskets --- .../static/bundled/eboutic/eboutic-index.ts | 28 +++++++++++-------- eboutic/templates/eboutic/eboutic_main.jinja | 8 +++++- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/eboutic/static/bundled/eboutic/eboutic-index.ts b/eboutic/static/bundled/eboutic/eboutic-index.ts index 18573158..59a258e3 100644 --- a/eboutic/static/bundled/eboutic/eboutic-index.ts +++ b/eboutic/static/bundled/eboutic/eboutic-index.ts @@ -11,7 +11,7 @@ const BASKET_CACHE_KEY = "basket"; const BASKET_CACHE_VERSION = 1; document.addEventListener("alpine:init", () => { - Alpine.data("basket", (lastPurchaseTime?: number) => ({ + Alpine.data("basket", (validPrices: number[], lastPurchaseTime?: number) => ({ basket: [] as BasketItem[], init() { @@ -19,15 +19,6 @@ document.addEventListener("alpine:init", () => { this.$watch("basket", () => { 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, 10)) - ) { - this.basket = []; - } - } document .getElementById("id_form-TOTAL_FORMS") .setAttribute(":value", "basket.length"); @@ -37,7 +28,22 @@ document.addEventListener("alpine:init", () => { const cached = versionedLocalStorage.getItem(BASKET_CACHE_KEY, { version: BASKET_CACHE_VERSION, }); - return cached ?? []; + if (!cached) { + return []; + } + if ( + lastPurchaseTime !== null && + localStorage.basketTimestamp !== undefined && + new Date(lastPurchaseTime) >= + new Date(Number.parseInt(localStorage.basketTimestamp, 10)) + ) { + // Invalidate basket if a purchase was made + return []; + } + // The basket is cached and not expired, so return it, + // but without items that are invalid + // (e.g. because the product is archived, or sold out) + return cached.filter((item) => validPrices.includes(item.priceId)); }, saveBasket() { diff --git a/eboutic/templates/eboutic/eboutic_main.jinja b/eboutic/templates/eboutic/eboutic_main.jinja index 869c79b1..ac814916 100644 --- a/eboutic/templates/eboutic/eboutic_main.jinja +++ b/eboutic/templates/eboutic/eboutic_main.jinja @@ -30,7 +30,13 @@ {% block content %}

{% trans %}Eboutic{% endtrans %}

-
+

Panier