-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (57 loc) · 2.16 KB
/
index.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
const months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
console.log("Corona Tracker");
let time = document.getElementById('time');
time.innerText = formatDate(new Date());
let country = document.getElementById('country');
let cases = document.getElementById('cases');
let deaths = document.getElementById('deaths');
let recovered = document.getElementById('recovered');
let selector = document.getElementById('selector');
let submit = document.getElementById('submit');
async function getData(){
const response = await fetch('https://covid-api.mmediagroup.fr/v1/cases?country=India')
const data = await response.json();
return data;
}
let data = getData();
populate(data)
country.addEventListener('click', () => {
country.style.border = "2px solid rgb(0, 204, 255)";
})
submit.addEventListener('click', () => {
let state = country.value;
state = state[0].toUpperCase() + state.substring(1);
showData(data, state);
});
function formatDate(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
let dateValue = date.getDate() < 9 ? "0" + date.getDate() : date.getDate();
return (months[date.getMonth()]) + " " + dateValue + ", " + date.getFullYear() + ", " + strTime;
}
function populate(data){
data.then(list => {
let states = "";
for(item in list){
if(item == 'All') continue;
states += `<option value="${item}"></option>`
}
selector.innerHTML = states;
})
}
function showData(data, state){
data.then( list => {
let dateTime = new Date(list[state]["updated"]);
let dateTimeStr = formatDate(dateTime);
time.innerText = dateTimeStr;
cases.innerText = list[state]["confirmed"];
deaths.innerText = list[state]["deaths"];
recovered.innerText = list[state]["recovered"];
});
data.catch(reason => console.log("reason is -- " + reason));
}