diff --git a/app.js b/app.js index 0310bf1..ce48655 100644 --- a/app.js +++ b/app.js @@ -40,6 +40,7 @@ async function initDatabase() { // For any collection, init here users = await getCollection(db, "users"); plans = await getCollection(db, "plans"); + currencies = await getCollection(db, "currencies"); } /*** ROUTINGS ***/ diff --git a/src/public/json/countries.json b/src/public/json/countries.json new file mode 100644 index 0000000..039a74c --- /dev/null +++ b/src/public/json/countries.json @@ -0,0 +1,132 @@ +{ + "countries": [ + { + "country": "United States", + "currency_abbreviation": "USD" + }, + { + "country": "Eurozone", + "currency_abbreviation": "EUR" + }, + { + "country": "United Kingdom", + "currency_abbreviation": "GBP" + }, + { + "country": "Japan", + "currency_abbreviation": "JPY" + }, + { + "country": "Switzerland", + "currency_abbreviation": "CHF" + }, + { + "country": "Canada", + "currency_abbreviation": "CAD" + }, + { + "country": "Australia", + "currency_abbreviation": "AUD" + }, + { + "country": "China", + "currency_abbreviation": "CNY" + }, + { + "country": "India", + "currency_abbreviation": "INR" + }, + { + "country": "Russia", + "currency_abbreviation": "RUB" + }, + { + "country": "Brazil", + "currency_abbreviation": "BRL" + }, + { + "country": "Mexico", + "currency_abbreviation": "MXN" + }, + { + "country": "South Korea", + "currency_abbreviation": "KRW" + }, + { + "country": "Indonesia", + "currency_abbreviation": "IDR" + }, + { + "country": "Bulgaria", + "currency_abbreviation": "BGN" + }, + { + "country": "Czech Republic", + "currency_abbreviation": "CZK" + }, + { + "country": "Denmark", + "currency_abbreviation": "DKK" + }, + { + "country": "Hong Kong", + "currency_abbreviation": "HKD" + }, + { + "country": "Hungary", + "currency_abbreviation": "HUF" + }, + { + "country": "Israel", + "currency_abbreviation": "ILS" + }, + { + "country": "Iceland", + "currency_abbreviation": "ISK" + }, + { + "country": "Malaysia", + "currency_abbreviation": "MYR" + }, + { + "country": "Norway", + "currency_abbreviation": "NOK" + }, + { + "country": "New Zealand", + "currency_abbreviation": "NZD" + }, + { + "country": "Philippines", + "currency_abbreviation": "PHP" + }, + { + "country": "Poland", + "currency_abbreviation": "PLN" + }, + { + "country": "Romania", + "currency_abbreviation": "RON" + }, + { + "country": "Sweden", + "currency_abbreviation": "SEK" + }, + { + "country": "Singapore", + "currency_abbreviation": "SGD" + }, + { + "country": "Thailand", + "currency_abbreviation": "THB" + }, + { + "country": "Turkey", + "currency_abbreviation": "TRY" + }, + { + "country": "South Africa", + "currency_abbreviation": "ZAR" + } + ] +} \ No newline at end of file diff --git a/src/public/scripts/exchangeRate.js b/src/public/scripts/exchangeRate.js new file mode 100644 index 0000000..61cad77 --- /dev/null +++ b/src/public/scripts/exchangeRate.js @@ -0,0 +1,7 @@ +function convert(from) { + fetch(`https://api.frankfurter.dev/v1/latest?base=${from}`) + .then((resp) => resp.json()) + .then((data) => { + console.log(data); + }); +} \ No newline at end of file diff --git a/src/public/scripts/geolocation.js b/src/public/scripts/geolocation.js index ebe001b..74e5652 100644 --- a/src/public/scripts/geolocation.js +++ b/src/public/scripts/geolocation.js @@ -1,24 +1,43 @@ +async function diffCountries() { + const res = await fetch("/static/json/countries.json"); + const data = await res.json(); + + return data.countries; +} + function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(success, error); } } -function success(position) { +async function success(position) { const { latitude, longitude } = position.coords; + let countryCurrency; - fetch("api/location" , { + const countries = await diffCountries(); + + const res = await fetch("api/location" , { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ latitude, longitude }) - }).then(res => res.json()) - .then(data => { - console.log(data.results[0].formatted_address); - }) + }); + + const data = await res.json(); + + countries.forEach(country => { + if (country.country === data.results[0].formatted_address) { + countryCurrency = country.currency_abbreviation; + + convert(countryCurrency); + } + }); } function error(err) { console.error("Geolocation error: ", err); } + +getLocation(); \ No newline at end of file diff --git a/src/util/currencies.js b/src/util/currencies.js new file mode 100644 index 0000000..e69de29 diff --git a/src/views/partials/footer.ejs b/src/views/partials/footer.ejs index 0c2379c..ed0882c 100644 --- a/src/views/partials/footer.ejs +++ b/src/views/partials/footer.ejs @@ -1,11 +1,11 @@ - + \ No newline at end of file