-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
61 lines (46 loc) · 1.38 KB
/
script.js
File metadata and controls
61 lines (46 loc) · 1.38 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
const APIURL = 'https://covid19.mathdro.id/api/'
const btn = document.querySelector('.btn')
const search = document.getElementById('search')
const main = document.getElementById('main')
let nameCountry
async function getCountry(country) {
try{
const {data} = await axios(APIURL + 'countries/' + country)
createCountryCard(data)
} catch(err){
createErrorCard(nameCountry + ' was not found')
}
}
function createCountryCard(country) {
const cardHTML = `
<div class="card" id="card">
<div class="name-country">
<h2>${nameCountry}</h2>
</div>
<div class="covid-info">
<h3>Confirmed: ${country.confirmed.value}</h3>
<h3>Recovered: ${country.recovered.value}</h3>
<h3>Deaths: ${country.deaths.value}</h3>
</div>
<p>Last Update: ${country.lastUpdate.replace(/-/g, '/').replace(/T/, ' ').replace(/\..+/, '')}</p>
</div>
`
main.innerHTML = cardHTML
}
function createErrorCard(msg) {
const cardHTML = `
<div class="card">
<h3>${msg}</h3>
</div>
`
main.innerHTML = cardHTML
}
btn.addEventListener('click', (e) => {
e.preventDefault()
const country = search.value
nameCountry = country
if(country) {
getCountry(country)
search.value = ''
}
})