app js working fully"

This commit is contained in:
0ceanSlim 2024-03-28 13:08:22 -04:00
parent 3b142d20a4
commit 33f3607c20

View File

@ -15,15 +15,58 @@ function updateBitcoinPrice(selectedCurrency) {
}) })
.catch(error => console.error(error)); .catch(error => console.error(error));
} }
//function refreshBitcoinPrice(selectedCurrency) {
// updateBitcoinPrice(selectedCurrency);
// setInterval(() => updateBitcoinPrice(selectedCurrency), 300000);
//}
updateBitcoinPrice('usd'); function updateSatsToDollarsConversion() {
const satsInputElement = document.getElementById('sats-input');
const dollarsOutputElement = document.getElementById('dollars-output');
const satsValue = parseFloat(satsInputElement.value);
if (!isNaN(satsValue)) {
const currencySelect = document.getElementById('currency-select');
const selectedCurrency = currencySelect.value;
fetch(`https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=${selectedCurrency}`)
.then(response => response.json())
.then(data => {
const bitcoinPrice = data['bitcoin'][selectedCurrency];
const satsConversionFactor = 100000000; // 1 bitcoin = 100 million sats
const dollarsValue = (satsValue / satsConversionFactor) * bitcoinPrice;
dollarsOutputElement.textContent = `${dollarsValue.toFixed(2)} ${selectedCurrency.toUpperCase()}`;
})
.catch(error => console.error(error));
} else {
dollarsOutputElement.textContent = '';
}
}
function updateFiatToSatsConversion(selectedCurrency) {
fetch(`https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=${selectedCurrency}`)
.then(response => response.json())
.then(data => {
const bitcoinPrice = data['bitcoin'][selectedCurrency];
const satsConversionFactor = 100000000;
const currencyValue = 1;
const sats = (currencyValue / bitcoinPrice) * satsConversionFactor;
const currencyToSatsElement = document.getElementById('dollars-to-sats');
currencyToSatsElement.textContent = `1 ${selectedCurrency.toUpperCase()} = ${sats.toFixed(0)} sats`;
})
.catch(error => console.error(error));
}
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
const satsInputElement = document.getElementById('sats-input'); const satsInputElement = document.getElementById('sats-input');
const currencySelect = document.getElementById('currency-select'); const currencySelect = document.getElementById('currency-select');
const selectedCurrency = currencySelect.value;
// Initially fetch Bitcoin price in USD // Initially fetch Bitcoin price in USD
updateBitcoinPrice('usd'); updateBitcoinPrice(selectedCurrency);
updateFiatToSatsConversion(selectedCurrency)
satsInputElement.addEventListener('input', updateSatsToDollarsConversion); satsInputElement.addEventListener('input', updateSatsToDollarsConversion);
@ -32,8 +75,8 @@ document.addEventListener('DOMContentLoaded', () => {
updateBitcoinPrice(selectedCurrency); updateBitcoinPrice(selectedCurrency);
updateFiatToSatsConversion(selectedCurrency); updateFiatToSatsConversion(selectedCurrency);
updateSatsToDollarsConversion(selectedCurrency); updateSatsToDollarsConversion(selectedCurrency);
updateFiatToSatsConversion(selectedCurrency)
});
}); });
refreshBitcoinPrice('usd'); // Refresh Bitcoin price in USD
});