forked from zatharox/Hackathon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
201 lines (178 loc) · 5.16 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* ---------------------------------------------- */
/* CODE EXPLAINED TUTORIALS */
/* www.youtube.com/CodeExplained */
/* ---------------------------------------------- */
// SELECT ALL ELEMENTS
const country_name_element = document.querySelector(".country .name");
const total_cases_element = document.querySelector(".total-cases .value");
const new_cases_element = document.querySelector(".total-cases .new-value");
const recovered_element = document.querySelector(".recovered .value");
const new_recovered_element = document.querySelector(".recovered .new-value");
const deaths_element = document.querySelector(".deaths .value");
const new_deaths_element = document.querySelector(".deaths .new-value");
const ctx = document.getElementById("axes_line_chart").getContext("2d");
// APP VARIABLES
let app_data = [],
cases_list = [],
recovered_list = [],
deaths_list = [],
deaths = [],
formatedDates = [];
// GET USERS COUNTRY CODE
fetch("https://api.ipgeolocation.io/ipgeo?apiKey=14c7928d2aef416287e034ee91cd360d")
.then((res) => {
return res.json();
})
.then((data) => {
let country_code = data.country_code2;
let user_country;
country_list.forEach((country) => {
if (country.code == country_code) {
user_country = country.name;
}
});
fetchData(user_country);
});
/* ---------------------------------------------- */
/* FETCH API */
/* ---------------------------------------------- */
function fetchData(country) {
user_country = country;
country_name_element.innerHTML = "Loading...";
(cases_list = []),
(recovered_list = []),
(deaths_list = []),
(dates = []),
(formatedDates = []);
var requestOptions = {
method: "GET",
redirect: "follow",
};
const api_fetch = async (country) => {
await fetch(
"https://api.covid19api.com/total/country/" + country + "/status/confirmed",
requestOptions
)
.then((res) => {
return res.json();
})
.then((data) => {
data.forEach((entry) => {
dates.push(entry.Date);
cases_list.push(entry.Cases);
});
});
await fetch(
"https://api.covid19api.com/total/country/" + country + "/status/recovered",
requestOptions
)
.then((res) => {
return res.json();
})
.then((data) => {
data.forEach((entry) => {
recovered_list.push(entry.Cases);
});
});
await fetch(
"https://api.covid19api.com/total/country/" + country + "/status/deaths",
requestOptions
)
.then((res) => {
return res.json();
})
.then((data) => {
data.forEach((entry) => {
deaths_list.push(entry.Cases);
});
});
updateUI();
};
api_fetch(country);
}
// UPDATE UI FUNCTION
function updateUI() {
updateStats();
axesLinearChart();
}
function updateStats() {
const total_cases = cases_list[cases_list.length - 1];
const new_confirmed_cases = total_cases - cases_list[cases_list.length - 2];
const total_recovered = recovered_list[recovered_list.length - 1];
const new_recovered_cases = total_recovered - recovered_list[recovered_list.length - 2];
const total_deaths = deaths_list[deaths_list.length - 1];
const new_deaths_cases = total_deaths - deaths_list[deaths_list.length - 2];
country_name_element.innerHTML = user_country;
total_cases_element.innerHTML = total_cases;
new_cases_element.innerHTML = `+${new_confirmed_cases}`;
recovered_element.innerHTML = total_recovered;
new_recovered_element.innerHTML = `+${new_recovered_cases}`;
deaths_element.innerHTML = total_deaths;
new_deaths_element.innerHTML = `+${new_deaths_cases}`;
// format dates
dates.forEach((date) => {
formatedDates.push(formatDate(date));
});
}
// UPDATE CHART
let my_chart;
function axesLinearChart() {
if (my_chart) {
my_chart.destroy();
}
my_chart = new Chart(ctx, {
type: "line",
data: {
datasets: [
{
label: "Cases",
data: cases_list,
fill: false,
borderColor: "#FFF",
backgroundColor: "#FFF",
borderWidth: 1,
},
{
label: "Recovered",
data: recovered_list,
fill: false,
borderColor: "#009688",
backgroundColor: "#009688",
borderWidth: 1,
},
{
label: "Deaths",
data: deaths_list,
fill: false,
borderColor: "#f44336",
backgroundColor: "#f44336",
borderWidth: 1,
},
],
labels: formatedDates,
},
options: {
responsive: true,
maintainAspectRatio: false,
},
});
}
// FORMAT DATES
const monthsNames = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
function formatDate(dateString) {
let date = new Date(dateString);
return `${date.getDate()} ${monthsNames[date.getMonth()]}`;
}