Update 2 files

- /src/main/java/com/example/demo/controller/RecipeUploadController.java
- /src/main/resources/templates/create-recipe.html
This commit is contained in:
Madeleine Stamp
2026-04-23 15:03:20 -06:00
parent 73718a5295
commit c16adcc361
2 changed files with 49 additions and 26 deletions
+27 -13
View File
@@ -129,7 +129,7 @@ function addIngredient(data = {}) {
row.className = 'dynamic-row';
row.innerHTML = `
<input type="text" class="ing-name" placeholder="Ingredient (e.g. Sugar)" value="${escapeHtml(data.ingredientName || '')}">
<input type="text" class="ing-qty" placeholder="Qty (e.g. 3)" value="${data.quantity ?? ''}">
<input type="text" class="ing-qty" placeholder="Qty (e.g. 2, 1/2, 1 1/2)" value="${escapeHtml(data.quantity ?? '')}">
<input type="text" class="ing-unit" placeholder="Unit (e.g. tbsp)" value="${escapeHtml(data.unit || '')}">
<input type="text" class="ing-notes" placeholder="Notes (optional)" value="${escapeHtml(data.notes || '')}">
<button class="btn-remove" title="Remove" type="button">✕</button>`;
@@ -283,7 +283,7 @@ function showMessage(message, isError = false) {
function showSuccessAndRedirect(message) {
showMessage(message, false);
setTimeout(() => {
window.location.href = '/';
window.location.href = '/explore';
}, 1500);
}
@@ -317,6 +317,29 @@ function clearAllErrors() {
box.textContent = '';
}
function isValidQuantity(val) {
const cleaned = String(val ?? '').replace(/\s+/g, ' ').trim();
if (cleaned === '') return true;
if (/^\d+(\.\d+)?$/.test(cleaned)) {
return true;
}
if (/^\d+\/\d+$/.test(cleaned)) {
const [numerator, denominator] = cleaned.split('/');
return Number(numerator) >= 0 && Number(denominator) > 0;
}
if (/^\d+\s\d+\/\d+$/.test(cleaned)) {
const [whole, fraction] = cleaned.split(' ');
const [numerator, denominator] = fraction.split('/');
return Number(whole) >= 0 && Number(numerator) >= 0 && Number(denominator) > 0;
}
return false;
}
function validateForm() {
let valid = true;
@@ -363,6 +386,7 @@ function validateForm() {
const ingredientRows = [...document.querySelectorAll('#ingredients-container .dynamic-row')];
const filledIngredientRows = ingredientRows.filter(row => row.querySelector('.ing-name').value.trim() !== '');
if (filledIngredientRows.length === 0 && ingredientRows.length > 0) {
showError(ingredientRows[0].querySelector('.ing-name'), 'Add at least one ingredient');
valid = false;
@@ -371,6 +395,7 @@ function validateForm() {
filledIngredientRows.forEach(row => {
const nameInput = row.querySelector('.ing-name');
const qtyInput = row.querySelector('.ing-qty');
clearError(nameInput);
clearError(qtyInput);
@@ -384,17 +409,6 @@ function validateForm() {
showError(qtyInput, 'Enter a valid quantity (e.g. 2, 1.5, 1/2, 1 1/2) or leave blank');
valid = false;
}
function isValidQuantity(val) {
if (val === '') return true;
if (!isNaN(Number(val))) return true;
if (/^\d+\/\d+$/.test(val)) return true;
if (/^\d+\s+\d+\/\d+$/.test(val)) return true;
return false;
}
});
const stepAreas = [...document.querySelectorAll('#steps-container textarea')];