Files
foodplanner/templates/modals/add_single_food.html

80 lines
4.0 KiB
HTML

<!-- Add Single Food Modal -->
<div class="modal fade" id="addSingleFoodModal" tabindex="-1" aria-labelledby="addSingleFoodModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addSingleFoodModalLabel">Add Single Food</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form id="addSingleFoodForm">
<input type="hidden" name="person" value="{{ person }}">
<input type="hidden" name="date" value="{{ current_date.isoformat() }}">
<div class="mb-3">
<label class="form-label">Select Food</label>
<input type="text" class="form-control mb-2" id="foodSearchInput" placeholder="Search foods by name or brand..." data-testid="food-search-input">
<select class="form-control" name="food_id" id="foodSelect" required onchange="updateServingSizeNote(this)" size="10">
<option value="">Choose food...</option>
{% for food in foods %}
<option value="{{ food.id }}" data-serving-size="{{ food.serving_size }}" data-brand="{{ food.brand }}" data-testid="food-option">{{ food.name }}</option>
{% endfor %}
</select>
</div>
<div class="mb-3">
<label class="form-label">Quantity (g)</label>
<span id="servingSizeNote" class="text-muted small ms-2"></span>
<input type="number" step="0.1" class="form-control" name="quantity" value="1.0" min="0.1" required>
</div>
<input type="hidden" name="meal_time" id="addSingleFoodMealTime">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" onclick="submitAddSingleFood()">Add Food</button>
</div>
<script>
function updateServingSizeNote(selectElement) {
const selectedOption = selectElement.options[selectElement.selectedIndex];
const servingSize = selectedOption.getAttribute('data-serving-size');
const servingSizeNote = document.getElementById('servingSizeNote');
if (servingSize) {
servingSizeNote.textContent = `(Serving size: ${servingSize}g)`;
} else {
servingSizeNote.textContent = '';
}
}
document.getElementById('foodSearchInput').addEventListener('input', function() {
const searchText = this.value.toLowerCase();
const select = document.getElementById('foodSelect');
const options = select.options;
for (let i = 0; i < options.length; i++) {
const option = options[i];
if (option.value === "") continue; // Skip "Choose food..."
const text = option.text.toLowerCase();
const brand = (option.getAttribute('data-brand') || "").toLowerCase();
if (text.includes(searchText) || brand.includes(searchText)) {
option.style.display = "";
} else {
option.style.display = "none";
}
}
});
// Reset search when modal is shown
document.getElementById('addSingleFoodModal').addEventListener('show.bs.modal', function () {
document.getElementById('foodSearchInput').value = '';
const options = document.getElementById('foodSelect').options;
for (let i = 0; i < options.length; i++) {
options[i].style.display = "";
}
document.getElementById('servingSizeNote').textContent = '';
});
</script>
</div>
</div>
</div>