-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog.js
54 lines (46 loc) · 1.64 KB
/
blog.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
const api = 'https://master-7rqtwti-fus3tltfddn7w.eu-2.platformsh.site';
const loadButton = document.getElementById('load-button');
function getRequest(url) {
return new Promise((resolve, reject) => {
let request = new XMLHttpRequest();
request.open('GET', url);
request.onreadystatechange = () => {
if (request.readyState === 4) {
if (request.status !== 200) {
reject(JSON.parse(request.response));
}
resolve(JSON.parse(request.response));
}
};
request.send();
});
}
async function getBlogPost() {
const titlePromise = getRequest(api + '/generate-title');
const loremPromise = getRequest(api + '/generate-lorem');
try {
let [titleResponse, loremResponse] = await Promise.all([titlePromise, loremPromise]);
document.querySelector('main').appendChild(buildPostElement(titleResponse.title, loremResponse.lorem));
} catch (error) {
document.querySelector('main').appendChild(buildPostElement('Une erreur est survenue !', error));
}
}
loadButton.addEventListener('click', async function () {
getBlogPost();
});
function buildPostElement(title, content) {
let card = document.createElement('div');
let cardBody = document.createElement('div');
let cardTitle = document.createElement('h2');
let cardContent = document.createElement('p');
card.classList.add('card');
cardBody.classList.add('card-body');
cardTitle.classList.add('card-title');
cardContent.classList.add('card-text');
cardTitle.textContent = title;
cardContent.textContent = content;
cardBody.appendChild(cardTitle);
cardBody.appendChild(cardContent);
card.appendChild(cardBody);
return card;
}