Make similar food suggestions clickable to populate food entry form (#12)
This commit is contained in:
Generated
-27
@@ -2288,9 +2288,6 @@
|
|||||||
"arm"
|
"arm"
|
||||||
],
|
],
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"libc": [
|
|
||||||
"glibc"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"os": [
|
"os": [
|
||||||
@@ -2305,9 +2302,6 @@
|
|||||||
"arm"
|
"arm"
|
||||||
],
|
],
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"libc": [
|
|
||||||
"musl"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"os": [
|
"os": [
|
||||||
@@ -2322,9 +2316,6 @@
|
|||||||
"arm64"
|
"arm64"
|
||||||
],
|
],
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"libc": [
|
|
||||||
"glibc"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"os": [
|
"os": [
|
||||||
@@ -2339,9 +2330,6 @@
|
|||||||
"arm64"
|
"arm64"
|
||||||
],
|
],
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"libc": [
|
|
||||||
"musl"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"os": [
|
"os": [
|
||||||
@@ -2356,9 +2344,6 @@
|
|||||||
"loong64"
|
"loong64"
|
||||||
],
|
],
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"libc": [
|
|
||||||
"glibc"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"os": [
|
"os": [
|
||||||
@@ -2373,9 +2358,6 @@
|
|||||||
"ppc64"
|
"ppc64"
|
||||||
],
|
],
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"libc": [
|
|
||||||
"glibc"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"os": [
|
"os": [
|
||||||
@@ -2390,9 +2372,6 @@
|
|||||||
"riscv64"
|
"riscv64"
|
||||||
],
|
],
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"libc": [
|
|
||||||
"glibc"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"os": [
|
"os": [
|
||||||
@@ -2407,9 +2386,6 @@
|
|||||||
"riscv64"
|
"riscv64"
|
||||||
],
|
],
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"libc": [
|
|
||||||
"musl"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"os": [
|
"os": [
|
||||||
@@ -2424,9 +2400,6 @@
|
|||||||
"s390x"
|
"s390x"
|
||||||
],
|
],
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"libc": [
|
|
||||||
"glibc"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"os": [
|
"os": [
|
||||||
|
|||||||
+31
-7
@@ -36,6 +36,13 @@ let currentViewDate = new Date();
|
|||||||
let selectedFood: FoodItem | null = null; // review here
|
let selectedFood: FoodItem | null = null; // review here
|
||||||
let currentUser: any | null = null;
|
let currentUser: any | null = null;
|
||||||
|
|
||||||
|
type AlternativeResult = {
|
||||||
|
food: FoodItem;
|
||||||
|
quantityGrams: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
let currentAlternatives: AlternativeResult[] = [];
|
||||||
|
|
||||||
// Initialize when page loads
|
// Initialize when page loads
|
||||||
document.addEventListener('DOMContentLoaded', async function() {
|
document.addEventListener('DOMContentLoaded', async function() {
|
||||||
await initializeAuth();
|
await initializeAuth();
|
||||||
@@ -477,11 +484,6 @@ const getFoodItemByName = (foodName: string): FoodItem => {
|
|||||||
return foodDataSearch[0];
|
return foodDataSearch[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
type AlternativeResult = {
|
|
||||||
food: FoodItem;
|
|
||||||
quantityGrams: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
function findAlternatives(
|
function findAlternatives(
|
||||||
current: FoodItem,
|
current: FoodItem,
|
||||||
currentQuantityGrams: number,
|
currentQuantityGrams: number,
|
||||||
@@ -602,6 +604,7 @@ function updateAlternativesDisplay() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const alternatives = findAlternatives(selectedFood, grams, foodDatabase);
|
const alternatives = findAlternatives(selectedFood, grams, foodDatabase);
|
||||||
|
currentAlternatives = alternatives;
|
||||||
|
|
||||||
const escapeHtml = (text: string): string =>
|
const escapeHtml = (text: string): string =>
|
||||||
text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
|
text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
|
||||||
@@ -610,12 +613,27 @@ function updateAlternativesDisplay() {
|
|||||||
if (alternatives.length === 0) {
|
if (alternatives.length === 0) {
|
||||||
list.innerHTML = '<div class="no-alternatives">No equivalent alternatives found</div>';
|
list.innerHTML = '<div class="no-alternatives">No equivalent alternatives found</div>';
|
||||||
} else {
|
} else {
|
||||||
list.innerHTML = alternatives.map(alt => `
|
list.innerHTML = alternatives.map((alt, index) => `
|
||||||
<div class="alternative-item">
|
<div class="alternative-item" data-index="${index}" tabindex="0" title="Click to select this food">
|
||||||
<span class="alternative-name">${escapeHtml(alt.food.name)}</span>
|
<span class="alternative-name">${escapeHtml(alt.food.name)}</span>
|
||||||
<span class="alternative-quantity">${alt.quantityGrams}g</span>
|
<span class="alternative-quantity">${alt.quantityGrams}g</span>
|
||||||
</div>
|
</div>
|
||||||
`).join('');
|
`).join('');
|
||||||
|
|
||||||
|
list.querySelectorAll<HTMLElement>('.alternative-item').forEach(item => {
|
||||||
|
item.addEventListener('click', () => {
|
||||||
|
const index = parseInt(item.dataset.index ?? '-1', 10);
|
||||||
|
if (index >= 0 && currentAlternatives[index]) {
|
||||||
|
selectAlternative(currentAlternatives[index].food, currentAlternatives[index].quantityGrams);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
item.addEventListener('keydown', (e: KeyboardEvent) => {
|
||||||
|
if (e.key === 'Enter' || e.key === ' ') {
|
||||||
|
e.preventDefault();
|
||||||
|
item.click();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1191,6 +1209,12 @@ function selectFood(food: FoodItem) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function selectAlternative(food: FoodItem, grams: number) {
|
||||||
|
selectFood(food);
|
||||||
|
getInputById('gramAmount').value = grams.toString();
|
||||||
|
previewCalories(food);
|
||||||
|
}
|
||||||
|
|
||||||
async function setFoodToEdit(foodId: string) {
|
async function setFoodToEdit(foodId: string) {
|
||||||
showLoading();
|
showLoading();
|
||||||
|
|
||||||
|
|||||||
@@ -1226,6 +1226,15 @@ label {
|
|||||||
align-items: center;
|
align-items: center;
|
||||||
padding: 8px 0;
|
padding: 8px 0;
|
||||||
border-bottom: 1px solid rgba(255, 255, 255, 0.08);
|
border-bottom: 1px solid rgba(255, 255, 255, 0.08);
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background-color 0.2s ease;
|
||||||
|
border-radius: 4px;
|
||||||
|
padding-left: 6px;
|
||||||
|
padding-right: 6px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.alternative-item:hover {
|
||||||
|
background-color: rgba(107, 141, 214, 0.15);
|
||||||
}
|
}
|
||||||
|
|
||||||
.alternative-item:last-child {
|
.alternative-item:last-child {
|
||||||
|
|||||||
Reference in New Issue
Block a user