-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcurrency.ts
48 lines (40 loc) · 1.75 KB
/
currency.ts
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
import './currency.html';
export class Currency {
constructor() {
document.getElementsByName('targetCurrency')
.forEach(e => e.addEventListener('click', this.currencyChanged));
document.getElementById('amount').addEventListener('input' , this.amountChanged);
}
private amountChanged = ($event: Event) : void => {
const targetCurrency: string = this.getSelectedCurrency();
const amount : number = +(<HTMLInputElement>$event.target).value;
console.log(`cur: ${targetCurrency} amount: ${amount} `);
this.getFromServer(amount, targetCurrency );
}
private getSelectedCurrency = (): string => {
const currencies = document.getElementsByName('targetCurrency');
for (let i = 0; i < currencies.length; i++ ) {
const element: HTMLInputElement = (<HTMLInputElement> currencies[i]);
if( element.checked ) return element.value;
}
return '';
}
private getFromServer = (amount: number, targetCurrency: string) : void => {
fetch(`http://localhost:3000/convert/usd/${targetCurrency}/${amount}`)
.then(response => {
if (!response.ok) throw response.status;
else {
response.json().then(data => {
console.log(data);
(<HTMLInputElement>(document.getElementById('convertedAmounth'))).value = data.result;
});
}
});
}
private currencyChanged = ($event: Event): void => {
const targetCurrency = (<HTMLInputElement>$event.target).value;
const amount = +(<HTMLInputElement>document.getElementById('amount')).value;
console.log(`amount: ${amount} , currency: ${targetCurrency}`);
this.getFromServer(amount, targetCurrency);
}
}