2021-10-19 10:37:14 +00:00
|
|
|
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 08:31:51 +00:00
|
|
|
function remplissage(results){
|
|
|
|
|
2021-10-19 10:18:53 +00:00
|
|
|
planetes.innerHTML = "";
|
|
|
|
|
2021-10-19 08:31:51 +00:00
|
|
|
const previousUrl = results.previous;
|
|
|
|
const nextUrl = results.next;
|
|
|
|
const tabResults = results.results;
|
|
|
|
|
|
|
|
for(resultPlanet of tabResults){
|
|
|
|
const nom = resultPlanet.name;
|
|
|
|
const diametre = resultPlanet.diameter;
|
|
|
|
const climate = resultPlanet.climate;
|
|
|
|
const terrain = resultPlanet.terrain;
|
|
|
|
const population = resultPlanet.population;
|
|
|
|
|
2021-10-19 10:18:53 +00:00
|
|
|
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 10:18:53 +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
|
|
|
|
2021-10-19 10:18:53 +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;
|
|
|
|
if(previousUrl == null) firstPage = true;
|
|
|
|
if(nextUrl == null) lastPage = true;
|
|
|
|
let previousButton = `<button id="prevButton">Page précédente</button>`;
|
|
|
|
let currentButton = `${currentPage}`;
|
|
|
|
let nextButton = `<button id="nextButton">Page suivante</button>`;
|
|
|
|
if(!firstPage) {
|
|
|
|
planetes.innerHTML += previousButton;
|
|
|
|
console.log(document.getElementById("prevButton"));
|
|
|
|
document.getElementById("prevButton").addEventListener("click", () => {
|
|
|
|
api_call(previousUrl, remplissage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
planetes.innerHTML += currentButton;
|
|
|
|
if(!lastPage) {
|
|
|
|
planetes.innerHTML += nextButton;
|
|
|
|
document.getElementById("nextButton").addEventListener("click", () => {
|
|
|
|
api_call(nextUrl, remplissage);
|
|
|
|
});
|
|
|
|
};
|
2021-10-19 07:47:51 +00:00
|
|
|
|
2021-10-19 13:23:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
2021-10-19 10:37:14 +00:00
|
|
|
|
2021-10-19 13:23:08 +00:00
|
|
|
api_call(API_URL + "/planets", remplissage);
|