feat: updates during the day without choosing edit date

This commit is contained in:
Greg 2025-05-31 08:13:11 +02:00
parent 0e9660566e
commit 553de56a83
2 changed files with 33 additions and 4 deletions

View File

@ -84,8 +84,9 @@ const UI = (() => {
* Populate the meal form fields based on data for the given date.
* @param {string} dateString - The date (YYYY-MM-DD) to load data for.
*/
const populateMealFormForDate = (dateString) => {
const mealData = DataManager.getMealByDate(dateString);
const populateMealFormForDate = (dateStringFromInput) => {
const isoDateString = Utils.uiDateToISO(dateStringFromInput);
const mealData = DataManager.getMealByDate(isoDateString);
document.getElementById('breakfast').value = mealData?.breakfast || '';
document.getElementById('lunch').value = mealData?.lunch || '';
document.getElementById('dinner').value = mealData?.dinner || '';
@ -140,7 +141,7 @@ const UI = (() => {
e.preventDefault();
const mealEntry = {
date: document.getElementById('meal-date').value,
date: Utils.uiDateToISO(document.getElementById('meal-date').value),
breakfast: document.getElementById('breakfast').value,
lunch: document.getElementById('lunch').value,
dinner: document.getElementById('dinner').value,

View File

@ -175,6 +175,33 @@ const Utils = (() => {
};
// Return public API
/**
* Convert a date string from UI format (DD/MM/YYYY) or existing ISO format (YYYY-MM-DD) to ISO format (YYYY-MM-DD).
* @param {string} dateStr - The date string to convert.
* @returns {string} - The date string in YYYY-MM-DD format, or original if conversion fails.
*/
const uiDateToISO = (dateStr) => {
if (!dateStr) return '';
// Check if already YYYY-MM-DD
if (/^\d{4}-\d{2}-\d{2}$/.test(dateStr)) {
// Potentially validate if it's a real date, but for now, assume format implies intent
return dateStr;
}
// Try to parse as DD/MM/YYYY
const parts = dateStr.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/);
if (parts) {
const day = String(parts[1]).padStart(2, '0');
const month = String(parts[2]).padStart(2, '0');
const year = parts[3];
return `${year}-${month}-${day}`;
}
console.warn(`uiDateToISO: Unrecognized date format "${dateStr}". Returning as is.`);
return dateStr; // Fallback: return original if not recognized
};
return {
formatDate,
getTodayDate,
@ -183,6 +210,7 @@ const Utils = (() => {
validateWeightEntry,
calculateWeightStats,
copyToClipboard,
sanitizeHTML
sanitizeHTML,
uiDateToISO // Export the new function
};
})();