This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
52 lines (47 loc) · 1.99 KB
/
main.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
const tableBody = document.getElementById('characters-table-body')
const tableFooter = document.getElementById('characters-table-foot')
window.onload = () => {
getApiData('https://rickandmortyapi.com/api/character')
}
const getApiData = url => {
fetch(url, {method: 'GET'})
.then(response => {
if (!response.ok) throw new Error('response Error')
return response.json()
}).then(data => {
renderTableBody(data.results)
renderTableFooter(data.info)
}).catch(error => console.log(error))
}
const renderTableBody = data => {
tableBody.innerHTML = ''
data.sort((a, b) => (a.name - b.name)).forEach(dataObj => {
tableBody.insertRow().innerHTML =
`<td>${dataObj.name}</td>
<td><img src="${dataObj.image}" alt="${dataObj.name}-img" class="rounded img-fluid"></td>
<td>${dataObj.status}</td>
<td>${dataObj.species}</td>
<td>${dataObj.gender}</td>
<td>${dataObj.origin.name}</td>
<td>${dataObj.location.name}</td>`
})
}
const renderTableFooter = info => {
tableFooter.innerHTML = ''
const totalPages = info.pages
const previousPageAction = `onclick="${info.prev ? `getNewData('${info.prev}')` : ''}"`
const nextPageAction = `onclick="${info.next ? `getNewData('${info.next}')` : ''}"`
const currentPage = parseInt(info.next.match(/\d+/g)[0]) - 1
tableFooter.insertRow().innerHTML =
`<td colspan="7">
<div>
<button type="button" role="link" class="btn btn-secondary" ${previousPageAction}>Previous</button>
<button type="button" class="btn btn-secondary current-page" disabled>Page: ${currentPage}/${totalPages}</button>
<button type="button" role="link" class="btn btn-secondary" ${nextPageAction}>Next</button>
</div>
</td>`
}
const getNewData = newUrl => {
window.scrollTo(0, 0)
getApiData(newUrl)
}