From bc49332321da7be0dea99003f7d13ddccb83eeed Mon Sep 17 00:00:00 2001 From: anto Date: Thu, 24 Oct 2024 16:35:26 +0200 Subject: [PATCH] test --- html/projet_detail.html | 2 +- js/globals.js | 42 ++++++++++++++++++++--------------------- js/index.js | 16 ++++++++-------- js/projet_detail.js | 29 ++++++++++++++++++++++++++-- 4 files changed, 57 insertions(+), 32 deletions(-) diff --git a/html/projet_detail.html b/html/projet_detail.html index 71aed8e..a9abccb 100644 --- a/html/projet_detail.html +++ b/html/projet_detail.html @@ -26,7 +26,7 @@ - + diff --git a/js/globals.js b/js/globals.js index 2b81abe..4795d66 100644 --- a/js/globals.js +++ b/js/globals.js @@ -3,7 +3,6 @@ let api_url = "https://timelapse.kerboul.me/api"; let global_project_list; let current_project = ""; - function formatDate(isoString) { const date = new Date(isoString); @@ -14,32 +13,33 @@ function formatDate(isoString) { hour: "numeric", minute: "numeric", second: "numeric", - timeZoneName: "short" + timeZoneName: "short", }; return date.toLocaleString("en-US", options); } -const readableString = date.toLocaleString("en-US", options); -console.log(readableString); // Function to get data from API -function getDataFromApi() { - // const cachedData = localStorage.getItem("project_list"); - // if (cachedData) { - // // If the data is in the cache, parse it and return it - // return Promise.resolve(JSON.parse(cachedData)); - // } else { - // If the data is not in the cache, make the API request - return $.ajax({ - url: api_url.concat("/itemsdb"), - method: "GET", - dataType: "json", - }).then(data => { - // If the request is successful, store the data in the cache and return it - localStorage.setItem("project_list", JSON.stringify(data)); - return data; - }); - // } +function getDataProjectFromApi() { + return $.ajax({ + url: api_url.concat("/itemsdb"), + method: "GET", + dataType: "json", + }).then((data) => { + // If the request is successful, store the data in the cache and return it + localStorage.setItem("project_list", JSON.stringify(data)); + return data; + }); +} +function getDataProjectMetricsFromApi(id) { + return $.ajax({ + url: api_url.concat(`/metric/${id}`), + method: "GET", + dataType: "json", + }).then((data) => { + // If the request is successful, store the data in the cache and return it + return data; + }); } // Function to send data to API diff --git a/js/index.js b/js/index.js index 7e89bdd..a2bed3d 100644 --- a/js/index.js +++ b/js/index.js @@ -9,13 +9,13 @@ function display_projects() { `; - for (let i = 0; i < global_project_list.rows.length; i++) { + for (let i = 0; i < global_project_list.length; i++) { datas += ` - - - - - + + + + + `; } table.innerHTML = datas; @@ -26,7 +26,7 @@ function display_projects() { for (let i = 0; i < buttons.length; i++) { buttons[i].addEventListener("click", (event) => { // Send data to API and then navigate to projet_detail.html page - window.location.href = `html/projet_detail.html?id=${global_project_list.rows[i].id}`; + window.location.href = `html/projet_detail.html?id=${global_project_list[i].id}`; }); buttons[i].addEventListener("onclick", (event) => { // Send data to API and then navigate to projet_detail.html page @@ -34,7 +34,7 @@ function display_projects() { }); } } -getDataFromApi() +getDataProjectFromApi() .then(project_list => { global_project_list = project_list; // Call the next function here diff --git a/js/projet_detail.js b/js/projet_detail.js index 5ad57cd..0648e5c 100644 --- a/js/projet_detail.js +++ b/js/projet_detail.js @@ -1,12 +1,37 @@ +function display_metrics(metrics_datas) { + // Get data from API and then generate HTML code to display the data in a table + const table = document.getElementById("table-metrics"); + let datas = ` + + + + `; + for (let i = 0; i < metrics_datas.length; i++) { + datas += ` + + + + `; + } + table.innerHTML = datas; +} + document.addEventListener("DOMContentLoaded", () => { const urlParams = new URLSearchParams(window.location.search); const projectId = urlParams.get("id"); - + getDataProjectMetricsFromApi(projectId) + .then(project_metrics => { + console.log(project_metrics) + display_metrics(project_metrics); + }) + .catch(error => { + console.error(error); + }); document.getElementById("projets").addEventListener("click", () => { window.location.href = "../index.html"; current_project=""; }); global_project_list=JSON.parse(localStorage.getItem("project_list")); - document.getElementById("name_project").innerHTML=global_project_list.rows[projectId-1].titre; + document.getElementById("name_project").innerHTML=global_project_list[projectId-1].titre; });
Métriques date Hygrométrie température
Status Actions
${global_project_list.rows[i].id}${global_project_list.rows[i].titre}${formatDate(global_project_list.rows[i].creation)}${global_project_list.rows[i].status}${global_project_list[i].id}${global_project_list[i].titre}${formatDate(global_project_list[i].creation)}${global_project_list[i].status}
DateTempératureHygrométrie
${formatDate(metrics_datas[i].date_metrique)}${metrics_datas[i].temperature}${metrics_datas[i].hygrometrie}