filtering for update

This commit is contained in:
durn
2026-04-23 18:29:39 -06:00
parent 6c8c86d1e7
commit 317d45bad3
+97 -10
View File
@@ -8,6 +8,7 @@
<link rel="stylesheet" th:href="@{/css/create-recipe.css}">
<link href="https://fonts.googleapis.com/css2?family=Delius+Swash+Caps&family=Mali:ital,wght@0,200;0,300;0,400;0,500;0,600;0,700;1,200;1,300;1,400;1,500;1,600;1,700" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css">
<script th:src="@{/js/bad-words-list.js}"></script>
</head>
<body>
@@ -115,6 +116,13 @@
<script>
// ---- DOM refs ----
function isProfane(str) {
const lower = str.toLowerCase();
return bannedWords.some(word => lower.includes(word));
}
const ingredientContainer = document.getElementById('ingredients-container');
const stepsContainer = document.getElementById('steps-container');
const dropZone = document.getElementById('drop-zone');
@@ -241,16 +249,16 @@ document.getElementById('add-tag-btn').addEventListener('click', function (e) {
});
function addTag() {
const input = document.getElementById('tag-input');
const val = input.value.trim().toLowerCase().replace(/\s+/g, '-');
if (!val || tags.includes(val)) {
input.value = '';
return;
}
tags.push(val);
input.value = '';
renderTags();
}
const input = document.getElementById('tag-input');
const val = input.value.trim().toLowerCase().replace(/\s+/g, '-');
if (!val || tags.includes(val) || isProfane(val)) {
input.value = '';
return;
}
tags.push(val);
input.value = '';
renderTags();
}
function removeTag(t) {
tags.splice(tags.indexOf(t), 1);
@@ -381,11 +389,22 @@ function validateForm() {
showError(title, 'Title is required');
valid = false;
}
if (isProfane(title.value)) {
showError(title, 'Title contains inappropriate language');
valid = false;
}
if (!desc.value.trim()) {
showError(desc, 'Description is required');
valid = false;
}
if (isProfane(desc.value)) {
showError(desc, 'Description contains inappropriate language');
valid = false;
}
if (!prep.value.trim() || isNaN(Number(prep.value)) || Number(prep.value) <= 0) {
showError(prep, 'Enter a valid prep time');
@@ -409,17 +428,85 @@ function validateForm() {
const ingredientRows = [...document.querySelectorAll('#ingredients-container .dynamic-row')];
const hasIngredient = ingredientRows.some(row => row.querySelector('.ing-name').value.trim() !== '');
const filledIngredientRows = ingredientRows.filter(row => row.querySelector('.ing-name').value.trim() !== '');
if (!hasIngredient && ingredientRows.length > 0) {
showError(ingredientRows[0].querySelector('.ing-name'), 'Add at least one ingredient');
valid = false;
}
filledIngredientRows.forEach(row => {
const nameInput = row.querySelector('.ing-name');
const qtyInput = row.querySelector('.ing-qty');
const unitInput = row.querySelector('.ing-unit');
const noteInput = row.querySelector('.ing-notes');
clearError(nameInput);
clearError(qtyInput);
if (!nameInput.value.trim()) {
showError(nameInput, 'Ingredient name is required');
valid = false;
}
if (isProfane(nameInput.value.trim())) {
showError(nameInput, 'Ingredient name contains inappropriate language');
valid = false;
}
if (isProfane(unitInput.value.trim())) {
showError(noteInput, 'Ingredient unit contains inappropriate language');
valid = false;
}
if (isProfane(noteInput.value.trim())) {
showError(noteInput, 'Ingredient note contains inappropriate language');
valid = false;
}
const qtyVal = qtyInput.value.trim();
if (!isValidQuantity(qtyVal)) {
showError(qtyInput, 'Enter a valid quantity (e.g. 2, 1.5, 1/2, 1 1/2) or leave blank');
valid = false;
}
});
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;
}
const stepAreas = [...document.querySelectorAll('#steps-container textarea')];
const filledSteps = stepAreas.filter(textarea => textarea.value.trim() !== '');
const hasStep = stepAreas.some(textarea => textarea.value.trim() !== '');
if (!hasStep && stepAreas.length > 0) {
showError(stepAreas[0], 'Add at least one instruction step');
valid = false;
}
filledSteps.forEach(textarea => {
if (isProfane(textarea.value)) {
showError(textarea, 'Step contains inappropriate language');
valid = false;
}});
if (!valid) {
document.querySelector('.invalid')?.scrollIntoView({