-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
744 lines (622 loc) · 21.6 KB
/
main.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/**
* This is the main class of the application. It holds the state and all
*/
class CurrencyConverter {
/**
* Initialisation instructions
* @param {LocalDb} localDb The index db class
*/
constructor(localDb) {
this.localDb = localDb; // The local db instance
this.countries = []; // Holds the countries and their details
this.recentConversions = []; // Stores the 5 most recent conversions
this.networkState = 'online'; // The offline state of the application
}
/**
* Flag an error when loading fails
*/
errorLoadingCountries() {
M.toast({
html: `
<span class="toast__message">Could not load currencies. Please reload the page</span>
<i class="material-icons">error</i>
`,
classes: 'toast__error'
});
const $selects = document.querySelectorAll('select');
// Change the message from loading to Error
$selects.forEach($node => {
const $child = $node.firstElementChild;
$child.textContent = 'Error...';
});
// Re init the selects
M.FormSelect.init($selects);
}
/**
* Build the currency selection list.
*/
buildSelectOptions() {
const $selects = document.querySelectorAll('select');
// Create the option elements
const optionElements = [];
// For each country build a node
this.countries.forEach(country => {
const $element = document.createElement('option');
const $textNode = document.createTextNode(
`${country['name']} - ${country['currencyId']}`
);
$element.appendChild($textNode);
$element.setAttribute('value', country['currencyId']);
$element.setAttribute('data-icon', getFlagUrl(country['id']));
$element.className = 'right';
// push to the option elements
optionElements.push($element);
});
// For each node append all the option elements
$selects.forEach(($node, index) => {
optionElements.forEach($element => {
if (index === 0) {
$node.appendChild($element);
} else {
$node.appendChild($element.cloneNode(true));
}
});
// Remove the disabled attribute
$node.removeAttribute('disabled');
// Change the loading options to "Select Currency" and disable them
const $loading = $node.firstElementChild;
$loading.textContent = 'Select currency';
$loading.setAttribute('disabled', true);
$loading.setAttribute('value', 'none');
});
// Reinitialise the dropdowns after appending countries
M.FormSelect.init($selects);
}
/**
* Returns an array of countries sorted by name.
* @param {*} serverRes - The raw response from the server
*/
sortCountriesByName(serverRes) {
const countries = [];
const res = serverRes['results'];
for (const key in res) {
if (res.hasOwnProperty(key)) {
const country = res[key];
countries.push(country);
}
}
return countries.sort((a, b) => {
return a['name'] < b['name'] ? -1 : a['name'] > b['name'] ? 1 : 0;
});
}
/**
* Set event listerners for the form and other elements
*/
setEventListeners() {
// Class
const xChange = new Xchange();
// Elements
const $form = document.getElementById('form');
const $amount = document.getElementById('amount');
const $from = document.getElementById('from');
const $to = document.getElementById('to');
// HTML Collection of options
const $fromOptions = $from.children;
const $toOptions = $to.children;
// Listen for changes in online state of the application
window.addEventListener('online', () => this._updateOnlineStatus());
window.addEventListener('offline', () => this._updateOnlineStatus());
// Amount input
$amount.addEventListener('input', event => {
const amountValue = event.target.value;
const fromValue = $from.options[from.selectedIndex].value;
const toValue = $to.options[to.selectedIndex].value;
this._toggleSubmitButton(amountValue, fromValue, toValue);
});
// From input
$from.addEventListener('change', event => {
const fromValue = event.target.value;
const amountValue = $amount.value;
const toValue = $to.options[to.selectedIndex].value;
// Enable / Disable the submit button
this._toggleSubmitButton(amountValue, fromValue, toValue);
// Disable the selected option so you can't change from and to the same currency
for (let i = 1; i < $toOptions.length; i++) {
// We start at index 1, ignoring the original
const $option = $toOptions[i];
if ($option.value === fromValue) {
$option.setAttribute('disabled', true);
} else {
$option.removeAttribute('disabled');
}
}
// Re initialise the selection
M.FormSelect.init($to); // Performance issue?. Suspect it's the source of the screen jitter bug
});
// To input
$to.addEventListener('change', event => {
const toValue = event.target.value;
const amountValue = $amount.value;
const fromValue = $from.options[$from.selectedIndex].value;
this._toggleSubmitButton(amountValue, fromValue, toValue);
// Disable selected option on from selection to avoid changing to same currency
for (let i = 1; i < $fromOptions.length; i++) {
// We start at index 1, ignoring the original
const $option = $fromOptions[i];
if ($option.value === toValue) {
$option.setAttribute('disabled', true);
} else {
$option.removeAttribute('disabled');
}
}
// Re initialise the selection
M.FormSelect.init($from);
});
// Form submission
$form.addEventListener('submit', event => {
// Prevent default actions
event.preventDefault();
// open the modal
const modal = document.getElementById('conversion-modal');
const modalInstance = M.Modal.init(modal, {
onCloseStart: () => this._resetForm(),
onCloseEnd: () => {
this._resetModalClasses();
this._updateRecentConversions();
}
});
modalInstance.open();
const amount = $amount.value;
const fromValue = $from.options[$from.selectedIndex].value;
const toValue = $to.options[$to.selectedIndex].value;
xChange
.getRate(fromValue, toValue)
.then(res => {
const key = `${fromValue}_${toValue}`;
const rate = res[key];
this._showConversion(amount, fromValue, toValue, rate, modalInstance);
this._addToRecent(key, rate, amount);
})
.catch(err => {
this._errorConverting(modalInstance);
});
});
}
/**
* Initiate the recent conversions
*/
async initRecentConversions() {
try {
// Clean up the db
await this.localDb.cleanUp('recent', 5, 'by-date');
// Get the 5 most recent coverserions
const results = await this.localDb.getAllItems('recent', 'by-date');
this.recentConversions = results;
// Build the recent conversions
if (results.length > 0) {
const $aside = document.getElementById('aside-content');
const $placeholder = document.getElementById('aside-placeholder');
// Remove the placeholder
$aside.removeChild($placeholder);
// for each conversion build an html element and add it to the DOM
results.forEach(conversion => {
// Get the values
const from = conversion.currencies.split('_')[0];
const to = conversion.currencies.split('_')[1];
const amount = conversion.amount;
const rate = conversion.rate;
const timestamp = new Date(conversion.timestamp);
const div = document.createElement('div');
const html = `
<div class="recent-conversion__from">
${amount} ${from}
</div>
<i class="material-icons recent-conversion__icon">chevron_right</i>
<div class="recent-conversion__to">
${(amount * rate).toFixed(2)} ${to}
</div>
<div class="recent-conversion__date">
(${timestamp.toLocaleDateString()} - ${timestamp.toLocaleTimeString()})
</div>
`;
// Build the class properties
div.className = 'recent-conversion animated fadeInDown';
div.innerHTML = html;
// If the aside has no children append this element. Otherwise insert it after the first element
if ($aside.firstElementChild === null) {
$aside.appendChild(div);
} else {
$aside.insertBefore(div, $aside.firstElementChild);
}
});
}
} catch (err) {
// HAndle error
console.log(err);
}
}
/**
* Updates the recentconversions list
*/
async _updateRecentConversions() {
try {
// Clean up the db and don't execute the next line until
await this.localDb.cleanUp('recent', 5, 'by-date');
// Get the 5 most recent coverserions
const results = await this.localDb.getAllItems('recent', 'by-date');
if (this.recentConversions.length > 0) {
const lastCurrentItem = this.recentConversions[
this.recentConversions.length - 1
];
const lastDbItem = results[results.length - 1];
// If nothing has changed. Bail
if (
lastCurrentItem.currencies === lastDbItem.currencies &&
lastCurrentItem.amount === lastDbItem.amount
)
return;
}
this.recentConversions = results;
// Capture the parent node
const $aside = document.getElementById('aside-content');
// The result to insert
const toInsert = results[results.length - 1];
const from = toInsert.currencies.split('_')[0];
const to = toInsert.currencies.split('_')[1];
const amount = toInsert.amount;
const rate = toInsert.rate;
const timestamp = new Date(toInsert.timestamp);
const div = document.createElement('div');
const html = `
<div class="recent-conversion__from">
${amount} ${from}
</div>
<i class="material-icons recent-conversion__icon">chevron_right</i>
<div class="recent-conversion__to">
${(amount * rate).toFixed(2)} ${to}
</div>
<div class="recent-conversion__date">
(${timestamp.toLocaleDateString()} - ${timestamp.toLocaleTimeString()})
</div>
`;
// Build the class properties
div.className = 'recent-conversion animated fadeInDown';
div.innerHTML = html;
// Query the recent conversions
const $recentConversions = document.querySelectorAll(
'.recent-conversion'
);
if ($recentConversions.length === 0) {
const $placeholder = document.getElementById('aside-placeholder');
$aside.removeChild($placeholder);
$aside.appendChild(div);
} else if ($recentConversions.length < 5) {
const $firstChild = $aside.firstChild;
$aside.insertBefore(div, $firstChild);
return;
} else {
// Remove the last node
const $lastChild = $aside.lastChild;
$aside.removeChild($aside.lastChild);
// Insert the new node at the top
const $firstChild = $aside.firstChild;
$aside.insertBefore(div, $firstChild);
}
} catch (err) {
// HAndle error
console.log(err);
}
}
/**
* Updates the online status of the application
*/
_updateOnlineStatus() {
const $warn = document.getElementById('warning');
const online = navigator.onLine;
$warn.className = online ? 'warning--hidden' : 'warning';
this.networkState = online ? 'online' : 'offline';
}
/**
* Toogles the submit button disabled state based on the parameters
* @param {number} amountValue The value in the amount field
* @param {string} fromValue The value of the from select element
* @param {string} toValue The value of the to select element
*/
_toggleSubmitButton(amountValue, fromValue, toValue) {
const $submit = document.getElementById('submit-button');
if (amountValue > 0 && fromValue !== 'none' && toValue !== 'none') {
$submit.removeAttribute('disabled');
} else {
$submit.setAttribute('disabled', true);
}
}
/**
* Handles the data population and class manipulation of the modal elements
* @param {number} amount The amount we're converting
* @param {string} fromValue The currency we're converting from
* @param {string} toValue The currency we're converting to
* @param {number} rate The exchange rate
*/
_showConversion(amount, fromValue, toValue, rate) {
// Elements
const $preloader = document.getElementById('preloader');
const $data = document.getElementById('data');
const $convertedFrom = document.getElementById('converted-from');
const $convertedTo = document.getElementById('converted-to');
const $rate = document.getElementById('rate');
// Convert amount and round off to 3 dp
const convertedAmount = (amount * rate).toFixed(2);
// Insert the values in their respective fields
$convertedFrom.textContent = `${amount} ${fromValue}`;
$convertedTo.textContent = `${convertedAmount} ${toValue}`;
$rate.textContent = `${rate}`;
// Hide the preloader and show the data
$preloader.className = 'modal__preloader--hidden';
$data.className = 'modal__data--visible animated fadeIn';
}
/**
* Closes the modal instance and shows an error toast message
* @param {*} modalInstance The instance of the modal so we can close it
*/
_errorConverting(modalInstance) {
modalInstance.close();
M.toast({
html: `
<span class="toast__message">Could not convert. Please try again</span>
<i class="material-icons">error</i>
`,
classes: 'toast__error'
});
}
/**
* This resets the modal classes on close end
*/
_resetModalClasses() {
const $preloader = document.getElementById('preloader');
const $data = document.getElementById('data');
$preloader.className = 'modal__preloader';
$data.className = 'modal__data';
}
/**
* Resets the form to a blank state if the checkbox is ticked
*/
_resetForm() {
const $clear = document.getElementById('clear');
if ($clear.checked) {
const $amount = document.getElementById('amount');
const $selects = document.querySelectorAll('select');
const $from = $selects[0];
const $to = $selects[1];
const $submit = document.getElementById('submit-button');
const $fromOptions = $from.children;
const $toOptions = $to.children;
// Reset the values
$amount.value = 0;
$from.selectedIndex = 0;
$to.selectedIndex = 0;
// remove the disabled attribute from the children of both selections
for (let i = 1; i < $fromOptions.length; i++) {
// We start at index 1, ignoring the original
const $fromOption = $fromOptions[i];
const $toOption = $toOptions[i];
$fromOption.removeAttribute('disabled');
$toOption.removeAttribute('disabled');
}
// Reinitialise the selections
M.FormSelect.init($selects);
// Deactivate submit button
$submit.setAttribute('disabled', true);
}
}
/**
* Registers the service worker
*/
registerServiceWorker() {
if (navigator.serviceWorker) {
// Register the SW
navigator.serviceWorker.register('./sw.js');
}
}
/**
*
* @param {string} key The key to store in the database
* @param {number} rate The rate of the key
* @param {number} string The amount that was converted
*/
_addToRecent(key, rate, amount) {
const obj = {
currencies: key,
rate: rate,
amount: +amount,
timestamp: Date.now()
};
// Store the item in the database
this.localDb.addItem('recent', obj);
}
}
/**
* This class handles all the interactions with the database
*/
class LocalDb {
constructor() {
this._dbPromise = idb.open('x-change', 1, upgradeDb => {
const oldVersion = upgradeDb.oldVersion;
switch (oldVersion) {
case 0:
const recentStore = upgradeDb.createObjectStore('recent', {
keyPath: 'currencies'
});
// create index
recentStore.createIndex('by-date', 'timestamp');
}
});
}
/**
* This function deletes all objects from a store except the newest specified number
* @param {string} store The store to carry out the operation on
* @param {number} numberToSave The number of items to save
* @param {string} [index] The index to retrieve
*/
async cleanUp(store, numberToSave, index) {
const db = await this._dbPromise;
const tx = db.transaction(store, 'readwrite');
const objectStore = tx.objectStore(store);
// Get the cursor depending on whether or not an index is provided
const cursor = index
? await objectStore.index(index).openCursor(null, 'prev')
: await objectStore.openCursor(null, 'prev');
// If there's no cursor, bail
if (!cursor) return;
// Advance the cursor
const advancedCursor = await cursor.advance(numberToSave);
// Declare the delete function
const deleteRest = function(cursor) {
if (!cursor) return;
cursor.delete();
cursor.continue().then(deleteRest);
};
// Call the delete function
deleteRest(advancedCursor);
}
/**
* This method retireves items from the IDB database
*
* @param {string} store The store to retrieve the item from
* @param {string} key The key of the item to retrieve
*/
async getItem(store, key) {
try {
const db = await this._dbPromise;
const tx = db.transaction(store);
const objectStore = tx.objectStore(store);
// Return the value
return objectStore.get(key);
} catch (err) {
// Handle the error
// console.log(err);
}
}
/**
* This method retrieves all the values in the specified store. Also allows an optional index
* @param {string} store The name of the store to retrieve all items for
* @param {string} [index] The optional index to query by
*/
async getAllItems(store, index) {
try {
const db = await this._dbPromise;
const tx = db.transaction(store);
const objectStore = tx.objectStore(store);
if (index) {
const storeIndex = objectStore.index(index);
return storeIndex.getAll();
}
return objectStore.getAll();
} catch (err) {
// Handle error
console.log(err);
}
}
/**
* This adds a key value pair into the database
* @param {string} store - The store to add the item to
* @param {*} value - The value to insert into the database
* @param {string} [key] - The optional key of the item to put into the
*/
async addItem(store, value, key) {
try {
const db = await this._dbPromise;
const tx = db.transaction(store, 'readwrite');
const objectStore = tx.objectStore(store);
if (key) {
objectStore.put(value, key);
} else {
objectStore.put(value);
}
return tx.complete;
} catch (err) {
// Handle the error here
// console.log(err);
}
}
}
// ======================================================================//
// API Interactions
// ======================================================================//
/**
* Manages the curency converter API Interactions
*/
class Xchange {
constructor() {
this._BASE_URL = 'https://free.currencyconverterapi.com/api/v5';
}
/**
* Gets a list of countries including currency information and Country codes
*/
getCountries() {
return fetch(`${this._BASE_URL}/countries`)
.then(res => {
if (!res.ok) {
throw Error(res.statusText);
}
return res;
})
.then(res => res.json());
}
/**
* Returns the rate of the currency conversion
* @param {string} from The currency to convert from
* @param {string} to The currency to conver to
*/
getRate(from = 'USD', to = 'ZMW') {
const key = `${from}_${to}`;
return fetch(`${this._BASE_URL}/convert?q=${key}&compact=ultra`)
.then(res => {
if (!res.ok) {
throw Error(res.statusText);
}
return res;
})
.then(res => res.json());
}
}
/**
* Retrieves the URL for a flag of a country given the alpha 2 country code
* @param {string} code The alpha 2 country code. Default is AD
*/
function getFlagUrl(code = 'AD') {
// This is a disaster. Forced to use a proxy that returns content at 10req/second... Once cached though, things move quick
return `https://thingproxy.freeboard.io/fetch/http://www.countryflags.io/${code.toLowerCase()}/flat/32.png`;
}
// ======================================================================//
// Init Code
// ======================================================================//
/**
* This function runs the initialisation logic. It's also the main 'entry point' of the application
*/
function init() {
const db = new LocalDb();
const xChange = new Xchange();
const cc = new CurrencyConverter(db);
// Other Init Logic
document.addEventListener('DOMContentLoaded', () => {
// Register the SW immediately
cc.registerServiceWorker();
// Initialise the selectors on document ready with the "Loading options"
const $elems = document.querySelectorAll('select');
M.FormSelect.init($elems);
// Request for the countries
xChange
.getCountries()
.then(res => {
cc.countries = cc.sortCountriesByName(res);
cc.buildSelectOptions();
cc.setEventListeners();
cc.initRecentConversions();
})
.catch(err => {
cc.errorLoadingCountries();
});
});
}
// Call Init;
init();