05/31 Fix fetch price issues
This commit is contained in:
@@ -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 %}
|
||||
|
||||
Reference in New Issue
Block a user