-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
56 lines (41 loc) · 1.29 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
import 'regenerator-runtime/runtime';
import axios from 'axios';
// find elements
const BASE_URL = 'https://api.highvolume.georanker.com/region/list?';
const API_KEY = "api-key";
const getRegionsItems = async () => {
const config = {
headers: {
'Content-Type' : 'application/json;charset=utf-8',
'Accept': 'application/json',
}
};
try {
const response = await axios.get(`${BASE_URL}apikey=${API_KEY}`,config);
const regionsItems = response.data.items;
console.log(`GET: Here's the list of regions`, regionsItems);
return regionsItems;
} catch (errors) {
console.error(errors);
}
};
// ...
const createRegionElement = item => {
const regionsElement = document.createElement('li');
regionsElement.appendChild(document.createTextNode(item.name));
return regionsElement;
};
const updateRegionsList = regionItems => {
const regionList = document.querySelector('ul');
if (Array.isArray(regionItems) && regionItems.length > 0) {
regionItems.map(regionItem => {
regionList.appendChild(createRegionElement(regionItem));
});
} else if (regionItems) {
regionList.appendChild(createRegionElement(regionItems));
}
};
const main = async () => {
updateRegionsList(await getRegionsItems());
};
main();