From eb7def39ac53729d7a1a55ede1d66d235525ce90 Mon Sep 17 00:00:00 2001 From: Kamron Khakimov Date: Mon, 23 Feb 2026 10:50:39 +0100 Subject: [PATCH 1/3] feat: use new local storage key for fetching data --- docs/Formular.md | 2 +- src/barchart/script.js | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/Formular.md b/docs/Formular.md index bd80621..3bfe596 100644 --- a/docs/Formular.md +++ b/docs/Formular.md @@ -48,7 +48,7 @@ Daten werden nach der Berechnung als JSON-String gespeichert: "height": "180", "bmi": 23.1, "category": "Normalgewicht", - "timestamp": "2025-02-23T14:30:45.123Z" + "timestamp": "2025-02-23T14:30:45.123Z" // new Date().toISOString() } ``` diff --git a/src/barchart/script.js b/src/barchart/script.js index 62c3f8d..32527d5 100644 --- a/src/barchart/script.js +++ b/src/barchart/script.js @@ -10,7 +10,7 @@ const ctx = document.getElementById("bmiChart"); const USER_HEIGHT_CM = 170; // <-- change once, used everywhere const USER_HEIGHT_M = USER_HEIGHT_CM / 100; -let bmiHistory = JSON.parse(localStorage.getItem("bmiHistory")) || []; +let bmiData = JSON.parse(localStorage.getItem("bmiData")) || []; function getBMIColor(bmi) { if (bmi < 18.5) return "#74c0fc"; // Underweight @@ -22,12 +22,12 @@ function getBMIColor(bmi) { const bmiChart = new Chart(ctx, { type: "bar", data: { - labels: bmiHistory.map((e) => e.date), + labels: bmiData.map((e) => e.timestamp), datasets: [ { label: "BMI", - data: bmiHistory.map((e) => e.bmi), - backgroundColor: bmiHistory.map((e) => getBMIColor(e.bmi)), + data: bmiData.map((e) => e.bmi), + backgroundColor: bmiData.map((e) => getBMIColor(e.bmi)), borderRadius: 6, }, ], @@ -71,9 +71,9 @@ function addBMI() { bmi, }; - bmiHistory.push(entry); + bmiData.push(entry); - localStorage.setItem("bmiHistory", JSON.stringify(bmiHistory)); + localStorage.setItem("bmiData", JSON.stringify(bmiData)); bmiChart.data.labels.push(entry.date); bmiChart.data.datasets[0].data.push(entry.bmi); From 5c1de1189173c91ae600c7eccff61ff074d5dbf1 Mon Sep 17 00:00:00 2001 From: Kamron Khakimov Date: Mon, 23 Feb 2026 11:09:15 +0100 Subject: [PATCH 2/3] refactor: remove unused code --- src/barchart/script.js | 44 +++--------------------------------------- 1 file changed, 3 insertions(+), 41 deletions(-) diff --git a/src/barchart/script.js b/src/barchart/script.js index 32527d5..23efb32 100644 --- a/src/barchart/script.js +++ b/src/barchart/script.js @@ -1,16 +1,5 @@ const ctx = document.getElementById("bmiChart"); - -/* - Height is profile data. - Later this can come from: - - localStorage - - backend API - - user profile page -*/ -const USER_HEIGHT_CM = 170; // <-- change once, used everywhere -const USER_HEIGHT_M = USER_HEIGHT_CM / 100; - -let bmiData = JSON.parse(localStorage.getItem("bmiData")) || []; +const bmiData = JSON.parse(localStorage.getItem("bmiData")) || []; function getBMIColor(bmi) { if (bmi < 18.5) return "#74c0fc"; // Underweight @@ -22,7 +11,7 @@ function getBMIColor(bmi) { const bmiChart = new Chart(ctx, { type: "bar", data: { - labels: bmiData.map((e) => e.timestamp), + labels: bmiData?.map((e) => e.timestamp), datasets: [ { label: "BMI", @@ -54,31 +43,4 @@ const bmiChart = new Chart(ctx, { }, }, }, -}); - -function addBMI() { - const weight = parseFloat(document.getElementById("weight").value); - - if (!weight) { - alert("Please enter weight"); - return; - } - - const bmi = +(weight / (USER_HEIGHT_M * USER_HEIGHT_M)).toFixed(1); - - const entry = { - date: new Date().toLocaleDateString(), - bmi, - }; - - bmiData.push(entry); - - localStorage.setItem("bmiData", JSON.stringify(bmiData)); - - bmiChart.data.labels.push(entry.date); - bmiChart.data.datasets[0].data.push(entry.bmi); - bmiChart.data.datasets[0].backgroundColor.push(getBMIColor(entry.bmi)); - bmiChart.update(); - - document.getElementById("weight").value = ""; -} +}); \ No newline at end of file From 98b24d4a0b7cb1f8f9439e7c98b7b20d8d62be81 Mon Sep 17 00:00:00 2001 From: Kamron Khakimov Date: Mon, 2 Mar 2026 10:19:26 +0100 Subject: [PATCH 3/3] fix: typo in localstorage save --- src/formular/formular.js | 6 +++--- src/{app.html => index.html} | 0 2 files changed, 3 insertions(+), 3 deletions(-) rename src/{app.html => index.html} (100%) diff --git a/src/formular/formular.js b/src/formular/formular.js index c05c979..0b11826 100644 --- a/src/formular/formular.js +++ b/src/formular/formular.js @@ -81,9 +81,9 @@ function saveToLocalStorage(age, date, weight, height, bmi, category) { timestamp: new Date().toISOString(), }; - let bmiHistory = JSON.parse(localStorage.getItem("bmiData") || "[]"); - bmiHistory.push(newdata); - localStorage.setItem("bmiData", JSON.stringify(bmiHistory)); + let bmiData = JSON.parse(localStorage.getItem("bmiData") || "[]"); + bmiData.push(newdata); + localStorage.setItem("bmiData", JSON.stringify(bmiData)); } diff --git a/src/app.html b/src/index.html similarity index 100% rename from src/app.html rename to src/index.html