fix: price refresh fast + button unlock

This commit is contained in:
dadaloop82
2026-05-07 20:43:28 +00:00
3 changed files with 18 additions and 23 deletions
+3 -2
View File
@@ -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'];
+13 -19
View File
@@ -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 = `<span class="price-col-loading">…</span>`;
});
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) {
+2 -2
View File
@@ -635,7 +635,7 @@
<div class="shopping-price-total-row">
<span class="price-total-label" data-i18n="shopping.price_total_label">💰 Spesa stimata:</span>
<span class="price-total-value" id="price-total-value"></span>
<button class="btn-price-refresh" id="btn-price-refresh" onclick="fetchAllPrices(true)" title="Aggiorna prezzi">🔄</button>
<button class="btn-price-refresh" id="btn-price-refresh" onclick="fetchAllPrices(false)" title="Aggiorna prezzi">🔄</button>
</div>
<div id="price-loading-bar" style="display:none" class="price-loading-bar">
<div class="price-loading-inner"></div>
@@ -1462,6 +1462,6 @@
</div>
</div>
<script src="assets/js/app.js?v=20260507l"></script>
<script src="assets/js/app.js?v=20260507m"></script>
</body>
</html>