2021-10-18 13:24:38 +00:00
|
|
|
console.log("util.js loaded");
|
|
|
|
|
2021-10-19 08:31:51 +00:00
|
|
|
export const API_URL = "https://swapi.dev/api";
|
2021-10-18 13:24:38 +00:00
|
|
|
|
|
|
|
/**
|
2021-10-18 19:27:45 +00:00
|
|
|
* Envoie une requête GET à l'api swappi
|
2021-10-18 13:24:38 +00:00
|
|
|
* @param {string} endpoint l'endpoint à interroger (ex: planets)
|
|
|
|
* @param {function} callback La fonction qui traitera la réponse JSON en retour
|
|
|
|
*/
|
2021-10-19 08:31:51 +00:00
|
|
|
export const api_call = (url, callback) => {
|
|
|
|
fetch(url)
|
2021-10-18 13:24:38 +00:00
|
|
|
.then(response => {
|
|
|
|
return response.json();
|
|
|
|
})
|
|
|
|
.then(jsonResp => {
|
|
|
|
callback(jsonResp);
|
2021-10-18 19:27:45 +00:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.log(`Erreur lors de l'appel API :
|
|
|
|
${error.message}`);
|
2021-10-18 13:24:38 +00:00
|
|
|
});
|
2021-10-18 15:32:20 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 10:37:14 +00:00
|
|
|
/**
|
|
|
|
* Récupère le numéro de la page courante
|
|
|
|
* @param {string} url l'url d'un résultat multipage (qui se termine par ?page=2 par exemple)
|
|
|
|
* @returns Le numéro de la page de l'url
|
|
|
|
*/
|
2021-10-19 14:47:46 +00:00
|
|
|
const getPage = (url) => {
|
2021-10-19 10:37:14 +00:00
|
|
|
tabUrl = url.split('/');
|
|
|
|
pageEgal = tabUrl[tabUrl.length - 1];
|
|
|
|
tabPageEgal = pageEgal.split('=');
|
|
|
|
numeroPage = tabPageEgal[tabPageEgal.length - 1];
|
|
|
|
|
|
|
|
return numeroPage;
|
|
|
|
}
|
2021-10-19 14:47:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ajoutes des boutons de pagination à un élément
|
|
|
|
* @param {string} prevUrl l'url de la page précédente
|
|
|
|
* @param {string} nextUrl l'url de la page suivante
|
|
|
|
* @param {object} elementParent L'objet parent dans lequel ajouter les boutons
|
|
|
|
*/
|
|
|
|
export const createPagination = (prevUrl, nextUrl, elementParent, remplissage) => {
|
|
|
|
console.log("Url précédente : " + prevUrl);
|
|
|
|
console.group("Url suivante : " + nextUrl);
|
|
|
|
const currentPage = (prevUrl == null ? parseInt(getPage(nextUrl)) - 1 : parseInt(getPage(prevUrl)) + 1);
|
|
|
|
console.log("Page courante : " + currentPage);
|
|
|
|
|
|
|
|
let firstPage = false;
|
|
|
|
let lastPage = false;
|
|
|
|
if (prevUrl == 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>`;
|
|
|
|
|
|
|
|
elementParent.innerHTML += previousButton;
|
|
|
|
elementParent.innerHTML += currentButton;
|
|
|
|
elementParent.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(prevUrl, remplissage);
|
|
|
|
});
|
|
|
|
document.getElementById("nextButton").addEventListener("click", () => {
|
|
|
|
api_call(nextUrl, remplissage);
|
|
|
|
});
|
|
|
|
}
|