mirror of
https://gitlab.com/etc404/software-engineering-project.git
synced 2026-05-10 20:52:58 +00:00
filtering for update
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
<link rel="stylesheet" th:href="@{/css/create-recipe.css}">
|
<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 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">
|
<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>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
@@ -115,6 +116,13 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
// ---- DOM refs ----
|
// ---- DOM refs ----
|
||||||
|
|
||||||
|
function isProfane(str) {
|
||||||
|
const lower = str.toLowerCase();
|
||||||
|
return bannedWords.some(word => lower.includes(word));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
const ingredientContainer = document.getElementById('ingredients-container');
|
const ingredientContainer = document.getElementById('ingredients-container');
|
||||||
const stepsContainer = document.getElementById('steps-container');
|
const stepsContainer = document.getElementById('steps-container');
|
||||||
const dropZone = document.getElementById('drop-zone');
|
const dropZone = document.getElementById('drop-zone');
|
||||||
@@ -243,7 +251,7 @@ document.getElementById('add-tag-btn').addEventListener('click', function (e) {
|
|||||||
function addTag() {
|
function addTag() {
|
||||||
const input = document.getElementById('tag-input');
|
const input = document.getElementById('tag-input');
|
||||||
const val = input.value.trim().toLowerCase().replace(/\s+/g, '-');
|
const val = input.value.trim().toLowerCase().replace(/\s+/g, '-');
|
||||||
if (!val || tags.includes(val)) {
|
if (!val || tags.includes(val) || isProfane(val)) {
|
||||||
input.value = '';
|
input.value = '';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -382,11 +390,22 @@ function validateForm() {
|
|||||||
valid = false;
|
valid = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isProfane(title.value)) {
|
||||||
|
showError(title, 'Title contains inappropriate language');
|
||||||
|
valid = false;
|
||||||
|
}
|
||||||
|
|
||||||
if (!desc.value.trim()) {
|
if (!desc.value.trim()) {
|
||||||
showError(desc, 'Description is required');
|
showError(desc, 'Description is required');
|
||||||
valid = false;
|
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) {
|
if (!prep.value.trim() || isNaN(Number(prep.value)) || Number(prep.value) <= 0) {
|
||||||
showError(prep, 'Enter a valid prep time');
|
showError(prep, 'Enter a valid prep time');
|
||||||
valid = false;
|
valid = false;
|
||||||
@@ -409,18 +428,86 @@ function validateForm() {
|
|||||||
|
|
||||||
const ingredientRows = [...document.querySelectorAll('#ingredients-container .dynamic-row')];
|
const ingredientRows = [...document.querySelectorAll('#ingredients-container .dynamic-row')];
|
||||||
const hasIngredient = ingredientRows.some(row => row.querySelector('.ing-name').value.trim() !== '');
|
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) {
|
if (!hasIngredient && ingredientRows.length > 0) {
|
||||||
showError(ingredientRows[0].querySelector('.ing-name'), 'Add at least one ingredient');
|
showError(ingredientRows[0].querySelector('.ing-name'), 'Add at least one ingredient');
|
||||||
valid = false;
|
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 stepAreas = [...document.querySelectorAll('#steps-container textarea')];
|
||||||
|
const filledSteps = stepAreas.filter(textarea => textarea.value.trim() !== '');
|
||||||
const hasStep = stepAreas.some(textarea => textarea.value.trim() !== '');
|
const hasStep = stepAreas.some(textarea => textarea.value.trim() !== '');
|
||||||
if (!hasStep && stepAreas.length > 0) {
|
if (!hasStep && stepAreas.length > 0) {
|
||||||
showError(stepAreas[0], 'Add at least one instruction step');
|
showError(stepAreas[0], 'Add at least one instruction step');
|
||||||
valid = false;
|
valid = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
filledSteps.forEach(textarea => {
|
||||||
|
if (isProfane(textarea.value)) {
|
||||||
|
showError(textarea, 'Step contains inappropriate language');
|
||||||
|
valid = false;
|
||||||
|
}});
|
||||||
|
|
||||||
if (!valid) {
|
if (!valid) {
|
||||||
document.querySelector('.invalid')?.scrollIntoView({
|
document.querySelector('.invalid')?.scrollIntoView({
|
||||||
behavior: 'smooth',
|
behavior: 'smooth',
|
||||||
|
|||||||
Reference in New Issue
Block a user