-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
85 lines (71 loc) · 2.99 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
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
renderStarWarsCharachtersPage();
async function renderStarWarsCharachtersPage() {
const sWPeopleData = await getSWPeople();
await renderStarWarsCharachtersList(sWPeopleData.results);
await renderPagination(sWPeopleData.previous, sWPeopleData.next);
}
async function renderStarWarsCharachtersList(list) {
let content = document.querySelector('.content');
content.innerHTML = "";
await list.forEach(async person => {
content.innerHTML = content.innerHTML += `<div class="person">
<h2>${person.name}</h2>
<div id="${person.uid}">
<button class="show-profile-button" onclick="renderProfileInfo(${person.uid});">Show profile</button>
</div>
</div>`;
});
}
async function renderPagination(prevLink, nextLink) {
let buttons = document.querySelector('.buttons');
buttons.innerHTML = "";
if (prevLink) {
renderPaginationButton("Previous", prevLink);
}
if (nextLink) {
renderPaginationButton("Next", nextLink)
}
}
async function renderProfileInfo(id) {
let profile = await getSWPersonById(id);
let properties = profile.result.properties;
let profileBlock = document.getElementById(id);
profileBlock.innerHTML = "";
profileBlock.innerHTML = `<div>
<p>Height: ${properties.height}</p>
<p>Mass: ${properties.mass}</p>
<p>Hair color: ${properties.hair_color}</p>
<p>Skin color: ${properties.skin_color}</p>
<p>Eye color: ${properties.eye_color}</p>
<p>Birth year: ${properties.birth_year}</p>
<p>Gender: ${properties.gender}</p>
</div>`;
profileBlock.append(html)
}
async function renderPaginationButton(text, apiUrl) {
let buttons = document.querySelector('.buttons');
const button = document.createElement("button");
button.innerHTML = text;
button.className = "pagination";
button.addEventListener("click", async function () {
const sWPeopleData = await getSWPeople(apiUrl);
renderStarWarsCharachtersList(sWPeopleData.results);
renderPagination(sWPeopleData.previous, sWPeopleData.next);
})
buttons.append(button);
}
async function getSWPeople(url = 'https://www.swapi.tech/api/people?page=1&limit=10') {
return await getData(url);
}
async function getSWPersonById(id) {
const url = `https://www.swapi.tech/api/people/${id}`;
return await getData(url);
}
async function getData(url) {
const response = await fetch(url);
if (!response.ok) {
throw new Error('There was a problem with the fetch operation');
}
const data = await response.json();
return data;
}