mandalorian/scripts/planets.js

92 lines
2.9 KiB
JavaScript
Raw Normal View History

import { api_call, API_URL, getPage } from "./util";
2021-10-18 15:32:20 +00:00
2021-10-19 07:47:51 +00:00
const planetes = document.getElementById("resultats");
2021-10-18 15:32:20 +00:00
console.log('planet.js loaded');
2021-10-19 13:23:08 +00:00
2021-10-19 14:04:18 +00:00
function remplissage(results) {
2021-10-19 08:31:51 +00:00
planetes.innerHTML = "";
2021-10-19 08:31:51 +00:00
const previousUrl = results.previous;
const nextUrl = results.next;
const tabResults = results.results;
2021-10-19 14:04:18 +00:00
for (resultPlanet of tabResults) {
2021-10-19 08:31:51 +00:00
const nom = resultPlanet.name;
const diametre = resultPlanet.diameter;
const climate = resultPlanet.climate;
const terrain = resultPlanet.terrain;
const population = resultPlanet.population;
const tabResidents = resultPlanet.residents;
2021-10-19 09:52:12 +00:00
//Problème de requêtes asyncrones
// let tabResidents = [];
// for(index in resultPlanet.residents){
// api_call(resultPlanet.residents[index], (jsonresp)=>{
// console.log(jsonresp.name);
// });
// }
// console.log(`${nom} - résidents : ${resultPlanet.residents}`);
// console.log(`nom des résidents : ${tabResidents}`);
const tabFilms = resultPlanet.films;
2021-10-19 08:31:51 +00:00
2021-10-19 14:04:18 +00:00
planetes.innerHTML +=
`<section class="resultat">
<h1 class="titreAccordeon">${nom}</h1>
<div class="accordeon">
<div class="1">
<p>${terrain}</p>
<p>${climate}</p>
<p>${diametre}Km</p>
</div>
<div class="2">${tabResidents}</div>
<div class="3">${tabFilms}</div>
</div>
</section>`;
2021-10-19 08:31:51 +00:00
}
2021-10-19 07:47:51 +00:00
//Pagination
2021-10-19 13:23:08 +00:00
console.log("Url précédente : " + previousUrl);
console.group("Url suivante : " + nextUrl);
const currentPage = (previousUrl == null ? parseInt(getPage(nextUrl)) - 1 : parseInt(getPage(previousUrl)) + 1);
console.log("Page courante : " + currentPage);
2021-10-19 07:47:51 +00:00
2021-10-19 13:23:08 +00:00
let firstPage = false;
let lastPage = false;
2021-10-19 14:04:18 +00:00
if (previousUrl == null) firstPage = true;
if (nextUrl == null) lastPage = true;
2021-10-19 13:23:08 +00:00
let previousButton = `<button id="prevButton">Page précédente</button>`;
let currentButton = `${currentPage}`;
let nextButton = `<button id="nextButton">Page suivante</button>`;
2021-10-19 14:04:18 +00:00
planetes.innerHTML += previousButton;
2021-10-19 13:23:08 +00:00
planetes.innerHTML += currentButton;
2021-10-19 14:04:18 +00:00
planetes.innerHTML += nextButton;
if(firstPage){
document.getElementById("prevButton").hidden = true;
}
else{
document.getElementById("prevButton").hidden = false;
}
if(lastPage){
document.getElementById("nextButton").hidden = true;
}
else{
document.getElementById("nextButton").hidden = false;
}
document.getElementById("prevButton").addEventListener("click", () => {
api_call(previousUrl, remplissage);
});
document.getElementById("nextButton").addEventListener("click", () => {
api_call(nextUrl, remplissage);
});
2021-10-19 13:23:08 +00:00
}
2021-10-19 13:23:08 +00:00
api_call(API_URL + "/planets", remplissage);