forked from codemistic/Web-Development
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountries.js
More file actions
166 lines (160 loc) · 5.91 KB
/
countries.js
File metadata and controls
166 lines (160 loc) · 5.91 KB
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
const countriesContainer = document.querySelector('.countries');
const btn = document.querySelector('.btn-country');
/*
const getcountrydata = function (country) {
const request = new XMLHttpRequest();
request.open('GET', `https://restcountries.com/v2/name/${country}`);
request.send();
request.addEventListener('load', function () {
const [data] = JSON.parse(this.responseText);
console.log(data);
const html = `
<article class="country">
<img class="country__img" src="${data.flag}" />
<div class="country__data">
<h3 class="country__name">${data.name}</h3>
<h4 class="country__region">${data.region}</h4>
<p class="country__row"><span>👫</span>${(
+data.population / 1000000
).toFixed(1)}M</p>
<p class="country__row"><span>🗣️</span>${
data.languages[0].name
}</p>
<p class="country__row"><span>💰</span>${
data.currencies[0].name
}</p>
</div>
</article>`;
countriesContainer.insertAdjacentHTML('beforeend', html);
countriesContainer.style.opacity = 1;
});
};
getcountrydata('nepal');
getcountrydata('usa');
getcountrydata('japan');
getcountrydata('china');*/
// Our First AJAX Call: XMLHttpRequest
/*
const getCountryData = function (country) {
const request = new XMLHttpRequest();
request.open('GET', `https://restcountries.eu/rest/v2/name/${country}`);
request.send();
request.addEventListener('load', function () {
const [data] = JSON.parse(this.responseText);
console.log(data);
const html = `
<article class="country">
<img class="country__img" src="${data.flag}" />
<div class="country__data">
<h3 class="country__name">${data.name}</h3>
<h4 class="country__region">${data.region}</h4>
<p class="country__row"><span>👫</span>${(
+data.population / 1000000
).toFixed(1)} people</p>
<p class="country__row"><span>🗣️</span>${data.languages[0].name}</p>
<p class="country__row"><span>💰</span>${data.currencies[0].name}</p>
</div>
</article>
`;
countriesContainer.insertAdjacentHTML('beforeend', html);
countriesContainer.style.opacity = 1;
});
};
getCountryData('portugal');
getCountryData('usa');
getCountryData('germany');
///////////////////////////////////////
// Welcome to Callback Hell
const renderCountry = function (data, className = '') {
const html = `
<article class="country ${className}">
<img class="country__img" src="${data.flag}" />
<div class="country__data">
<h3 class="country__name">${data.name}</h3>
<h4 class="country__region">${data.region}</h4>
<p class="country__row"><span>👫</span>${(
+data.population / 1000000
).toFixed(1)} people</p>
<p class="country__row"><span>🗣️</span>${data.languages[0].name}</p>
<p class="country__row"><span>💰</span>${data.currencies[0].name}</p>
</div>
</article>
`;
countriesContainer.insertAdjacentHTML('beforeend', html);
countriesContainer.style.opacity = 1;
};
const getCountryAndNeighbour = function (country) {
// AJAX call country 1
const request = new XMLHttpRequest();
request.open('GET', `https://restcountries.com/v2/name/${country}`);
request.send();
request.addEventListener('load', function () {
const [data] = JSON.parse(this.responseText);
console.log(data);
// Render country 1
renderCountry(data);
// Get neighbour country (2)
const [neighbour] = data.borders;
if (!neighbour) return;
// AJAX call country 2
const request2 = new XMLHttpRequest();
request2.open('GET', `https://restcountries.com/v2/alpha/${neighbour}`);
request2.send();
request2.addEventListener('load', function () {
const data2 = JSON.parse(this.responseText);
console.log(data2);
renderCountry(data2, 'neighbour');
});
});
};
getCountryAndNeighbour('portugal');
getCountryAndNeighbour('usa');*/
//promise and fetch api
//container for asyncronously delivered value,container for future value
//promise =>pending=>setteled-- i)fulfilled-sucessful ii)rejected
const renderCountry = function (data, className = '') {
const html = `
<article class="country ${className}">
<img class="country__img" src="${data.flag}" />
<div class="country__data">
<h3 class="country__name">${data.name}</h3>
<h4 class="country__region">${data.region}</h4>
<p class="country__row"><span>👫</span>${(
+data.population / 1000000
).toFixed(1)}M people</p>
<p class="country__row"><span>🗣️</span>${data.languages[0].name}</p>
<p class="country__row"><span>💰</span>${data.currencies[0].name}</p>
</div>
</article>
`;
countriesContainer.insertAdjacentHTML('beforeend', html);
countriesContainer.style.opacity = 1;
};
/*const request = fetch(`https://restcountries.com/v2/name/nepal`); //fetch function return a promise
console.log(request);
const getCountryData = function (country) {
fetch(`https://restcountries.com/v2/name/${country}`)
.then(function (response) {
console.log(response);
return response.json();
})
.then(function (data) {
console.log(data);
renderCountry(data[0]);
});
};*/
const getCountryData = function (country) {
fetch(`https://restcountries.com/v2/name/${country}`)
.then((response) => response.json())
.then((data) => {
renderCountry(data[0]);
const neighbour = data[0].borders?.[0];
return fetch(`https://restcountries.com/v2/alpha/${neighbour}`);
})
.then((response) => response.json())
.then((xxx) => renderCountry(xxx, 'neighbour'));
};
//handle promise rejection
btn.addEventListener('click', function () {
getCountryData('portugal');
});