-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
224 lines (204 loc) · 9.82 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// https://api.covidactnow.org/v2/states.json?apiKey=YOUR_KEY_HERE
var baseURL = 'https://api.covidactnow.org/v2';
var apiKey = '?apiKey=15bb291f94e449c7973d0131c7989297';
var nationalData = '/country/US.json';
var statesData = '/states.json';
var countiesData = '/counties.json';
var singleCountyData = '/county/{fips}.json';
var inputText = document.getElementById('input-text');
var submitBtn = document.getElementById('submit');
var cases = document.getElementById('cases');
var deaths = document.getElementById('state-deaths');
var deathsHeader = document.getElementById('deaths-header');
var hospitalizations = document.getElementById('state-hospitalizations');
var hospitalizationHeader = document.getElementById('hospitalization-header');
var icuData = document.getElementById('icu-data');
var icuHeader = document.getElementById('icu-header');
var hospitalBedData = document.getElementById('hospital-bed-data');
var vaccineData = document.getElementById('vaccine-data');
var vaccineHeader = document.getElementById('vaccine-header');
var totalNationalCases = document.getElementById('national-total-cases');
var totalNationalNewCases = document.getElementById('national-new-cases');
var nationalFullyVaccinated = document.getElementById('fully-vaccinated');
var nationalSingleDose = document.getElementById('single-dose');
var nationalHospitalization = document.getElementById('hospitalizations');
var nationalDeaths = document.getElementById('deaths');
var dropdown = document.getElementById('dropdown');
var dropdownMenu = document.getElementById('dropdown-menu');
var dataCards = document.getElementById('data-cards');
var selectBtn = document.getElementById('select-button');
var selectText = document.getElementById('selected');
var dropdownContent = document.getElementsByClassName('dropdown-content');
var continentDeaths = document.getElementById('continent-deaths');
var continentCases = document.getElementById('continent-cases');
// US states and abbreviations.
var usStates = [
{ name: 'ALABAMA', abbreviation: 'AL'},
{ name: 'ALASKA', abbreviation: 'AK'},
{ name: 'AMERICAN SAMOA', abbreviation: 'AS'},
{ name: 'ARIZONA', abbreviation: 'AZ'},
{ name: 'ARKANSAS', abbreviation: 'AR'},
{ name: 'CALIFORNIA', abbreviation: 'CA'},
{ name: 'COLORADO', abbreviation: 'CO'},
{ name: 'CONNECTICUT', abbreviation: 'CT'},
{ name: 'DELAWARE', abbreviation: 'DE'},
{ name: 'DISTRICT OF COLUMBIA', abbreviation: 'DC'},
{ name: 'FEDERATED STATES OF MICRONESIA', abbreviation: 'FM'},
{ name: 'FLORIDA', abbreviation: 'FL'},
{ name: 'GEORGIA', abbreviation: 'GA'},
{ name: 'GUAM', abbreviation: 'GU'},
{ name: 'HAWAII', abbreviation: 'HI'},
{ name: 'IDAHO', abbreviation: 'ID'},
{ name: 'ILLINOIS', abbreviation: 'IL'},
{ name: 'INDIANA', abbreviation: 'IN'},
{ name: 'IOWA', abbreviation: 'IA'},
{ name: 'KANSAS', abbreviation: 'KS'},
{ name: 'KENTUCKY', abbreviation: 'KY'},
{ name: 'LOUISIANA', abbreviation: 'LA'},
{ name: 'MAINE', abbreviation: 'ME'},
{ name: 'MARSHALL ISLANDS', abbreviation: 'MH'},
{ name: 'MARYLAND', abbreviation: 'MD'},
{ name: 'MASSACHUSETTS', abbreviation: 'MA'},
{ name: 'MICHIGAN', abbreviation: 'MI'},
{ name: 'MINNESOTA', abbreviation: 'MN'},
{ name: 'MISSISSIPPI', abbreviation: 'MS'},
{ name: 'MISSOURI', abbreviation: 'MO'},
{ name: 'MONTANA', abbreviation: 'MT'},
{ name: 'NEBRASKA', abbreviation: 'NE'},
{ name: 'NEVADA', abbreviation: 'NV'},
{ name: 'NEW HAMPSHIRE', abbreviation: 'NH'},
{ name: 'NEW JERSEY', abbreviation: 'NJ'},
{ name: 'NEW MEXICO', abbreviation: 'NM'},
{ name: 'NEW YORK', abbreviation: 'NY'},
{ name: 'NORTH CAROLINA', abbreviation: 'NC'},
{ name: 'NORTH DAKOTA', abbreviation: 'ND'},
{ name: 'NORTHERN MARIANA ISLANDS', abbreviation: 'MP'},
{ name: 'OHIO', abbreviation: 'OH'},
{ name: 'OKLAHOMA', abbreviation: 'OK'},
{ name: 'OREGON', abbreviation: 'OR'},
{ name: 'PALAU', abbreviation: 'PW'},
{ name: 'PENNSYLVANIA', abbreviation: 'PA'},
{ name: 'PUERTO RICO', abbreviation: 'PR'},
{ name: 'RHODE ISLAND', abbreviation: 'RI'},
{ name: 'SOUTH CAROLINA', abbreviation: 'SC'},
{ name: 'SOUTH DAKOTA', abbreviation: 'SD'},
{ name: 'TENNESSEE', abbreviation: 'TN'},
{ name: 'TEXAS', abbreviation: 'TX'},
{ name: 'UTAH', abbreviation: 'UT'},
{ name: 'VERMONT', abbreviation: 'VT'},
{ name: 'VIRGIN ISLANDS', abbreviation: 'VI'},
{ name: 'VIRGINIA', abbreviation: 'VA'},
{ name: 'WASHINGTON', abbreviation: 'WA'},
{ name: 'WEST VIRGINIA', abbreviation: 'WV'},
{ name: 'WISCONSIN', abbreviation: 'WI'},
{ name: 'WYOMING', abbreviation: 'WY' }
]
// Listening to a change in state.
dropdown.addEventListener('change', function(e) {
var selectedState = dropdown.value;
var locationInput = selectedState.toUpperCase().trim();
for (var i = 0; i < usStates.length; i++) {
if (locationInput === usStates[i].name) {
// get state abbreviation
var stateAbbr = usStates[i].abbreviation;
// Showing the data cards to the web page
dataCards.classList.remove('is-hidden');
// Setting the local storage key and value.
localStorage.setItem('state', selectedState);
localStorage.setItem('state-abbr', stateAbbr);
getStateData(stateAbbr, selectedState);
}
}
})
// Fetching national data from API.
function getNationalData() {
fetch(baseURL + nationalData + apiKey)
.then(function(response) {
return response.json();
})
.then(function(data) {
renderNationalData(data);
return data;
})
}
// Rendering national data to the webpage.
function renderNationalData(data) {
totalNationalCases.innerText = data.actuals.cases.toLocaleString('en-US');
totalNationalNewCases.innerText = data.actuals.newCases.toLocaleString('en-US');
nationalFullyVaccinated.innerText = data.actuals.vaccinationsCompleted.toLocaleString('en-US');
nationalSingleDose.innerText = data.actuals.vaccinationsInitiated.toLocaleString('en-US');
nationalHospitalization.innerText = data.actuals.hospitalBeds.currentUsageTotal.toLocaleString('en-US');
nationalDeaths.innerText = data.actuals.deaths.toLocaleString('en-US');
}
// Fetching state data from API.
function getStateData(state, stateName) {
fetch(baseURL + '/state/' + state + '.timeseries.json' + apiKey)
.then(function(response) {
return response.json();
})
.then(function(data) {
renderStateData(data, stateName);
return data;
})
}
// Rendering state data to the webpage.
function renderStateData(data, stateName) {
icuHeader.innerText = stateName;
vaccineHeader.innerText = stateName;
hospitalizationHeader.innerText = stateName;
deathsHeader.innerText = stateName;
icuData.innerText = 'There are ' + (data.actuals.icuBeds.capacity - data.actuals.icuBeds.currentUsageTotal).toLocaleString('en-US') + ' ICU bed(s) available.';
vaccineData.innerText = data.actuals.vaccinesAdministered.toLocaleString('en-US') + ' vaccinations have been administered.';
deaths.innerText = data.actuals.deaths.toLocaleString('en-US') + ' people have died from COVID-19.';
hospitalizations.innerText = Math.round((data.actuals.hospitalBeds.currentUsageCovid / data.actuals.hospitalBeds.capacity) * 100)+'% of hospitalizations are COVID-19 cases.';
}
// Fetching continent data from API.
function getContinentData() {
fetch("https://covid-api.mmediagroup.fr/v1/cases?continent=North America")
.then(function(response) {
return response.json();
})
.then(function(data) {
renderContinentData(data);
})
}
// Render continent data.
function renderContinentData(data) {
var continentCasesArray = [];
var continentDeathsArray = [];
for (var country in data) {
continentCasesArray.push(data[country]['All']['confirmed']);
continentDeathsArray.push(data[country]['All']['deaths']);
}
var continentCasesCount = 0;
var continentDeathsCount = 0;
// Getting sum of continent cases.
for (var i = 0; i < continentCasesArray.length; i++) {
continentCasesCount += continentCasesArray[i];
continentCases.innerText = continentCasesCount.toLocaleString('en-US');
}
// Getting sum of continent deaths.
for (var i = 0; i < continentDeathsArray.length; i++) {
continentDeathsCount += continentDeathsArray[i];
continentDeaths.innerText = continentDeathsCount.toLocaleString('en-US');
}
}
// Getting the local storage data
function getLocalStorageData() {
if (localStorage.getItem('state')) {
var stateName = localStorage.getItem('state');
var stateAbbr = localStorage.getItem('state-abbr');
// Changing the select state wording to reflect the selected state
selectText.innerText = stateName;
getStateData(stateAbbr, stateName);
dataCards.classList.remove('is-hidden');
} else {
selectText.innerText = "Select your state";
}
}
// Calling function to display US national data.
getNationalData();
// Calling function to display North America data.
getContinentData();
// Calling the local storage data (if available).
getLocalStorageData();