Merge branch 'main' into containerized

This commit is contained in:
Wolfgang Hottgenroth 2024-01-30 11:56:53 +01:00
commit 0bca4ba03b
Signed by: wn
GPG Key ID: 836E9E1192A6B132

View File

@ -9,6 +9,9 @@
// JavaScript-Funktion, um die Produkte beim Laden der Seite zu holen // JavaScript-Funktion, um die Produkte beim Laden der Seite zu holen
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function() {
cleanUpLocalStorage();
restoreTableFromLocalStorage();
updateTotalNutrition();
fetch('/get_products') fetch('/get_products')
.then(response => response.json()) .then(response => response.json())
.then(data => { .then(data => {
@ -30,6 +33,41 @@
}); });
}); });
function restoreTableFromLocalStorage() {
let entries = JSON.parse(localStorage.getItem('nutritionEntries')) || [];
let oneHourAgo = new Date().getTime() - (60 * 60 * 1000);
entries = entries.filter(entry => entry.timestamp > oneHourAgo); // Entfernen alter Einträge
entries.forEach(entry => {
addToTable(entry.food, entry.weight, entry.nutritionData);
});
localStorage.setItem('nutritionEntries', JSON.stringify(entries)); // Aktualisiere localStorage
}
function addToTable(food, weight, nutritionData) {
const table = document.getElementById('nutrition-table');
const row = table.insertRow();
row.insertCell(0).innerHTML = food;
row.insertCell(1).innerHTML = weight;
row.insertCell(2).innerHTML = nutritionData.kcal;
row.insertCell(3).innerHTML = nutritionData.ew;
row.insertCell(4).innerHTML = nutritionData.fett;
row.insertCell(5).innerHTML = nutritionData.kh;
row.insertCell(6).innerHTML = nutritionData.bst;
row.insertCell(7).innerHTML = nutritionData.ca;
}
function cleanUpLocalStorage() {
let entries = JSON.parse(localStorage.getItem('nutritionEntries')) || [];
let oneHourAgo = new Date().getTime() - (60 * 60 * 1000);
entries = entries.filter(entry => entry.timestamp > oneHourAgo);
localStorage.setItem('nutritionEntries', JSON.stringify(entries));
}
function updateRemoveButtonState() { function updateRemoveButtonState() {
const selectedRows = document.querySelectorAll('#nutrition-table .selected').length; const selectedRows = document.querySelectorAll('#nutrition-table .selected').length;
@ -40,6 +78,15 @@
const table = document.getElementById('nutrition-table'); const table = document.getElementById('nutrition-table');
Array.from(table.rows).forEach(row => { Array.from(table.rows).forEach(row => {
if (row.classList.contains('selected')) { if (row.classList.contains('selected')) {
let rowData = {
food: row.cells[0].innerText,
weight: row.cells[1].innerText,
// Weitere Daten bei Bedarf
};
// Entfernen des Eintrags aus dem lokalen Speicher
removeEntryFromLocalStorage(rowData);
table.deleteRow(row.rowIndex); table.deleteRow(row.rowIndex);
} }
}); });
@ -47,6 +94,12 @@
updateTotalNutrition(); updateTotalNutrition();
} }
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));
}
function updateRemoveButtonState() { function updateRemoveButtonState() {
const selectedRows = document.querySelectorAll('#nutrition-table .selected').length; const selectedRows = document.querySelectorAll('#nutrition-table .selected').length;
document.getElementById('remove-button').disabled = selectedRows === 0; document.getElementById('remove-button').disabled = selectedRows === 0;
@ -86,9 +139,15 @@
document.getElementById('submit-button').disabled = true; // Deaktivieren des Hinzufügen-Buttons document.getElementById('submit-button').disabled = true; // Deaktivieren des Hinzufügen-Buttons
updateTotalNutrition(); updateTotalNutrition();
saveToLocalStorage(food, weight, data);
}) })
.catch(error => console.error('Fehler:', error)); .catch(error => console.error('Fehler:', error));
}
function saveToLocalStorage(food, weight, nutritionData) {
let entries = JSON.parse(localStorage.getItem('nutritionEntries')) || [];
entries.push({ food, weight, nutritionData, timestamp: new Date().getTime() });
localStorage.setItem('nutritionEntries', JSON.stringify(entries));
} }
function updateTotalNutrition() { function updateTotalNutrition() {