|
| 1 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 2 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 3 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 4 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 5 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 6 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 7 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 8 | + }); |
| 9 | +}; |
| 10 | +var __generator = (this && this.__generator) || function (thisArg, body) { |
| 11 | + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; |
| 12 | + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; |
| 13 | + function verb(n) { return function (v) { return step([n, v]); }; } |
| 14 | + function step(op) { |
| 15 | + if (f) throw new TypeError("Generator is already executing."); |
| 16 | + while (_) try { |
| 17 | + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; |
| 18 | + if (y = 0, t) op = [op[0] & 2, t.value]; |
| 19 | + switch (op[0]) { |
| 20 | + case 0: case 1: t = op; break; |
| 21 | + case 4: _.label++; return { value: op[1], done: false }; |
| 22 | + case 5: _.label++; y = op[1]; op = [0]; continue; |
| 23 | + case 7: op = _.ops.pop(); _.trys.pop(); continue; |
| 24 | + default: |
| 25 | + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } |
| 26 | + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } |
| 27 | + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } |
| 28 | + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } |
| 29 | + if (t[2]) _.ops.pop(); |
| 30 | + _.trys.pop(); continue; |
| 31 | + } |
| 32 | + op = body.call(thisArg, _); |
| 33 | + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } |
| 34 | + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; |
| 35 | + } |
| 36 | +}; |
| 37 | +// utils |
| 38 | +function $(selector) { |
| 39 | + return document.querySelector(selector); |
| 40 | +} |
| 41 | +function getUnixTimestamp(date) { |
| 42 | + return new Date(date).getTime(); |
| 43 | +} |
| 44 | +// DOM |
| 45 | +var a; |
| 46 | +var confirmedTotal = $('.confirmed-total'); |
| 47 | +var deathsTotal = $('.deaths'); |
| 48 | +var recoveredTotal = $('.recovered'); |
| 49 | +var lastUpdatedTime = $('.last-updated-time'); |
| 50 | +var rankList = $('.rank-list'); |
| 51 | +var deathsList = $('.deaths-list'); |
| 52 | +var recoveredList = $('.recovered-list'); |
| 53 | +var deathSpinner = createSpinnerElement('deaths-spinner'); |
| 54 | +var recoveredSpinner = createSpinnerElement('recovered-spinner'); |
| 55 | +function createSpinnerElement(id) { |
| 56 | + var wrapperDiv = document.createElement('div'); |
| 57 | + wrapperDiv.setAttribute('id', id); |
| 58 | + wrapperDiv.setAttribute('class', 'spinner-wrapper flex justify-center align-center'); |
| 59 | + var spinnerDiv = document.createElement('div'); |
| 60 | + spinnerDiv.setAttribute('class', 'ripple-spinner'); |
| 61 | + spinnerDiv.appendChild(document.createElement('div')); |
| 62 | + spinnerDiv.appendChild(document.createElement('div')); |
| 63 | + wrapperDiv.appendChild(spinnerDiv); |
| 64 | + return wrapperDiv; |
| 65 | +} |
| 66 | +// state |
| 67 | +var isDeathLoading = false; |
| 68 | +var isRecoveredLoading = false; |
| 69 | +// api |
| 70 | +function fetchCovidSummary() { |
| 71 | + var url = 'https://api.covid19api.com/summary'; |
| 72 | + return axios.get(url); |
| 73 | +} |
| 74 | +var CovidStatus; |
| 75 | +(function (CovidStatus) { |
| 76 | + CovidStatus["Confirmed"] = "confirmed"; |
| 77 | + CovidStatus["Recovered"] = "recovered"; |
| 78 | + CovidStatus["Deaths"] = "deaths"; |
| 79 | +})(CovidStatus || (CovidStatus = {})); |
| 80 | +function fetchCountryInfo(countryCode, status) { |
| 81 | + // status params: confirmed, recovered, deaths |
| 82 | + var url = "https://api.covid19api.com/country/" + countryCode + "/status/" + status; |
| 83 | + return axios.get(url); |
| 84 | +} |
| 85 | +// methods |
| 86 | +function startApp() { |
| 87 | + setupData(); |
| 88 | + initEvents(); |
| 89 | +} |
| 90 | +// events |
| 91 | +function initEvents() { |
| 92 | + rankList.addEventListener('click', handleListClick); |
| 93 | +} |
| 94 | +function handleListClick(event) { |
| 95 | + return __awaiter(this, void 0, void 0, function () { |
| 96 | + var selectedId, deathResponse, recoveredResponse, confirmedResponse; |
| 97 | + return __generator(this, function (_a) { |
| 98 | + switch (_a.label) { |
| 99 | + case 0: |
| 100 | + if (event.target instanceof HTMLParagraphElement || |
| 101 | + event.target instanceof HTMLSpanElement) { |
| 102 | + selectedId = event.target.parentElement.id; |
| 103 | + } |
| 104 | + if (event.target instanceof HTMLLIElement) { |
| 105 | + selectedId = event.target.id; |
| 106 | + } |
| 107 | + if (isDeathLoading) { |
| 108 | + return [2 /*return*/]; |
| 109 | + } |
| 110 | + clearDeathList(); |
| 111 | + clearRecoveredList(); |
| 112 | + startLoadingAnimation(); |
| 113 | + isDeathLoading = true; |
| 114 | + return [4 /*yield*/, fetchCountryInfo(selectedId, CovidStatus.Deaths)]; |
| 115 | + case 1: |
| 116 | + deathResponse = (_a.sent()).data; |
| 117 | + return [4 /*yield*/, fetchCountryInfo(selectedId, CovidStatus.Recovered)]; |
| 118 | + case 2: |
| 119 | + recoveredResponse = (_a.sent()).data; |
| 120 | + return [4 /*yield*/, fetchCountryInfo(selectedId, CovidStatus.Confirmed)]; |
| 121 | + case 3: |
| 122 | + confirmedResponse = (_a.sent()).data; |
| 123 | + endLoadingAnimation(); |
| 124 | + setDeathsList(deathResponse); |
| 125 | + setTotalDeathsByCountry(deathResponse); |
| 126 | + setRecoveredList(recoveredResponse); |
| 127 | + setTotalRecoveredByCountry(recoveredResponse); |
| 128 | + setChartData(confirmedResponse); |
| 129 | + isDeathLoading = false; |
| 130 | + return [2 /*return*/]; |
| 131 | + } |
| 132 | + }); |
| 133 | + }); |
| 134 | +} |
| 135 | +function setDeathsList(data) { |
| 136 | + var sorted = data.sort(function (a, b) { return getUnixTimestamp(b.Date) - getUnixTimestamp(a.Date); }); |
| 137 | + sorted.forEach(function (value) { |
| 138 | + var li = document.createElement('li'); |
| 139 | + li.setAttribute('class', 'list-item-b flex align-center'); |
| 140 | + var span = document.createElement('span'); |
| 141 | + span.textContent = value.Cases; |
| 142 | + span.setAttribute('class', 'deaths'); |
| 143 | + var p = document.createElement('p'); |
| 144 | + p.textContent = new Date(value.Date).toLocaleDateString().slice(0, -1); |
| 145 | + li.appendChild(span); |
| 146 | + li.appendChild(p); |
| 147 | + deathsList.appendChild(li); |
| 148 | + }); |
| 149 | +} |
| 150 | +function clearDeathList() { |
| 151 | + deathsList.innerHTML = null; |
| 152 | +} |
| 153 | +function setTotalDeathsByCountry(data) { |
| 154 | + deathsTotal.innerText = data[0].Cases; |
| 155 | +} |
| 156 | +function setRecoveredList(data) { |
| 157 | + var sorted = data.sort(function (a, b) { return getUnixTimestamp(b.Date) - getUnixTimestamp(a.Date); }); |
| 158 | + sorted.forEach(function (value) { |
| 159 | + var li = document.createElement('li'); |
| 160 | + li.setAttribute('class', 'list-item-b flex align-center'); |
| 161 | + var span = document.createElement('span'); |
| 162 | + span.textContent = value.Cases; |
| 163 | + span.setAttribute('class', 'recovered'); |
| 164 | + var p = document.createElement('p'); |
| 165 | + p.textContent = new Date(value.Date).toLocaleDateString().slice(0, -1); |
| 166 | + li.appendChild(span); |
| 167 | + li.appendChild(p); |
| 168 | + recoveredList.appendChild(li); |
| 169 | + }); |
| 170 | +} |
| 171 | +function clearRecoveredList() { |
| 172 | + recoveredList.innerHTML = null; |
| 173 | +} |
| 174 | +function setTotalRecoveredByCountry(data) { |
| 175 | + recoveredTotal.innerText = data[0].Cases; |
| 176 | +} |
| 177 | +function startLoadingAnimation() { |
| 178 | + deathsList.appendChild(deathSpinner); |
| 179 | + recoveredList.appendChild(recoveredSpinner); |
| 180 | +} |
| 181 | +function endLoadingAnimation() { |
| 182 | + deathsList.removeChild(deathSpinner); |
| 183 | + recoveredList.removeChild(recoveredSpinner); |
| 184 | +} |
| 185 | +function setupData() { |
| 186 | + return __awaiter(this, void 0, void 0, function () { |
| 187 | + var data; |
| 188 | + return __generator(this, function (_a) { |
| 189 | + switch (_a.label) { |
| 190 | + case 0: return [4 /*yield*/, fetchCovidSummary()]; |
| 191 | + case 1: |
| 192 | + data = (_a.sent()).data; |
| 193 | + setTotalConfirmedNumber(data); |
| 194 | + setTotalDeathsByWorld(data); |
| 195 | + setTotalRecoveredByWorld(data); |
| 196 | + setCountryRanksByConfirmedCases(data); |
| 197 | + setLastUpdatedTimestamp(data); |
| 198 | + return [2 /*return*/]; |
| 199 | + } |
| 200 | + }); |
| 201 | + }); |
| 202 | +} |
| 203 | +function renderChart(data, labels) { |
| 204 | + var ctx = $('#lineChart').getContext('2d'); |
| 205 | + Chart.defaults.global.defaultFontColor = '#f5eaea'; |
| 206 | + Chart.defaults.global.defaultFontFamily = 'Exo 2'; |
| 207 | + new Chart(ctx, { |
| 208 | + type: 'line', |
| 209 | + data: { |
| 210 | + labels: labels, |
| 211 | + datasets: [ |
| 212 | + { |
| 213 | + label: 'Confirmed for the last two weeks', |
| 214 | + backgroundColor: '#feb72b', |
| 215 | + borderColor: '#feb72b', |
| 216 | + data: data, |
| 217 | + }, |
| 218 | + ], |
| 219 | + }, |
| 220 | + options: {}, |
| 221 | + }); |
| 222 | +} |
| 223 | +function setChartData(data) { |
| 224 | + var chartData = data.slice(-14).map(function (value) { return value.Cases; }); |
| 225 | + var chartLabel = data |
| 226 | + .slice(-14) |
| 227 | + .map(function (value) { return new Date(value.Date).toLocaleDateString().slice(5, -1); }); |
| 228 | + renderChart(chartData, chartLabel); |
| 229 | +} |
| 230 | +function setTotalConfirmedNumber(data) { |
| 231 | + confirmedTotal.innerText = data.Countries.reduce(function (total, current) { return (total += current.TotalConfirmed); }, 0); |
| 232 | +} |
| 233 | +function setTotalDeathsByWorld(data) { |
| 234 | + deathsTotal.innerText = data.Countries.reduce(function (total, current) { return (total += current.TotalDeaths); }, 0); |
| 235 | +} |
| 236 | +function setTotalRecoveredByWorld(data) { |
| 237 | + recoveredTotal.innerText = data.Countries.reduce(function (total, current) { return (total += current.TotalRecovered); }, 0); |
| 238 | +} |
| 239 | +function setCountryRanksByConfirmedCases(data) { |
| 240 | + var sorted = data.Countries.sort(function (a, b) { return b.TotalConfirmed - a.TotalConfirmed; }); |
| 241 | + sorted.forEach(function (value) { |
| 242 | + var li = document.createElement('li'); |
| 243 | + li.setAttribute('class', 'list-item flex align-center'); |
| 244 | + li.setAttribute('id', value.Slug); |
| 245 | + var span = document.createElement('span'); |
| 246 | + span.textContent = value.TotalConfirmed; |
| 247 | + span.setAttribute('class', 'cases'); |
| 248 | + var p = document.createElement('p'); |
| 249 | + p.setAttribute('class', 'country'); |
| 250 | + p.textContent = value.Country; |
| 251 | + li.appendChild(span); |
| 252 | + li.appendChild(p); |
| 253 | + rankList.appendChild(li); |
| 254 | + }); |
| 255 | +} |
| 256 | +function setLastUpdatedTimestamp(data) { |
| 257 | + lastUpdatedTime.innerText = new Date(data.Date).toLocaleString(); |
| 258 | +} |
| 259 | +startApp(); |
0 commit comments