05/31 Fix fetch price issues

This commit is contained in:
2026-05-31 22:06:10 -04:00
parent 9bc8bb3238
commit 67ed91d94c
3 changed files with 62 additions and 13 deletions
@@ -105,17 +105,33 @@ priceInput.addEventListener('input', updateTotal);
const fillBtn = document.getElementById('fillPrice');
if (fillBtn) {
fillBtn.addEventListener('click', function() {
fillBtn.disabled = true;
fillBtn.textContent = '…';
fetch('/investments/api/price/{{ inv.ticker }}')
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 15000);
fetch('/investments/api/price/{{ inv.ticker }}', { signal: controller.signal })
.then(r => r.json())
.then(data => {
if (data.price) {
clearTimeout(timer);
fillBtn.disabled = false;
fillBtn.textContent = '↓';
if (data.price != null) {
priceInput.value = data.price.toFixed(4);
updateTotal();
} else {
fillBtn.title = data.error || 'No price available';
fillBtn.style.color = '#ef4444';
setTimeout(() => { fillBtn.style.color = ''; fillBtn.title = ''; }, 3000);
}
fillBtn.textContent = '↓';
})
.catch(() => { fillBtn.textContent = '↓'; });
.catch(err => {
clearTimeout(timer);
fillBtn.disabled = false;
fillBtn.textContent = '↓';
fillBtn.title = err.name === 'AbortError' ? 'Timed out' : 'Network error';
});
});
}
{% endif %}