-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
96 lines (83 loc) · 3 KB
/
app.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
// Select our DOM elements
const form = document.querySelector("#form");
let input = document.querySelector("#input");
let header = document.querySelector("#header");
const button = document.querySelector("#button");
const global = document.querySelector("#global");
const tableArea = document.querySelector("#table");
const tableDiv = document.querySelector("#table-div");
const tableBody = document.querySelector("#table-body");
const allCountries = document.querySelector("#all-countries");
// Event Listeners
form.addEventListener("submit", (e) => {
e.preventDefault();
const country = input.value;
// Fetch by country
fetch(`https://api.covid19api.com/country/${country}`)
.then((res) => res.json())
.then((data) => {
data = data[data.length - 1];
if (data !== undefined) {
header.style.display = "none";
input.value = "";
tableDiv.style.visibility = "visible";
tableBody.innerHTML = `
<tr>
<th style="text-transformation: capitalize" scope="row">${data.Country}</th>
<td>${data.Deaths}</td>
<td>${data.Confirmed}</td>
<td>${data.Active}</td>
<td>${data.Recovered}</td>
</tr>
`;
} else {
tableDiv.style.visibility = "hidden";
header.style.display = "block";
header.textContent = "Country Not Found...Try again!";
}
});
});
// Fetch Global
global.addEventListener("click", () => {
fetch("https://api.covid19api.com/summary")
.then((res) => res.json())
.then((data) => {
data = data.Global;
header.style.display = "none";
input.value = "";
tableDiv.style.visibility = "visible";
tableBody.innerHTML = `
<tr>
<th style="text-transformation: capitalize" scope="row">Global</th>
<td>${data.TotalDeaths}</td>
<td>${data.TotalConfirmed}</td>
<td>uknown</td>
<td>${data.TotalRecovered}</td>
</tr>
`;
});
});
// Fetch All Countries
allCountries.addEventListener("click", () => {
fetch("https://api.covid19api.com/summary")
.then((res) => res.json())
.then((data) => {
data = data.Countries;
header.style.display = "none";
input.value = "";
tableDiv.style.visibility = "visible";
const countriesArr = [];
tableBody.innerHTML = data
.map((country) => {
return `<tr>
<th scope="row">${country.Country}</th>
<td>${country.TotalDeaths}</td>
<td>${country.TotalConfirmed}</td>
<td>uknown</td>
<td>${country.TotalRecovered}</td>
</tr>
`;
})
.join("");
});
});