diff --git a/src/templates/index.html b/src/templates/index.html
index 032cac5..e992c1c 100644
--- a/src/templates/index.html
+++ b/src/templates/index.html
@@ -34,41 +34,47 @@
function recalculateTableBasedOnPortions() {
const portions = parseInt(document.getElementById('portions').value, 10) || 1;
- const table = document.getElementById('nutrition-table');
- Array.from(table.rows).slice(1).forEach((row, index) => {
- // Wir gehen davon aus, dass die Einträge im localStorage gespeichert sind
- let entries = JSON.parse(localStorage.getItem('nutritionEntries')) || [];
- if (entries[index]) {
- const entry = entries[index];
- const newWeight = Math.ceil(entry.weight / portions);
- fetchUpdatedNutrition(entry.food, newWeight, row);
- }
+ let entries = JSON.parse(localStorage.getItem('nutritionEntries')) || [];
+ entries.forEach((entry, index) => {
+ const newWeight = Math.ceil(entry.weight / portions);
+ // Da die Server-Anfrage möglicherweise nicht sofort antwortet, passen wir den Index an, um die korrekte Zeile zu aktualisieren.
+ fetchUpdatedNutrition(entry.food, newWeight, index + 1); // +1, um den Kopfzeilenindex zu überspringen
});
}
-function fetchUpdatedNutrition(food, weight, rowToUpdate) {
+function fetchUpdatedNutrition(food, weight, rowIndex) {
fetch(`/add_lm?food=${encodeURIComponent(food)}&weight=${encodeURIComponent(weight)}`)
.then(response => response.json())
.then(data => {
// Direktes Aktualisieren der Zeile mit den neuen Daten
- updateRowWithNewData(rowToUpdate, weight, data);
+ const table = document.getElementById('nutrition-table');
+ if (table.rows[rowIndex]) {
+ updateRowWithNewData(table.rows[rowIndex], weight, data);
+ }
updateTotalNutrition(); // Aktualisiert die Gesamtnährwerte
})
- .catch(error => console.error('Fehler:', error));
+ .catch(error => {
+ console.error('Fehler:', error);
+ // Behandlung spezifischer Fehler, z.B. Zurücksetzen der Werte oder Anzeigen einer Fehlermeldung
+ });
}
function updateRowWithNewData(row, weight, nutritionData) {
- row.cells[1].innerText = weight; // Aktualisiere Gewicht
- row.cells[2].innerText = nutritionData.kcal;
- row.cells[3].innerText = nutritionData.ew;
- row.cells[4].innerText = nutritionData.fett;
- row.cells[5].innerText = nutritionData.kh;
- row.cells[6].innerText = nutritionData.bst;
- row.cells[7].innerText = nutritionData.ca;
+ if (nutritionData && row.cells.length > 7) {
+ row.cells[1].innerText = weight; // Aktualisiere Gewicht
+ row.cells[2].innerText = nutritionData.kcal;
+ row.cells[3].innerText = nutritionData.ew;
+ row.cells[4].innerText = nutritionData.fett;
+ row.cells[5].innerText = nutritionData.kh;
+ row.cells[6].innerText = nutritionData.bst;
+ row.cells[7].innerText = nutritionData.ca;
+ } else {
+ // Fehlerbehandlung, falls die Datenstruktur nicht wie erwartet ist
+ console.error('Unvollständige Daten erhalten: ', nutritionData);
+ }
}
-
function restoreTableFromLocalStorage() {
let entries = JSON.parse(localStorage.getItem('nutritionEntries')) || [];
let oneHourAgo = new Date().getTime() - (60 * 60 * 1000);