|
| 1 | +// importing currency codes list and API Key |
| 2 | +import { currency_list, api } from "./currencyCodes.js"; |
| 3 | + |
| 4 | +const fromCurrencySelectTag = document.querySelector("#fromCurrency"); |
| 5 | +const toCurrencySelectTag = document.querySelector("#toCurrency"); |
| 6 | +const resultTag = document.querySelector("#result"); |
| 7 | +const btn = document.querySelector("#btn"); |
| 8 | +const status = document.querySelector("#status"); |
| 9 | + |
| 10 | +// append the curreny codes list to the list and select defaults |
| 11 | +currency_list.forEach((code) => { |
| 12 | + const newElement = document.createElement("option"); |
| 13 | + newElement.value = code; |
| 14 | + newElement.textContent = code; |
| 15 | + |
| 16 | + if (code === "USD") |
| 17 | + newElement.selected = true; |
| 18 | + |
| 19 | + fromCurrencySelectTag.append(newElement); |
| 20 | + |
| 21 | + const newElementTo = newElement.cloneNode(true); |
| 22 | + |
| 23 | + if (code === "INR") |
| 24 | + newElementTo.selected = true; |
| 25 | + |
| 26 | + toCurrencySelectTag.append(newElementTo); |
| 27 | +}); |
| 28 | + |
| 29 | +// Swap currency on "click" |
| 30 | +document.getElementById("switchCurrency").onclick = () => { |
| 31 | + |
| 32 | + const fromValue = fromCurrencySelectTag.value; |
| 33 | + fromCurrencySelectTag.value = toCurrencySelectTag.value; |
| 34 | + toCurrencySelectTag.value = fromValue; |
| 35 | + |
| 36 | +}; |
| 37 | + |
| 38 | +// handle "click" event for conversion |
| 39 | +btn.onclick = () => { |
| 40 | + |
| 41 | + const numberInputField = document.getElementById("userValue"); |
| 42 | + const userEnteredAmount = numberInputField.value; |
| 43 | + |
| 44 | + if(userEnteredAmount < 1 || isNaN(userEnteredAmount)) { |
| 45 | + numberInputField.style.border = "1px solid red"; |
| 46 | + resultTag.style.color = "red"; |
| 47 | + resultTag.textContent = "Error: Only numeric values greater than 0 are allowed."; |
| 48 | + } |
| 49 | + else { |
| 50 | + numberInputField.style.border = "1px solid gray"; |
| 51 | + resultTag.style.color = "black"; |
| 52 | + btn.textContent = "Processing: have patients..."; |
| 53 | + |
| 54 | + btn.disabled = true; |
| 55 | + btn.style.color = "gray"; |
| 56 | + btn.style.cursor = "not-allowed"; |
| 57 | + |
| 58 | + convertAmount(userEnteredAmount); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// convert the amount to different currency using Fetch API |
| 63 | +function convertAmount(amount) { |
| 64 | + |
| 65 | + fetchData(`https://v6.exchangerate-api.com/v6/${api}/latest/USD`) |
| 66 | + .then(data => { |
| 67 | + |
| 68 | + const fromRates = data.conversion_rates[fromCurrencySelectTag.value]; |
| 69 | + const toRates = data.conversion_rates[toCurrencySelectTag.value]; |
| 70 | + |
| 71 | + const perRate = (1 * (toRates / fromRates)).toFixed(2); |
| 72 | + const convertedAmount = (amount * (toRates / fromRates)).toFixed(2); |
| 73 | + |
| 74 | + resultTag.style.color = "black"; |
| 75 | + status.textContent = `1 ${fromCurrencySelectTag.value} = ${perRate} ${toCurrencySelectTag.value}`; |
| 76 | + resultTag.textContent = `${amount} ${fromCurrencySelectTag.value} = ${convertedAmount} ${toCurrencySelectTag.value}`; |
| 77 | + |
| 78 | + btn.disabled = false; |
| 79 | + btn.style.color = "black"; |
| 80 | + btn.style.cursor = "pointer"; |
| 81 | + btn.textContent = "Convert"; |
| 82 | + }) |
| 83 | + .catch(error => console.log(`Additional information about error: ${error}`)); |
| 84 | +} |
| 85 | + |
| 86 | +// Fetch API Data with proper validation |
| 87 | +async function fetchData(url) { |
| 88 | + |
| 89 | + try { |
| 90 | + const response = await fetch(url); |
| 91 | + |
| 92 | + if (!response.ok) |
| 93 | + throw new Error(`HTTP error! Status: ${response.status}`); |
| 94 | + |
| 95 | + const data = await response.json(); |
| 96 | + return data; |
| 97 | + } |
| 98 | + catch (error) { |
| 99 | + resultTag.style.color = "red"; |
| 100 | + resultTag.textContent = `Fetch API Error: ${error}`; |
| 101 | + |
| 102 | + throw error; |
| 103 | + } |
| 104 | +} |
0 commit comments