Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 20 additions & 14 deletions src/tables/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@
}
window.__tablesLoaded = true;

const STORAGE_KEY = "bmiData";
let bmiList = [];

function resolveUrl(relativeToThisScript) {
const base = document.currentScript?.src || window.location.href;
return new URL(relativeToThisScript, base).toString();
}

function calculateBMI(weight, height) {
const w = Number(weight);
const hCm = Number(height);
Expand All @@ -35,6 +31,19 @@
return "Adipositas";
}

function loadFromStorage() {
const data = localStorage.getItem(STORAGE_KEY);
try {
return data ? JSON.parse(data) : [];
} catch {
return [];
}
}

function saveToStorage() {
localStorage.setItem(STORAGE_KEY, JSON.stringify(bmiList));
}

function buildTable(filterSelect, sortSelect, tableBody) {
let list = [...bmiList];
const now = new Date();
Expand Down Expand Up @@ -89,6 +98,7 @@

tr.querySelector("button")?.addEventListener("click", () => {
bmiList.splice(index, 1);
saveToStorage(); // neu speichern
buildTable(filterSelect, sortSelect, tableBody);
});

Expand Down Expand Up @@ -124,15 +134,11 @@
// Prefer stable root path if you serve /src as web root:
// fetch("/data/mock.json")
// Otherwise keep script-relative:
const jsonUrl = resolveUrl("../data/mock.json");
// const jsonUrl = resolveUrl("../data/mock.json");

fetch(jsonUrl, { cache: "no-store" })
.then((res) => res.json())
.then((data) => {
bmiList = data?.tables?.bmiEntries || [];
buildTable(filterSelect, sortSelect, tableBody);
})
.catch((err) => console.error("Fehler beim Laden der JSON:", err));
// Daten jetzt aus LocalStorage laden statt aus mock.json
bmiList = loadFromStorage();
buildTable(filterSelect, sortSelect, tableBody);
}

// Export global, so the router can call it after injecting the view
Expand All @@ -144,4 +150,4 @@
} else {
tablesInit();
}
})();
})();