const exchangeRates = null; // Currency rates fetched from the server console.log(exchangeRates); let giveCurrency = 'INR'; // Default 'give' currency let takeCurrency = 'USD'; // Default 'take' currency updateCurrencyInfo('give', giveCurrency); updateCurrencyInfo('take', takeCurrency); selectCurrency('give', giveCurrency); selectCurrency('take', takeCurrency); filterCurrenciesForSection('take', giveCurrency); updateCurrencyIcon('give', giveCurrency); // Update the initial give currency icon updateCurrencyIcon('take', takeCurrency); // Update the initial take currency icon updateCardPlaceholder(giveCurrency); $('#giveAmount').on('input', performCurrencyConversion); $('.card-currency--list.give .currency-item').on('click', function () { giveCurrency = $(this).data('currency'); $('.card-currency--list.give .currency-item').removeClass('selected'); $(this).addClass('selected'); filterCurrenciesForSection('take', giveCurrency); // Update take section to hide selected give currency updateCurrencyInfo('give', giveCurrency); performCurrencyConversion(); updateCurrencyIcon('give', giveCurrency); // Update give currency icon updateCardPlaceholder(giveCurrency); }); $('.card-currency--list.take .currency-item').on('click', function () { takeCurrency = $(this).data('currency'); $('.card-currency--list.take .currency-item').removeClass('selected'); $(this).addClass('selected'); filterCurrenciesForSection('give', takeCurrency); // Update give section to hide selected take currency $('#take_currency').val(takeCurrency); updateCurrencyInfo('take', takeCurrency); performCurrencyConversion(); updateCurrencyIcon('take', takeCurrency); // Update take currency icon }); function selectCurrency(section, currency) { const $currencyList = $(`.card-currency--list.${section}`); const $defaultCurrencyItem = $currencyList.find(`.currency-item[data-currency="${currency}"]`); if ($defaultCurrencyItem.length) { $currencyList.find('.currency-item').removeClass('selected'); $defaultCurrencyItem.addClass('selected'); } } // currencyToHide in the take section if selected in the give section function filterCurrenciesForSection(section, currencyToHide) { const $currencyItems = $(`.card-currency--list.${section} .currency-item`); $currencyItems.show(); // Reset all visibility $currencyItems.filter(`[data-currency="${currencyToHide}"]`).hide(); // Hide selected currency } function performCurrencyConversion() { const giveAmount = parseFloat($('#giveAmount').val()) || 0; $('#give_amount').val(giveAmount); if (!isNaN(giveAmount)) { const giveRate = exchangeRates[giveCurrency] || 1; const takeRate = exchangeRates[takeCurrency] || 1; if (giveRate && takeRate) { const amountInUSD = giveAmount / giveRate; const convertedAmount = amountInUSD * takeRate; $('#takeAmount').val(convertedAmount.toFixed(2)); $('#take_amount').val(convertedAmount.toFixed(2)); } else { alert('Conversion rate not available.'); } } }