-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
185 lines (128 loc) · 5.44 KB
/
scripts.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
"use strict";
const init = () => {
const form = document.getElementById("theForm");
const rsltDiv = document.getElementById("results");
// initialize focus on text input field
document.getElementById("search").focus();
// =======================================================================
// perform the backend call (php -> REST API) to run the search
// uses async/await - returns a promise
//
const runSearch = async formData => {
const resp = await fetch("restcountries.php", {
method: "POST",
body: formData
});
const rslt = await resp.json();
return rslt;
}
// =======================================================================
// set summary data at bottom of page
//
const setSummary = (count, regions, subregions) => {
document.getElementById("result-count").textContent = count;
document.getElementById("region-list").textContent = regions;
document.getElementById("subregion-list").textContent = subregions;
}
//=============================================================================
// getSummaryString - build summary count data - helper function
//
// input: object obj[key_i] = count_i
// output: string key1 (cnt1) | key2 (cnt2) | key3 (cnt3) ...
// sorted by descending count
//
const getSummaryString = countObj => {
return Object.keys(countObj)
.sort( (a, b) => {
return ( countObj[b] - countObj[a] )
})
.map( i => ` ${i} (${countObj[i]}) `)
.join("|");
};
// =======================================================================
// clear page
//
const clearResults = () => {
// clear any prior results / state
rsltDiv.classList.remove("errorState");
while (rsltDiv.firstChild) {
rsltDiv.firstChild.remove();
}
setSummary("", "", "");
}
// =======================================================================
// form "submit" handling
//
form.addEventListener("submit", evt => {
evt.preventDefault(); // cancel actual form submission
// perform client-side form validation prior to submitting to server
const formData = new FormData(form);
const srch = formData.get("search");
clearResults();
// check for blank search field
if (!srch) {
showErrorResult("Please enter a search term.");
return; // get out
}
// check for valid country code search entry (2 or 3 characters required)
if (formData.get("searchType") == 2) {
if (srch.length < 2 || srch.length > 3) {
showErrorResult("Country code search must be 2 or 3 characters long.");
return; // get out
}
}
// run the search
runSearch(formData)
.then( json => showResults(json));
})
//==============================================================================
//
const showErrorResult = errText => {
rsltDiv.classList.add("errorState");
rsltDiv.textContent = errText;
}
// =======================================================================
// build out the display with the API json results
//
const showResults = results => {
// handle errors returned from API call
if (results.error) {
showErrorResult(results.errMsg);
return;
// normal case - we have data to show
} else {
const nf = new Intl.NumberFormat(); // use for thousands separators below
let countryCount = 0, regCount = {}, subCount = {};
results.forEach( obj => {
let div = document.createElement("div");
div.classList.add("country");
// destructuring assignment
let { name, alpha2Code, alpha3Code, flag, region, subregion, population, languages } = obj;
languages = languages.map( i => i.name).join(", ");
// aggregate summary data
countryCount++;
(regCount[region]) ? regCount[region]++ : regCount[region] = 1;
(subCount[subregion]) ? subCount[subregion]++ : subCount[subregion] = 1;
div.innerHTML = `
<div class='flex flex2'>
<img src=${flag} />
</div>
<div class='flex flex4'>
<p><span class='label'>Country:</span> ${name}</p>
<p><span class='label'>Population:</span> ${nf.format(population)}</p>
<p><span class='label'>Languages:</span> ${languages}</p>
</div>
<div class='flex flex4'>
<p><span class='label'>Country Codes:</span> ${alpha2Code}, ${alpha3Code}</p>
<p><span class='label'>Region:</span> ${region}</p>
<p><span class='label'>Subregion:</span> ${subregion}</p>
</div>
`;
rsltDiv.appendChild(div);
});
//Build content for bottom summary
setSummary(countryCount, getSummaryString(regCount), getSummaryString(subCount));
}
}
}
window.onload = init;