From 4f98a634146cacf046a4e9c3a3e2658c19e03300 Mon Sep 17 00:00:00 2001 From: dadaloop82 Date: Thu, 7 May 2026 20:43:26 +0000 Subject: [PATCH] fix: refresh btn busts only total cache (fast); fix _pricesFetching button lock --- api/index.php | 5 +++-- assets/js/app.js | 32 +++++++++++++------------------- index.html | 4 ++-- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/api/index.php b/api/index.php index f7a0a44..83f697b 100644 --- a/api/index.php +++ b/api/index.php @@ -6646,7 +6646,8 @@ function getAllShoppingPrices(PDO $db): void { $country = trim($input['country'] ?? env('PRICE_COUNTRY', 'Italia')); $currency = trim($input['currency'] ?? env('PRICE_CURRENCY', 'EUR')); $lang = trim($input['lang'] ?? 'it'); - $forceRefresh = !empty($input['force_refresh']); + $forceRefresh = !empty($input['force_refresh']); // re-fetch AI prices (expensive, rarely used) + $forceTotal = !empty($input['force_total']); // bust only the 5-min total cache (fast) $updateMonths = (int)env('PRICE_UPDATE_MONTHS', '3'); if (empty($clientItems)) { @@ -6696,7 +6697,7 @@ function getAllShoppingPrices(PDO $db): void { $items )) . $country . $currency); - if (!$forceRefresh && file_exists($totalCachePath)) { + if (!$forceRefresh && !$forceTotal && file_exists($totalCachePath)) { $tc = json_decode(file_get_contents($totalCachePath), true) ?? []; if (isset($tc[$totalCacheKey]) && (time() - ($tc[$totalCacheKey]['ts'] ?? 0)) < 300) { $cached = $tc[$totalCacheKey]['result']; diff --git a/assets/js/app.js b/assets/js/app.js index 6829fbe..c1c1664 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -8870,10 +8870,12 @@ async function fetchAllPrices(forceRefresh = false) { // the active fetch finishes and re-enables them in its finally block. const fetchBtn = document.getElementById('btn-fetch-prices'); const refreshBtn = document.getElementById('btn-price-refresh'); + if (_pricesFetching) { + // Already running — don't stack calls, just leave the active fetch to finish + return; + } if (fetchBtn) fetchBtn.disabled = true; if (refreshBtn) { refreshBtn.disabled = true; refreshBtn.textContent = '⏳'; } - - if (_pricesFetching) return; if (!shoppingItems.length) { if (fetchBtn) fetchBtn.disabled = false; if (refreshBtn) { refreshBtn.disabled = false; refreshBtn.textContent = '🔄'; } @@ -8898,22 +8900,11 @@ async function fetchAllPrices(forceRefresh = false) { const sym = _currencySymbol(s.price_currency || 'EUR'); - if (forceRefresh) { - _cachedPrices = {}; - try { sessionStorage.removeItem('_pricecache'); sessionStorage.removeItem('_pricetotal'); sessionStorage.removeItem('_pricecachets'); } catch { /* ignore */ } - shoppingItems.forEach((_, idx) => { - const badge = document.getElementById(`price-badge-${idx}`); - if (badge) badge.innerHTML = ``; - }); - if (totalEl) totalEl.textContent = t('shopping.price_loading'); - if (loadingBar) loadingBar.style.display = 'block'; - if (loadingInner) { loadingInner.style.transition = 'none'; loadingInner.style.width = '5%'; } - } else { - // Show cached prices instantly while the server call is in flight - _applyPriceBadgesFromCache(); - if (loadingBar) loadingBar.style.display = 'block'; - if (loadingInner) { loadingInner.style.transition = 'none'; loadingInner.style.width = '5%'; } - } + // Show cached badges instantly while the server call is in flight + _applyPriceBadgesFromCache(); + if (totalEl && forceRefresh) totalEl.textContent = t('shopping.price_loading'); + if (loadingBar) loadingBar.style.display = 'block'; + if (loadingInner) { loadingInner.style.transition = 'none'; loadingInner.style.width = '5%'; } const lang = s.language || 'it'; const country = s.price_country || 'Italia'; @@ -8927,7 +8918,10 @@ async function fetchAllPrices(forceRefresh = false) { const data = await api('get_all_shopping_prices', {}, 'POST', { items: itemsPayload, country, currency, lang, - force_refresh: forceRefresh, + // force_refresh=true only busts the 5-min total cache on the server; + // it never re-fetches AI prices (3-month per-item cache stays intact) + force_total: forceRefresh, + force_refresh: false, }); if (data && data.success) { diff --git a/index.html b/index.html index c5e4cd0..d4d97e7 100644 --- a/index.html +++ b/index.html @@ -635,7 +635,7 @@
💰 Spesa stimata: - +
- +