fix: price refresh fast + button unlock
This commit is contained in:
+3
-2
@@ -6646,7 +6646,8 @@ function getAllShoppingPrices(PDO $db): void {
|
|||||||
$country = trim($input['country'] ?? env('PRICE_COUNTRY', 'Italia'));
|
$country = trim($input['country'] ?? env('PRICE_COUNTRY', 'Italia'));
|
||||||
$currency = trim($input['currency'] ?? env('PRICE_CURRENCY', 'EUR'));
|
$currency = trim($input['currency'] ?? env('PRICE_CURRENCY', 'EUR'));
|
||||||
$lang = trim($input['lang'] ?? 'it');
|
$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');
|
$updateMonths = (int)env('PRICE_UPDATE_MONTHS', '3');
|
||||||
|
|
||||||
if (empty($clientItems)) {
|
if (empty($clientItems)) {
|
||||||
@@ -6696,7 +6697,7 @@ function getAllShoppingPrices(PDO $db): void {
|
|||||||
$items
|
$items
|
||||||
)) . $country . $currency);
|
)) . $country . $currency);
|
||||||
|
|
||||||
if (!$forceRefresh && file_exists($totalCachePath)) {
|
if (!$forceRefresh && !$forceTotal && file_exists($totalCachePath)) {
|
||||||
$tc = json_decode(file_get_contents($totalCachePath), true) ?? [];
|
$tc = json_decode(file_get_contents($totalCachePath), true) ?? [];
|
||||||
if (isset($tc[$totalCacheKey]) && (time() - ($tc[$totalCacheKey]['ts'] ?? 0)) < 300) {
|
if (isset($tc[$totalCacheKey]) && (time() - ($tc[$totalCacheKey]['ts'] ?? 0)) < 300) {
|
||||||
$cached = $tc[$totalCacheKey]['result'];
|
$cached = $tc[$totalCacheKey]['result'];
|
||||||
|
|||||||
+13
-19
@@ -8870,10 +8870,12 @@ async function fetchAllPrices(forceRefresh = false) {
|
|||||||
// the active fetch finishes and re-enables them in its finally block.
|
// the active fetch finishes and re-enables them in its finally block.
|
||||||
const fetchBtn = document.getElementById('btn-fetch-prices');
|
const fetchBtn = document.getElementById('btn-fetch-prices');
|
||||||
const refreshBtn = document.getElementById('btn-price-refresh');
|
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 (fetchBtn) fetchBtn.disabled = true;
|
||||||
if (refreshBtn) { refreshBtn.disabled = true; refreshBtn.textContent = '⏳'; }
|
if (refreshBtn) { refreshBtn.disabled = true; refreshBtn.textContent = '⏳'; }
|
||||||
|
|
||||||
if (_pricesFetching) return;
|
|
||||||
if (!shoppingItems.length) {
|
if (!shoppingItems.length) {
|
||||||
if (fetchBtn) fetchBtn.disabled = false;
|
if (fetchBtn) fetchBtn.disabled = false;
|
||||||
if (refreshBtn) { refreshBtn.disabled = false; refreshBtn.textContent = '🔄'; }
|
if (refreshBtn) { refreshBtn.disabled = false; refreshBtn.textContent = '🔄'; }
|
||||||
@@ -8898,22 +8900,11 @@ async function fetchAllPrices(forceRefresh = false) {
|
|||||||
|
|
||||||
const sym = _currencySymbol(s.price_currency || 'EUR');
|
const sym = _currencySymbol(s.price_currency || 'EUR');
|
||||||
|
|
||||||
if (forceRefresh) {
|
// Show cached badges instantly while the server call is in flight
|
||||||
_cachedPrices = {};
|
_applyPriceBadgesFromCache();
|
||||||
try { sessionStorage.removeItem('_pricecache'); sessionStorage.removeItem('_pricetotal'); sessionStorage.removeItem('_pricecachets'); } catch { /* ignore */ }
|
if (totalEl && forceRefresh) totalEl.textContent = t('shopping.price_loading');
|
||||||
shoppingItems.forEach((_, idx) => {
|
if (loadingBar) loadingBar.style.display = 'block';
|
||||||
const badge = document.getElementById(`price-badge-${idx}`);
|
if (loadingInner) { loadingInner.style.transition = 'none'; loadingInner.style.width = '5%'; }
|
||||||
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%'; }
|
|
||||||
}
|
|
||||||
|
|
||||||
const lang = s.language || 'it';
|
const lang = s.language || 'it';
|
||||||
const country = s.price_country || 'Italia';
|
const country = s.price_country || 'Italia';
|
||||||
@@ -8927,7 +8918,10 @@ async function fetchAllPrices(forceRefresh = false) {
|
|||||||
const data = await api('get_all_shopping_prices', {}, 'POST', {
|
const data = await api('get_all_shopping_prices', {}, 'POST', {
|
||||||
items: itemsPayload,
|
items: itemsPayload,
|
||||||
country, currency, lang,
|
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) {
|
if (data && data.success) {
|
||||||
|
|||||||
+2
-2
@@ -635,7 +635,7 @@
|
|||||||
<div class="shopping-price-total-row">
|
<div class="shopping-price-total-row">
|
||||||
<span class="price-total-label" data-i18n="shopping.price_total_label">💰 Spesa stimata:</span>
|
<span class="price-total-label" data-i18n="shopping.price_total_label">💰 Spesa stimata:</span>
|
||||||
<span class="price-total-value" id="price-total-value">–</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>
|
||||||
<div id="price-loading-bar" style="display:none" class="price-loading-bar">
|
<div id="price-loading-bar" style="display:none" class="price-loading-bar">
|
||||||
<div class="price-loading-inner"></div>
|
<div class="price-loading-inner"></div>
|
||||||
@@ -1462,6 +1462,6 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="assets/js/app.js?v=20260507l"></script>
|
<script src="assets/js/app.js?v=20260507m"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user