-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndexController.js
140 lines (126 loc) · 4.98 KB
/
IndexController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
let fromCurrency;
let toCurrency;
let amount;
let query;
let count = 0
let result = null;
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js').then(function(registration) {
console.log('Service worker registration succeeded:', registration);
}).catch(function(error) {
console.log('Service worker registration failed:', error);
});
} else {
console.log('Service workers are not supported.');
}
/// / This works on all devices/browsers, and uses IndexedDBShim as a final fallback
const indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB;
// Open (or create) the database
const open = indexedDB.open("currency-converter", 1);
// Create the schema
open.onupgradeneeded = () => {
let db = open.result;
let store = db.createObjectStore("currency-converter", {keyPath: "name"});
let index = store.createIndex("NameIndex", ["name"]);
};
let storeData = (data,check) => {
if(check){
open.onsuccess = ((data,check) => {
console.log(data);
// Start a new transaction
let db = open.result;
let tx = db.transaction("currency-converter", "readwrite");
let store = tx.objectStore("currency-converter");
let index = store.index("NameIndex");
let getCurrency = store.getAll();
getCurrency.onsuccess = function () {
if(getCurrency.result){
for(const currency in getCurrency.result){
console.log(typeof (getCurrency.result[currency].name));
console.log(typeof (data));
console.log(getCurrency.result[currency].name == data);
if(getCurrency.result[currency].name == data){
const total = parseFloat(getCurrency.result[currency].value.value) * parseFloat(amount);
document.getElementById("currency2").value =total;
result = true;
}
}
}else{
result = false;
}
}
})(data,check);
}else{
open.onsuccess = ((data,check) => {
console.log(data);
// Start a new transaction
let db = open.result;
let tx = db.transaction("currency-converter", "readwrite");
let store = tx.objectStore("currency-converter");
let index = store.index("NameIndex");
let obj = {};
for(const dt in data){
if(data.hasOwnProperty(dt)){
obj={name:dt,value:{currency:dt,value:data[dt].val}};
store.put(obj);
}
}
})(data,check);
}
console.log('bhj',result)
return result;
}
let getCurrency = (() => {
let url = 'https://free.currencyconverterapi.com/api/v5/countries';
fetch(url)
.then((response) => {
return response.json()
}).then((data) => {
const currencies = data.results;
for(let currency in currencies){
if (currencies.hasOwnProperty(currency)) {
document.getElementById("currency-to").innerHTML +=`<option value="${currencies[currency].currencyId}">${currencies[currency].currencyName}</option>`;
document.getElementById("currency-from").innerHTML +=`<option value="${currencies[currency].currencyId}">${currencies[currency].currencyName}</option>`;
}
}
})
.catch(err =>{
console.log('Request failed', err)
});
})();
let convertCurrency = () => {
fromCurrency = document.getElementById("currency-from").value;
toCurrency = document.getElementById("currency-to").value;
amount=document.getElementById("currency").value;
if(amount == '' || amount <=0){
alert('Amount to be converted cannot be zero, empty or negative');
return;
}
fromCurrency = encodeURIComponent(fromCurrency);
toCurrency = encodeURIComponent(toCurrency);
query = fromCurrency + '_' + toCurrency;
let getFromDb = storeData(query,true);
console.log('i',getFromDb);
if(getFromDb){
return;
}else{
let url = 'https://free.currencyconverterapi.com/api/v5/convert?q=' + query + '&compact=y';
fetch(url)
.then((response) => {
return response.json()
}).then((data) => {
storeData(data);
let value = data[query].val;
if (value != undefined) {
const total = parseFloat(value) * parseFloat(amount);
document.getElementById("currency2").value =total;
} else {
const err = new Error("Value not found for " + query);
}
})
.catch(err =>{
console.log('Request failed', err)
});
}
}
let clearField = () =>document.getElementById("currency").value ='';