feat: fix date handling issue for the calendar starting on october 31st 2025

This commit is contained in:
2025-10-30 11:29:19 -03:00
parent 73eeebd18c
commit 38efd60297
+47 -3
View File
@@ -64,7 +64,9 @@ async function handleRegister(e: SubmitEvent) {
swal('Registration successful! Welcome to Food Tracker.'); swal('Registration successful! Welcome to Food Tracker.');
} catch (error) { } catch (error) {
console.error('Registration error:', error); console.error('Registration error:', error);
if (error instanceof Error) {
swal('Oh no!', 'Registration failed: ' + error.message, 'error'); swal('Oh no!', 'Registration failed: ' + error.message, 'error');
}
} finally { } finally {
hideLoading(); hideLoading();
} }
@@ -86,7 +88,9 @@ async function handleLogin(e: SubmitEvent) {
} catch (error) { } catch (error) {
console.error('Login error:', error); console.error('Login error:', error);
if (error instanceof Error) {
swal('Oh no!', 'Login failed: ' + error.message, 'error'); swal('Oh no!', 'Login failed: ' + error.message, 'error');
}
} finally { } finally {
hideLoading(); hideLoading();
} }
@@ -156,9 +160,11 @@ async function handleSaveSettings(e: SubmitEvent) {
} catch (error) { } catch (error) {
hideLoading(); hideLoading();
console.error('Saving error:', error); console.error('Saving error:', error);
if (error instanceof Error) {
swal('Oh no!', 'Saving failed: ' + error.message, 'error'); swal('Oh no!', 'Saving failed: ' + error.message, 'error');
} }
} }
}
async function handleLogout() { async function handleLogout() {
showLoading(); showLoading();
@@ -172,9 +178,11 @@ async function handleLogout() {
} catch (error) { } catch (error) {
hideLoading(); hideLoading();
console.error('Logout error:', error); console.error('Logout error:', error);
if (error instanceof Error) {
swal('Oh no!', 'Logout failed: ' + error.message, 'error'); swal('Oh no!', 'Logout failed: ' + error.message, 'error');
} }
} }
}
async function toggleSettingsView() { async function toggleSettingsView() {
const showSettings = getButtonById('settingsBtn').textContent.includes('Settings'); const showSettings = getButtonById('settingsBtn').textContent.includes('Settings');
@@ -209,9 +217,11 @@ async function toggleSettingsView() {
} catch (error) { } catch (error) {
hideLoading(); hideLoading();
console.error('Fetching settings failed:', error); console.error('Fetching settings failed:', error);
if (error instanceof Error) {
swal('Oh no!', 'Fetching settings failed: ' + error.message, 'error'); swal('Oh no!', 'Fetching settings failed: ' + error.message, 'error');
} }
} }
}
else { else {
getDivById('app-content').classList.remove('hidden'); getDivById('app-content').classList.remove('hidden');
getDivById('settings-content').classList.add('hidden'); getDivById('settings-content').classList.add('hidden');
@@ -240,7 +250,11 @@ async function showMainApp() {
const entries = await AppwriteDB.getMonthlyCalories(selectedDate); const entries = await AppwriteDB.getMonthlyCalories(selectedDate);
entries.forEach((entry) => { entries.forEach((entry) => {
const calories = parseInt(entry.totalCalories); const calories = parseInt(entry.totalCalories);
const day = parseInt(entry.date.substring(8)); let day = parseInt(entry.date.substring(8));
// New rule starting in October 30th, 2025
if (selectedDate >= new Date('2025-10-30')) {
day = parseInt(entry.date.substring(8)) + parseInt(entry.date.substring(5,7)) + parseInt(entry.date.substring(0,4));
}
updateDocumentIdForDay(entry.$id, calories, day); updateDocumentIdForDay(entry.$id, calories, day);
}); });
} }
@@ -539,7 +553,9 @@ async function setFoodToEdit(foodId: string) {
hideLoading(); hideLoading();
} catch (error) { } catch (error) {
console.error('Set food to edit error:', error); console.error('Set food to edit error:', error);
if (error instanceof Error) {
swal('Oh no!', 'Failed to load food entry: ' + error.message, 'error'); swal('Oh no!', 'Failed to load food entry: ' + error.message, 'error');
}
} finally { } finally {
hideLoading(); hideLoading();
} }
@@ -586,7 +602,9 @@ async function setFoodToCopy(foodId: string) {
hideLoading(); hideLoading();
} catch (error) { } catch (error) {
console.error('Set food to copy error:', error); console.error('Set food to copy error:', error);
if (error instanceof Error) {
swal('Oh no!', 'Failed to load food entry: ' + error.message, 'error'); swal('Oh no!', 'Failed to load food entry: ' + error.message, 'error');
}
} finally { } finally {
hideLoading(); hideLoading();
} }
@@ -725,7 +743,9 @@ const updateFood = async () => {
selectDate(selectedDate); selectDate(selectedDate);
} catch (error) { } catch (error) {
console.error('Update food error:', error); console.error('Update food error:', error);
if (error instanceof Error) {
swal('Oh no!', 'Failed to update food entry: ' + error.message, 'error'); swal('Oh no!', 'Failed to update food entry: ' + error.message, 'error');
}
} finally { } finally {
hideLoading(); hideLoading();
} }
@@ -746,10 +766,23 @@ const getDocumentIdForDay = (day: number): number => {
} }
const updateDocumentIdForToday = (id: string, totalCalories: number): void => { const updateDocumentIdForToday = (id: string, totalCalories: number): void => {
const today = selectedDate.getDate(); let today = selectedDate.getDate();
// Adopt new rule starting in October 30th, 2025
if (selectedDate >= new Date('2025-10-30')) {
// New rule: today number will be the sum of the day, month, and year
today = selectedDate.getDate() + selectedDate.getMonth() + 1 + selectedDate.getFullYear();
}
updateDocumentIdForDay(id, totalCalories, today); updateDocumentIdForDay(id, totalCalories, today);
} }
/**
* Update or add the document ID for a specific day in the app state. The day is based on the date selected.
* The day parameter is the day of the month (1-31) or the sum of day, month, and year for dates after October 30th, 2025.
*
* @param {string} id - The document ID to set.
* @param {number} totalCalories - The total calories for the day.
* @param {number} day - The day of the month (1-31) or the sum of day, month, and year.
*/
const updateDocumentIdForDay = (id: string, totalCalories: number, day: number): void => { const updateDocumentIdForDay = (id: string, totalCalories: number, day: number): void => {
const record = appState.calendarMonthlyCalories.filter(x => x.day === day); const record = appState.calendarMonthlyCalories.filter(x => x.day === day);
if (record.length === 0) { if (record.length === 0) {
@@ -853,7 +886,9 @@ const addFood = async () => {
renderCalendar(); renderCalendar();
} catch (error) { } catch (error) {
console.error('Add food error:', error); console.error('Add food error:', error);
if (error instanceof Error) {
swal('Oh no!', 'Failed to add food entry: ' + error.message, 'error'); swal('Oh no!', 'Failed to add food entry: ' + error.message, 'error');
}
} finally { } finally {
hideLoading(); hideLoading();
} }
@@ -964,7 +999,9 @@ async function loadFoodEntries(date: Date) {
} }
} catch (error) { } catch (error) {
console.error('Load food entries error:', error); console.error('Load food entries error:', error);
if (error instanceof Error) {
swal('Oh no!', 'Failed to load food entries: ' + error.message, 'error'); swal('Oh no!', 'Failed to load food entries: ' + error.message, 'error');
}
} finally { } finally {
hideLoading(); hideLoading();
} }
@@ -1012,7 +1049,9 @@ async function handleDeleteFood(documentId: string) {
selectDate(selectedDate); selectDate(selectedDate);
} catch (error) { } catch (error) {
console.error('Delete food error:', error); console.error('Delete food error:', error);
if (error instanceof Error) {
swal('Oh no!', 'Failed to delete food entry: ' + error.message, 'error'); swal('Oh no!', 'Failed to delete food entry: ' + error.message, 'error');
}
} finally { } finally {
hideLoading(); hideLoading();
} }
@@ -1191,7 +1230,12 @@ const renderCalendar = async () => {
// Add days of current month // Add days of current month
for (let day = 1; day <= daysInMonth; day++) { for (let day = 1; day <= daysInMonth; day++) {
const dayCalories = getDocumentIdForDay(day); // New rule starting October 30th, 2025
let adjustedDay = day;
if (currentViewDate >= new Date('2025-10-30')) {
adjustedDay = day + (month + 1) + year;
}
const dayCalories = getDocumentIdForDay(adjustedDay);
const dayElement = createDayElement(day, false, year, month, dayCalories); const dayElement = createDayElement(day, false, year, month, dayCalories);
grid.appendChild(dayElement); grid.appendChild(dayElement);
} }