From 553de56a83b4ffa47372e578f51a89d3b7befb6d Mon Sep 17 00:00:00 2001 From: Greg Date: Sat, 31 May 2025 08:13:11 +0200 Subject: [PATCH] feat: updates during the day without choosing edit date --- js/ui.js | 7 ++++--- js/utils.js | 30 +++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/js/ui.js b/js/ui.js index a7fd183..28fc687 100644 --- a/js/ui.js +++ b/js/ui.js @@ -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, diff --git a/js/utils.js b/js/utils.js index c1425ce..5dbb02b 100644 --- a/js/utils.js +++ b/js/utils.js @@ -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 }; })();