added cookies

This commit is contained in:
moerp 2024-01-30 11:48:43 +01:00
parent b754bfb836
commit c702cfb22a

View File

@ -9,6 +9,9 @@
// JavaScript-Funktion, um die Produkte beim Laden der Seite zu holen
document.addEventListener('DOMContentLoaded', function() {
cleanUpLocalStorage();
restoreTableFromLocalStorage();
updateTotalNutrition();
fetch('/get_products')
.then(response => response.json())
.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() {
const selectedRows = document.querySelectorAll('#nutrition-table .selected').length;
@ -40,6 +78,15 @@
const table = document.getElementById('nutrition-table');
Array.from(table.rows).forEach(row => {
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);
}
});
@ -47,6 +94,12 @@
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() {
const selectedRows = document.querySelectorAll('#nutrition-table .selected').length;
document.getElementById('remove-button').disabled = selectedRows === 0;
@ -86,9 +139,15 @@
document.getElementById('submit-button').disabled = true; // Deaktivieren des Hinzufügen-Buttons
updateTotalNutrition();
saveToLocalStorage(food, weight, data);
})
.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() {