From 32df39efb8078a1c6a06c25d35c1537db2246807 Mon Sep 17 00:00:00 2001 From: moerp Date: Thu, 4 Apr 2024 13:03:25 +0200 Subject: [PATCH] bug --- src/templates/index.html | 46 +++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 20 deletions(-) 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);