|
|
|
@ -32,6 +32,49 @@
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function recalculateTableBasedOnPortions() {
|
|
|
|
|
const portions = parseInt(document.getElementById('portions').value, 10) || 1;
|
|
|
|
|
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, rowIndex) {
|
|
|
|
|
fetch(`/add_lm?food=${encodeURIComponent(food)}&weight=${encodeURIComponent(weight)}`)
|
|
|
|
|
.then(response => response.json())
|
|
|
|
|
.then(data => {
|
|
|
|
|
// Direktes Aktualisieren der Zeile mit den neuen Daten
|
|
|
|
|
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);
|
|
|
|
|
// Behandlung spezifischer Fehler, z.B. Zurücksetzen der Werte oder Anzeigen einer Fehlermeldung
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateRowWithNewData(row, weight, nutritionData) {
|
|
|
|
|
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);
|
|
|
|
@ -94,10 +137,14 @@
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function removeEntryFromLocalStorage(rowData) {
|
|
|
|
|
let entries = JSON.parse(localStorage.getItem('nutritionEntries')) || [];
|
|
|
|
|
entries = entries.filter(entry => entry.food !== rowData.food || entry.weight !== rowData.weight);
|
|
|
|
|
localStorage.setItem('nutritionEntries', JSON.stringify(entries));
|
|
|
|
|
}
|
|
|
|
|
let entries = JSON.parse(localStorage.getItem('nutritionEntries')) || [];
|
|
|
|
|
// Konvertieren von rowData.weight in eine Zahl, um eine korrekte Vergleichsbasis zu schaffen
|
|
|
|
|
const weightToCompare = parseFloat(rowData.weight);
|
|
|
|
|
// Verwendung einer strikten Gleichheitsprüfung für beide Werte
|
|
|
|
|
entries = entries.filter(entry => entry.food !== rowData.food || parseFloat(entry.weight) !== weightToCompare);
|
|
|
|
|
localStorage.setItem('nutritionEntries', JSON.stringify(entries));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function updateRemoveButtonState() {
|
|
|
|
|
const selectedRows = document.querySelectorAll('#nutrition-table .selected').length;
|
|
|
|
@ -111,11 +158,18 @@
|
|
|
|
|
document.getElementById('submit-button').disabled = !(food && weight);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function addProduct() {
|
|
|
|
|
const foodInput = document.getElementById('my_combobox');
|
|
|
|
|
const weightInput = document.getElementById('weight');
|
|
|
|
|
const portionsInput = document.getElementById('portions');
|
|
|
|
|
const food = foodInput.value;
|
|
|
|
|
const weight = weightInput.value;
|
|
|
|
|
let weight = parseFloat(weightInput.value);
|
|
|
|
|
const portions = portionsInput.value ? parseInt(portionsInput.value, 10) : 1;
|
|
|
|
|
|
|
|
|
|
// Teilen des Gewichts durch die Anzahl der Portionen und Aufrunden
|
|
|
|
|
weight = Math.ceil(weight / portions);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fetch(`/add_lm?food=${encodeURIComponent(food)}&weight=${encodeURIComponent(weight)}`)
|
|
|
|
@ -187,15 +241,22 @@ function updateTotalNutrition() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<div class="content">
|
|
|
|
|
<form onsubmit="event.preventDefault(); addProduct();" id="product-form">
|
|
|
|
|
<label for="my_combobox">Wählen Sie ein Lebensmittel</label>
|
|
|
|
|
<input list="products" name="my_combobox" id="my_combobox" placeholder="Lebensmittel" oninput="updateButtonState()" autocomplete="off">
|
|
|
|
|
<datalist id="products">
|
|
|
|
|
<!-- Produkte werden hier dynamisch eingefügt -->
|
|
|
|
|
</datalist>
|
|
|
|
|
<input type="number" id="weight" name="weight" placeholder="Gramm" oninput="updateButtonState()">
|
|
|
|
|
<button type="submit" id="submit-button" disabled>Hinzufügen</button>
|
|
|
|
|
<form onsubmit="event.preventDefault(); addProduct();" id="product-form" class="form-layout">
|
|
|
|
|
<div class="form-row">
|
|
|
|
|
<label for="portions">Wählen Sie die Anzahl an Portionen</label>
|
|
|
|
|
<input type="number" id="portions" name="portions" placeholder="Portionen" min="1" value="1" oninput="recalculateTableBasedOnPortions()">
|
|
|
|
|
</div>
|
|
|
|
|
<div class="form-row">
|
|
|
|
|
<label for="my_combobox">Wählen Sie ein Lebensmittel</label>
|
|
|
|
|
<input list="products" name="my_combobox" id="my_combobox" placeholder="Lebensmittel" oninput="updateButtonState()" autocomplete="off">
|
|
|
|
|
<datalist id="products">
|
|
|
|
|
<!-- Produkte werden hier dynamisch eingefügt -->
|
|
|
|
|
</datalist>
|
|
|
|
|
<input type="number" id="weight" name="weight" placeholder="Gramm" oninput="updateButtonState()">
|
|
|
|
|
<button type="submit" id="submit-button" disabled>Hinzufügen</button>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<table id="nutrition-table">
|
|
|
|
|
<tr>
|
|
|
|
|