-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
226 lines (182 loc) · 6.67 KB
/
script.js
File metadata and controls
226 lines (182 loc) · 6.67 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// script to register the service worker, create the database , store currencies and rates in the
// database
function openDatabase() {
// If the browser doesn't support service worker, we don't care about having a database
if (!navigator.serviceWorker) {
return Promise.resolve();
}
return idb.open('kimconvert', 1, function(upgradeDb) {
let store = upgradeDb.createObjectStore('currency', {keyPath: "id"});
let store2 = upgradeDb.createObjectStore('rate', {keyPath: "id"});
});
}
let dbPromise = openDatabase();
// sw registration
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js').then(function(reg) {
if(reg.installing) {
console.log('Service worker installing');
} else if(reg.waiting) {
console.log('Service worker installed');
} else if(reg.active) {
console.log('Service worker active');
}
}).catch(function(error) {
// registration failed
console.log('Registration failed with ' + error);
});
}
// get all currencies available
// first check db and then get from nextwork and store them in database
getCurrencies();
fetchCurrency();
function getCurrencies(){
dbPromise.then(db => {
let tx = db.transaction('currency', 'readwrite');
let store = tx.objectStore('currency');
return store.openCursor();
}).then(function readCusrsor(cusr){
if (!cusr){
return;
}
select_orig_currency = document.getElementById('from_currency');
select_to_currency = document.getElementById('to_currency');
let id = cusr.value.id;
let name = cusr.value.name;
select_orig_currency.add(new Option(name, id));
select_to_currency.add(new Option(name, id));
cusr.continue().then(readCusrsor);
})
}
function fetchCurrency(){
fetch('https://free.currencyconverterapi.com/api/v5/currencies')
.then(response=> {
return response.json();
}).then(data =>{
// if no data, get currencies from database
if(!data){
return;
}
// console.log(data);
// const currencies = data.results;
// for( const currency in currencies){
// console.log(currency);
// }
// console.log(Object.entries(data.results));
const currencyArray = Object.entries(data.results);
let testmap = new Map();
for(const currency of currencyArray){
// console.log(currency);
let currencyName = currency[1].currencyName;
// let currencySymbole = currency[1].currencySymboll
let currencyId = currency[1].id;
testmap.set(currency[1].id, currency[1].currencyName);
// currencies.add(currencyId, currencyName);
// console.log(currencyId);
// console.log(currencyName);
// console.log(testmap.size);
}
return testmap;
})
.then(currencyMap =>{
select_orig_currency = document.getElementById('from_currency');
select_to_currency = document.getElementById('to_currency');
// console.log(currencyMap.size);
for (const curr of currencyMap) {
let[id, name] = curr;
// console.log(id);
// console.log(name);
select_orig_currency.add(new Option(name, id));
select_to_currency.add(new Option(name, id));
}
// add this to the indexedBD the currency object store
dbPromise.then(db => {
let tx = db.transaction('currency', 'readwrite');
let store = tx.objectStore('currency');
for (const curr of currencyMap) {
let[id, name] = curr;
let obj = {id, name};
store.put(obj);
}
})
})
// .catch(err => {
// console.log("There was an error .", err);
// })
}
// submit request
// https://free.currencyconverterapi.com/api/v5/convert?q=USD_PHP
function submitQuery(fromField, toField){
urlQuery = 'https://free.currencyconverterapi.com/api/v5/convert?q='
this.fromField = fromField;
this.toField = toField;
let tofromPart = fromField+'_'+toField;
queryString = urlQuery + tofromPart;
console.log(queryString);
// console.log(queryString);
fetch(queryString)
.then(response =>{
return response.json();
}).then(data => {
// if no data, return
if(!data){
return;
}
// console.log(data);
const qResults = Object.entries(data.results);
// console.log(qResults);
// console.log(qResults[0][1].val);
let rate = qResults[0][1].val;
let id = qResults[0][1].id;
let to = qResults[0][1].to;
let fr = qResults[0][1].fr;
// Store results in rate object
dbPromise.then(db => {
let tx = db.transaction('rate', 'readwrite');
let store = tx.objectStore('rate');
let obj = {id, rate, to, fr};
store.put(obj);
})
return rate;
}).then(rate => {
let amountField = document.getElementById('from_amount').value;
let convertedValue = rate * amountField;
document.getElementById('to_amount').value = convertedValue;
}).catch(()=>{
let rateDB = getRateFromDatabase(tofromPart);
console.log(`${rateDB} is value from database`);
if(rateDB === 0){
document.getElementById('to_amount').value = "Need Internet";
return;
}
let amountFieldDb = document.getElementById('from_amount').value;
let convertedValueDb = rateDB * amountFieldDb;
document.getElementById('to_amount').value = convertedValueDb;
})
}
//code to handle pressing the submit button
const form_element = document.getElementById('currency-form');
form_element.addEventListener('submit', event => {
event.preventDefault();
let fromField = document.getElementById('from_currency').value;
let toField = document.getElementById('to_currency').value;
console.log(`${fromField} and ${toField}`);
submitQuery(fromField, toField);
});
function getRateFromDatabase(tofromPart) {
this.tofromPart = tofromPart;
let theRate = 0;
dbPromise.then(db => {
let tx = db.transaction('rate', 'readwrite');
let store = tx.objectStore('rate');
return store.get(tofromPart);
}).then(obj => {
if(!obj){
console.log("Nothig got from db");
return 0;
}
console.log(obj.rate);
theRate = obj.rate;
})
return theRate;
}