feat: updates during the day without choosing edit date
This commit is contained in:
parent
0e9660566e
commit
553de56a83
7
js/ui.js
7
js/ui.js
@ -84,8 +84,9 @@ const UI = (() => {
|
|||||||
* Populate the meal form fields based on data for the given date.
|
* Populate the meal form fields based on data for the given date.
|
||||||
* @param {string} dateString - The date (YYYY-MM-DD) to load data for.
|
* @param {string} dateString - The date (YYYY-MM-DD) to load data for.
|
||||||
*/
|
*/
|
||||||
const populateMealFormForDate = (dateString) => {
|
const populateMealFormForDate = (dateStringFromInput) => {
|
||||||
const mealData = DataManager.getMealByDate(dateString);
|
const isoDateString = Utils.uiDateToISO(dateStringFromInput);
|
||||||
|
const mealData = DataManager.getMealByDate(isoDateString);
|
||||||
document.getElementById('breakfast').value = mealData?.breakfast || '';
|
document.getElementById('breakfast').value = mealData?.breakfast || '';
|
||||||
document.getElementById('lunch').value = mealData?.lunch || '';
|
document.getElementById('lunch').value = mealData?.lunch || '';
|
||||||
document.getElementById('dinner').value = mealData?.dinner || '';
|
document.getElementById('dinner').value = mealData?.dinner || '';
|
||||||
@ -140,7 +141,7 @@ const UI = (() => {
|
|||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
const mealEntry = {
|
const mealEntry = {
|
||||||
date: document.getElementById('meal-date').value,
|
date: Utils.uiDateToISO(document.getElementById('meal-date').value),
|
||||||
breakfast: document.getElementById('breakfast').value,
|
breakfast: document.getElementById('breakfast').value,
|
||||||
lunch: document.getElementById('lunch').value,
|
lunch: document.getElementById('lunch').value,
|
||||||
dinner: document.getElementById('dinner').value,
|
dinner: document.getElementById('dinner').value,
|
||||||
|
|||||||
30
js/utils.js
30
js/utils.js
@ -175,6 +175,33 @@ const Utils = (() => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Return public API
|
// 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 {
|
return {
|
||||||
formatDate,
|
formatDate,
|
||||||
getTodayDate,
|
getTodayDate,
|
||||||
@ -183,6 +210,7 @@ const Utils = (() => {
|
|||||||
validateWeightEntry,
|
validateWeightEntry,
|
||||||
calculateWeightStats,
|
calculateWeightStats,
|
||||||
copyToClipboard,
|
copyToClipboard,
|
||||||
sanitizeHTML
|
sanitizeHTML,
|
||||||
|
uiDateToISO // Export the new function
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user