-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
116 lines (113 loc) · 3.63 KB
/
script.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
const totalCases = document.querySelector('.total-cases');
const recoveryCases = document.querySelector('.recovery-cases');
const deathCases = document.querySelector('.death-cases');
const dataTable = document.querySelector('.data-table-body');
const changeSort = document.querySelector('.sort');
const changeSortByCountry = document.querySelector('.sort-alphabet');
const tableInputSearch = document.querySelector('#tableSearch');
let dataFixs;
//--------------------------------------------
//get data world (3 summary)
function getWorldData() {
return fetch('https://api.covid19api.com/world/total')
.then((result) => {
if (!result.ok) {
throw new Error(result.statusText);
}
return result.json();
})
.then((result) => {
return result;
});
}
//display world data (3 summary)
async function displayWorldData() {
try {
const result = await getWorldData();
totalCases.innerHTML = numberWithCommas(result.TotalConfirmed);
recoveryCases.innerHTML = numberWithCommas(result.TotalRecovered);
deathCases.innerHTML = numberWithCommas(result.TotalDeaths);
} catch (err) {
alert(err);
}
}
// ---------------------------------------------
//get summary data world almost all
function getSummaryData() {
return fetch('https://api.covid19api.com/summary')
.then((result) => {
if (!result.ok) {
throw new Error(result.statusText);
}
return result.json();
})
.then((result) => result);
}
//display summary data almost all
async function displaySummaryData() {
try {
const dataSummary = await getSummaryData();
dataFixs = dataSummary.Countries;
dataTable.innerHTML = getDataTable(dataFixs);
} catch (err) {
alert(err);
}
}
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
function getDataTable(dataFixs) {
return dataFixs
.map((dataFix, i) => {
return ` <tr>
<th scope="row">${i + 1}</th>
<td>${dataFix.Country}</td>
<td>${numberWithCommas(dataFix.TotalConfirmed)}</td>
<td>+ ${numberWithCommas(dataFix.NewConfirmed)}</td>
<td>${numberWithCommas(dataFix.TotalDeaths)}</td>
<td>${numberWithCommas(dataFix.TotalRecovered)}</td>
<td>${dataFix.Date}</td>
</tr>`;
})
.join('');
}
function handleSort() {
if (this.value == 'sortlargest') {
dataFixs.sort((a, b) => (a.TotalConfirmed < b.TotalConfirmed ? 1 : -1));
}
if (this.value == 'sortsmalles') {
dataFixs.sort((a, b) => (a.TotalConfirmed > b.TotalConfirmed ? 1 : -1));
}
dataTable.innerHTML = getDataTable(dataFixs);
}
function handleSortByCountry() {
if (this.value == 'a_z') {
dataFixs.sort((a, b) => (a.Country > b.Country ? 1 : -1));
}
if (this.value == 'z_a') {
dataFixs.sort((a, b) => (a.Country < b.Country ? 1 : -1));
}
dataTable.innerHTML = getDataTable(dataFixs);
}
function handleSearch() {
let txtValue;
const filter = this.value.toUpperCase();
const table = document.querySelector('.data-table-body');
const trs = table.querySelectorAll('tr');
trs.forEach((tr) => {
const td = tr.querySelectorAll('td')[0]; // take the index td always to 0 = country
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr.style.display = '';
} else {
tr.style.display = 'none';
}
}
});
}
changeSort.addEventListener('change', handleSort);
changeSortByCountry.addEventListener('change', handleSortByCountry);
tableInputSearch.addEventListener('keyup', handleSearch);
window.addEventListener('load', displayWorldData);
window.addEventListener('load', displaySummaryData);