diff --git a/scripts/planets.js b/scripts/planets.js
index 75441ed..e0fb475 100644
--- a/scripts/planets.js
+++ b/scripts/planets.js
@@ -1,4 +1,4 @@
-import { api_call, API_URL, getPage } from "./util";
+import { api_call, API_URL, createPagination } from "./util";
const planetes = document.getElementById("resultats");
@@ -49,43 +49,7 @@ function remplissage(results) {
}
//Pagination
- 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);
-
- let firstPage = false;
- let lastPage = false;
- if (previousUrl == null) firstPage = true;
- if (nextUrl == null) lastPage = true;
- let previousButton = ``;
- let currentButton = `${currentPage}`;
- let nextButton = ``;
-
- planetes.innerHTML += previousButton;
- planetes.innerHTML += currentButton;
- 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);
- });
+ createPagination(previousUrl, nextUrl, planetes, remplissage);
}
api_call(API_URL + "/planets", remplissage);
diff --git a/scripts/util.js b/scripts/util.js
index 9397675..446b1ee 100644
--- a/scripts/util.js
+++ b/scripts/util.js
@@ -26,7 +26,7 @@ export const api_call = (url, callback) => {
* @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
*/
-export const getPage = (url) => {
+ const getPage = (url) => {
tabUrl = url.split('/');
pageEgal = tabUrl[tabUrl.length - 1];
tabPageEgal = pageEgal.split('=');
@@ -34,3 +34,49 @@ export const getPage = (url) => {
return numeroPage;
}
+
+/**
+ * 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 = ``;
+ let currentButton = `${currentPage}`;
+ let nextButton = ``;
+
+ 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);
+ });
+}