fix: alkaline percentage tracker

This commit is contained in:
2025-09-04 15:02:43 -03:00
parent fa4a380b9a
commit f8440ac3e5
+16 -23
View File
@@ -12,9 +12,6 @@ let searchTimeout: number | null = null;
let selectedFood: FoodItem | null = null; // review here let selectedFood: FoodItem | null = null; // review here
let currentHighlightIndex: number = -1; let currentHighlightIndex: number = -1;
let currentResults: FoodItem[] = []; let currentResults: FoodItem[] = [];
let totalCaloriesToday: number = 0;
let totalGramsToday: number = 0;
let totalAlkalineToday: number = 0;
// Authentication state // Authentication state
let currentUser: any | null = null; let currentUser: any | null = null;
@@ -347,9 +344,6 @@ function clearAppData() {
</div> </div>
`; `;
selectedFood = null; selectedFood = null;
totalCaloriesToday = 0;
totalGramsToday = 0;
totalAlkalineToday = 0;
getButtonById('cancel-food-btn').style.display = 'none'; getButtonById('cancel-food-btn').style.display = 'none';
} }
@@ -1058,6 +1052,7 @@ function addFoodToTable(foodData: FoodStorage, documentId: string) {
<div class="nutrition-label">Fiber</div> <div class="nutrition-label">Fiber</div>
<div class="nutrition-value">${foodData.fiber}g</div> <div class="nutrition-value">${foodData.fiber}g</div>
</div> </div>
<div class="food-is-alkaline hidden">${foodData.alkaline ? 'alkaline' : ''}</div>
</div> </div>
<div class="card-actions"> <div class="card-actions">
<button class="btn btn-edit" data-food-id="${documentId}">Edit</button> <button class="btn btn-edit" data-food-id="${documentId}">Edit</button>
@@ -1075,30 +1070,31 @@ function addFoodToTable(foodData: FoodStorage, documentId: string) {
// Update total calories // Update total calories
function updateTotalCalories(entries: Models.DefaultDocument[]) { function updateTotalCalories(entries: Models.DefaultDocument[]) {
let total = totalCaloriesToday; const caloriesDiv = document.querySelectorAll('.calories-display');
let total = 0;
entries.forEach((entry: Models.DefaultDocument) => { caloriesDiv.forEach((innerDiv: Element) => {
total += entry.calories || 0; total += parseInt(innerDiv.innerHTML.replace(' cal', '')) || 0;
}); });
getDivById('caloriesCounter').textContent = total.toString(); getDivById('caloriesCounter').textContent = total.toString();
totalCaloriesToday = total;
// FIX ME: alkaline count
// Update alkaline level // Update alkaline level
let totalGrams = totalGramsToday; const gramsDivs = document.querySelectorAll('.food-time');
let totalGrams = 0;
let indexMap: {index: number, grams: number}[] = []; let indexMap: {index: number, grams: number}[] = [];
entries.forEach((entry: Models.DefaultDocument, key: number) => { gramsDivs.forEach((innerDiv: Element, key: number) => {
totalGrams += entry.grams || 0; const totalGramDiv = parseInt(innerDiv.innerHTML.split(' ')[2].replace('g', ''));
totalGrams += totalGramDiv || 0;
indexMap.push({ indexMap.push({
index: key, index: key,
grams: entry.grams || 0 grams: totalGramDiv || 0
}); });
}); });
let totalAlkaline = totalAlkalineToday; const alkalineDivs = document.querySelectorAll('.food-is-alkaline');
entries.forEach((entry: Models.DefaultDocument, key: number) => { let totalAlkaline = 0;
if (entry.alkaline) { alkalineDivs.forEach((alkaDiv: Element, key: number) => {
if (alkaDiv.innerHTML === 'alkaline') {
for (let ob of indexMap) { for (let ob of indexMap) {
if (ob.index === key) { if (ob.index === key) {
totalAlkaline += ob.grams; totalAlkaline += ob.grams;
@@ -1125,15 +1121,12 @@ const nextMonth = () => {
const selectDate = async (date: Date) => { const selectDate = async (date: Date) => {
selectedDate = date; selectedDate = date;
totalCaloriesToday = 0;
totalGramsToday = 0;
totalAlkalineToday = 0;
updateCurrentDate(); updateCurrentDate();
await loadFoodEntries(date); await loadFoodEntries(date);
renderCalendar(); renderCalendar();
} }
const renderCalendar = () => { const renderCalendar = async () => {
const year = currentViewDate.getFullYear(); const year = currentViewDate.getFullYear();
const month = currentViewDate.getMonth(); const month = currentViewDate.getMonth();