mirror of
https://gitlab.com/etc404/software-engineering-project.git
synced 2026-05-10 20:52:58 +00:00
Fixed a redirect
This commit is contained in:
@@ -1,19 +1,19 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="_csrf_header" th:content="${_csrf.headerName}"/>
|
||||
<meta name="_csrf" th:content="${_csrf.token}"/>
|
||||
<meta name="_csrf_header" th:content="${_csrf.headerName}"/>
|
||||
<meta name="_csrf" th:content="${_csrf.token}"/>
|
||||
<title>Create Thyme Crunch Recipe</title>
|
||||
<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">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<header class="top-header">
|
||||
<img th:src="@{images/header_left.png}" alt="Violin f-hole shape to the left of header." class="swirl">
|
||||
<img th:src="@{/images/header_left.png}" alt="Violin f-hole shape to the left of header." class="swirl">
|
||||
<h1 class="site-name">Thyme Crunch</h1>
|
||||
<img th:src="@{images/header_right.png}" alt="Violin f-hole shape to the right of header." class="swirl">
|
||||
<img th:src="@{/images/header_right.png}" alt="Violin f-hole shape to the right of header." class="swirl">
|
||||
</header>
|
||||
|
||||
<div class="form-wrap">
|
||||
@@ -34,17 +34,16 @@
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label>Ingredients <span class="required">*</span></label>
|
||||
<label>Ingredients <span class="required">*</span></label>
|
||||
<div id="ingredients-container"></div>
|
||||
<button class="btn-add" id="add-ingredient-btn">+ Add ingredient</button>
|
||||
<button class="btn-add" id="add-ingredient-btn" type="button">+ Add ingredient</button>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label>Instructions <span class="required">*</span></label>
|
||||
<div id="steps-container"></div>
|
||||
<button class="btn-add" id="add-step-btn">+ Add step</button>
|
||||
<button class="btn-add" id="add-step-btn" type="button">+ Add step</button>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<!-- Cover image / Right -->
|
||||
@@ -53,99 +52,111 @@
|
||||
<div class="image-drop" id="drop-zone">
|
||||
<p class="upload-title">Click to upload or drag and drop an image.</p>
|
||||
</div>
|
||||
<div class="image-preview" id="preview">
|
||||
<div class="image-preview" id="preview" style="display: none;">
|
||||
<img id="preview-img" src="" alt="Cover preview">
|
||||
<button class="remove-img" id="remove-img-btn">Remove</button>
|
||||
<button class="remove-img" id="remove-img-btn" type="button">Remove</button>
|
||||
</div>
|
||||
<input type="file" id="img-input" accept="image/*" style="display: none;">
|
||||
</div>
|
||||
|
||||
|
||||
<div class="field">
|
||||
<label for="prep">Preparation Time: <span class="required">*</span></label>
|
||||
<input type="text" id="prep" placeholder="0">
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
|
||||
<div class="field">
|
||||
<label for="cooking">Cooking Time: <span class="required">*</span></label>
|
||||
<input type="text" id="cooking" placeholder="0">
|
||||
</div>
|
||||
|
||||
|
||||
<div class="field">
|
||||
<label for="servings">Servings: <span class="required">*</span></label>
|
||||
<input type="text" id="servings" placeholder="0">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="cost">Estimated Cost: <span class="required">*</span></label>
|
||||
<input type="text" id="cost" placeholder="0">
|
||||
<label>Estimated Cost: <span class="required">*</span></label>
|
||||
<div class="cost-selector" id="cost-selector">
|
||||
<span class="dollar" data-value="1">$</span>
|
||||
<span class="dollar" data-value="2">$</span>
|
||||
<span class="dollar" data-value="3">$</span>
|
||||
<span class="dollar" data-value="4">$</span>
|
||||
</div>
|
||||
<input type="hidden" id="cost" value="0">
|
||||
|
||||
<div class="field">
|
||||
<label>Estimated Cost: <span class="required">*</span></label>
|
||||
<div class="cost-selector" id="cost-selector">
|
||||
<span class="dollar" data-value="1">$</span>
|
||||
<span class="dollar" data-value="2">$</span>
|
||||
<span class="dollar" data-value="3">$</span>
|
||||
<span class="dollar" data-value="4">$</span>
|
||||
</div>
|
||||
<input type="hidden" id="cost" value="0">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Tags -->
|
||||
<div class="form-section">
|
||||
|
||||
<div class="field">
|
||||
<label>Tags</label>
|
||||
<div class="tag-input-row">
|
||||
<input type="text" id="tag-input" placeholder="Type in a tag and click Add">
|
||||
<button class="btn" id="add-tag-btn">Add</button>
|
||||
<button class="btn" id="add-tag-btn" type="button">Add</button>
|
||||
</div>
|
||||
<div class="tag-wrap" id="tag-wrap"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="form-message" class="form-message" style="display:none;"></div>
|
||||
|
||||
<div class="actions">
|
||||
<button class="btn-create" id="publish-btn">CREATE</button>
|
||||
<button class="btn-create" id="publish-btn" type="button">CREATE</button>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// ---- DOM refs ----
|
||||
const ingredientContainer = document.getElementById('ingredients-container');
|
||||
const stepsContainer = document.getElementById('steps-container');
|
||||
const dropZone = document.getElementById('drop-zone');
|
||||
const imgInput = document.getElementById('img-input');
|
||||
const previewImg = document.getElementById('preview-img');
|
||||
const previewBox = document.getElementById('preview');
|
||||
const tags = [];
|
||||
|
||||
// ---- Ingredients ----
|
||||
const ingredientContainer = document.getElementById('ingredients-container');
|
||||
document.getElementById('add-ingredient-btn').addEventListener('click', addIngredient);
|
||||
addIngredient(); // start with one
|
||||
document.getElementById('add-ingredient-btn').addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
addIngredient();
|
||||
});
|
||||
addIngredient();
|
||||
|
||||
function addIngredient() {
|
||||
function addIngredient(data = {}) {
|
||||
const row = document.createElement('div');
|
||||
row.className = 'dynamic-row';
|
||||
row.innerHTML = `
|
||||
<input type="text" class="ing-name" placeholder="Ingredient (e.g. Sugar)">
|
||||
<input type="text" class="ing-qty" placeholder="Qty (e.g. 3)">
|
||||
<input type="text" class="ing-unit" placeholder="Unit (e.g. tbsp)">
|
||||
<input type="text" class="ing-notes" placeholder="Notes (optional)">
|
||||
<button class="btn-remove" title="Remove">✕</button>`;
|
||||
row.querySelector('.btn-remove').addEventListener('click', () => {
|
||||
<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-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>`;
|
||||
row.querySelector('.btn-remove').addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
row.remove();
|
||||
});
|
||||
ingredientContainer.appendChild(row);
|
||||
}
|
||||
|
||||
// ---- Steps ----
|
||||
const stepsContainer = document.getElementById('steps-container');
|
||||
document.getElementById('add-step-btn').addEventListener('click', addStep);
|
||||
addStep(); // start with one
|
||||
document.getElementById('add-step-btn').addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
addStep();
|
||||
});
|
||||
addStep();
|
||||
|
||||
function addStep() {
|
||||
function addStep(data = {}) {
|
||||
const row = document.createElement('div');
|
||||
row.className = 'dynamic-row';
|
||||
row.innerHTML = `
|
||||
<div class="step-bubble">?</div>
|
||||
<textarea placeholder="Describe this step..."></textarea>
|
||||
<button class="btn-remove" title="Remove">✕</button>
|
||||
<textarea placeholder="Describe this step...">${escapeHtml(data.instruction || '')}</textarea>
|
||||
<button class="btn-remove" title="Remove" type="button">✕</button>
|
||||
`;
|
||||
row.querySelector('.btn-remove').addEventListener('click', () => {
|
||||
row.querySelector('.btn-remove').addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
row.remove();
|
||||
renumberSteps();
|
||||
});
|
||||
@@ -156,10 +167,10 @@ function addStep() {
|
||||
function renumberSteps() {
|
||||
stepsContainer.querySelectorAll('.step-bubble').forEach((bubble, i) => {
|
||||
bubble.textContent = i + 1;
|
||||
bubble.textContent = i + 1;
|
||||
});
|
||||
}
|
||||
// Cost display
|
||||
|
||||
// ---- Cost display ----
|
||||
const dollars = document.querySelectorAll('.dollar');
|
||||
|
||||
dollars.forEach(dollar => {
|
||||
@@ -169,6 +180,7 @@ dollars.forEach(dollar => {
|
||||
dollars.forEach(d => {
|
||||
d.classList.toggle('active', parseInt(d.dataset.value) <= val);
|
||||
});
|
||||
clearError(document.getElementById('cost-selector'));
|
||||
});
|
||||
|
||||
dollar.addEventListener('mouseover', () => {
|
||||
@@ -186,53 +198,27 @@ dollars.forEach(dollar => {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// ---- Collecting on submit ----
|
||||
// Call this wherever you build your POST payload:
|
||||
function getIngredients() {
|
||||
return [...ingredientContainer.querySelectorAll('.dynamic-row')].map(row => {
|
||||
qtyValue = row.querySelector('.ing-qty').value.trim(); // quantity should be a number NOT A STRING
|
||||
return {
|
||||
ingredient: {
|
||||
name: row.querySelector('.ing-name').value.trim()
|
||||
},
|
||||
quantity: Number(qtyValue),
|
||||
unit: row.querySelector('.ing-unit').value.trim(),
|
||||
notes: row.querySelector('.ing-notes').value.trim()
|
||||
};
|
||||
}).filter(item => item.ingredient.name);
|
||||
}
|
||||
|
||||
|
||||
function getSteps() {
|
||||
return [...stepsContainer.querySelectorAll('textarea')]
|
||||
.map((el, i) => ({ step_number: i + 1, instruction: el.value.trim() }))
|
||||
.filter(item => item.instruction);
|
||||
}
|
||||
|
||||
// --- Image upload ---
|
||||
const dropZone = document.getElementById('drop-zone');
|
||||
const imgInput = document.getElementById('img-input');
|
||||
|
||||
// ---- Image upload preview ----
|
||||
dropZone.addEventListener('click', () => imgInput.click());
|
||||
|
||||
imgInput.addEventListener('change', function () {
|
||||
if (!this.files || !this.files[0]) return;
|
||||
|
||||
const url = URL.createObjectURL(this.files[0]);
|
||||
document.getElementById('preview-img').src = url;
|
||||
previewImg.src = url;
|
||||
dropZone.style.display = 'none';
|
||||
document.getElementById('preview').style.display = 'block';
|
||||
previewBox.style.display = 'block';
|
||||
});
|
||||
|
||||
document.getElementById('remove-img-btn').addEventListener('click', function () {
|
||||
document.getElementById('remove-img-btn').addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
imgInput.value = '';
|
||||
document.getElementById('preview').style.display = 'none';
|
||||
previewImg.src = '';
|
||||
previewBox.style.display = 'none';
|
||||
dropZone.style.display = 'block';
|
||||
});
|
||||
|
||||
// --- Tags ---
|
||||
const tags = [];
|
||||
|
||||
// ---- Tags ----
|
||||
document.getElementById('tag-input').addEventListener('keydown', function (e) {
|
||||
if (e.key === 'Enter') {
|
||||
e.preventDefault();
|
||||
@@ -240,12 +226,18 @@ document.getElementById('tag-input').addEventListener('keydown', function (e) {
|
||||
}
|
||||
});
|
||||
|
||||
document.getElementById('add-tag-btn').addEventListener('click', addTag);
|
||||
document.getElementById('add-tag-btn').addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
addTag();
|
||||
});
|
||||
|
||||
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; }
|
||||
if (!val || tags.includes(val)) {
|
||||
input.value = '';
|
||||
return;
|
||||
}
|
||||
tags.push(val);
|
||||
input.value = '';
|
||||
renderTags();
|
||||
@@ -258,7 +250,7 @@ function removeTag(t) {
|
||||
|
||||
function renderTags() {
|
||||
document.getElementById('tag-wrap').innerHTML = tags
|
||||
.map(t => `<div class="tag">${t} <span class="remove-tag" data-tag="${t}">✕</span></div>`)
|
||||
.map(t => `<div class="tag">${escapeHtml(t)} <span class="remove-tag" data-tag="${escapeHtml(t)}">✕</span></div>`)
|
||||
.join('');
|
||||
|
||||
document.querySelectorAll('.remove-tag').forEach(btn => {
|
||||
@@ -266,118 +258,217 @@ function renderTags() {
|
||||
});
|
||||
}
|
||||
|
||||
async function getLoggedInUser() {
|
||||
try {
|
||||
const res = await fetch('http://localhost:8080/api/users/me', {
|
||||
credentials: 'include'
|
||||
});
|
||||
if (!res.ok) throw new Error('Failed to get logged-in user');
|
||||
return await res.json();
|
||||
} catch (err) {
|
||||
console.error('Error fetching user:', err);
|
||||
return null;
|
||||
// ---- Helpers ----
|
||||
function escapeHtml(str) {
|
||||
return String(str ?? '')
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
|
||||
function showMessage(message, isError = false) {
|
||||
const box = document.getElementById('form-message');
|
||||
box.textContent = message;
|
||||
box.style.display = 'block';
|
||||
box.style.backgroundColor = isError ? '#ffd7d7' : '#dff5dd';
|
||||
box.style.color = isError ? '#7a0000' : '#1f5c2c';
|
||||
box.style.border = isError ? '1px solid #d88' : '1px solid #8bc48b';
|
||||
box.style.padding = '12px';
|
||||
box.style.marginTop = '20px';
|
||||
box.style.borderRadius = '8px';
|
||||
}
|
||||
|
||||
function showSuccessAndRedirect(message) {
|
||||
showMessage(message, false);
|
||||
setTimeout(() => {
|
||||
window.location.href = '/';
|
||||
}, 1500);
|
||||
}
|
||||
|
||||
function showError(input, message) {
|
||||
input.classList.add('invalid');
|
||||
|
||||
let error = input.parentElement.querySelector('.error-message');
|
||||
if (!error) {
|
||||
error = document.createElement('p');
|
||||
error.className = 'error-message';
|
||||
error.style.color = 'red';
|
||||
error.style.fontSize = '0.9em';
|
||||
error.style.marginTop = '6px';
|
||||
input.parentElement.appendChild(error);
|
||||
}
|
||||
|
||||
error.textContent = message;
|
||||
}
|
||||
|
||||
function clearError(input) {
|
||||
input.classList.remove('invalid');
|
||||
const error = input.parentElement.querySelector('.error-message');
|
||||
if (error) error.remove();
|
||||
}
|
||||
|
||||
function clearAllErrors() {
|
||||
document.querySelectorAll('.invalid').forEach(el => el.classList.remove('invalid'));
|
||||
document.querySelectorAll('.error-message').forEach(el => el.remove());
|
||||
const box = document.getElementById('form-message');
|
||||
box.style.display = 'none';
|
||||
box.textContent = '';
|
||||
}
|
||||
|
||||
function validateForm() {
|
||||
let valid = true;
|
||||
|
||||
const title = document.getElementById('title');
|
||||
const desc = document.getElementById('desc');
|
||||
const prep = document.getElementById('prep');
|
||||
const cooking = document.getElementById('cooking');
|
||||
const servings = document.getElementById('servings');
|
||||
const costSelector = document.getElementById('cost-selector');
|
||||
const costValue = Number(document.getElementById('cost').value);
|
||||
|
||||
[title, desc, prep, cooking, servings].forEach(clearError);
|
||||
clearError(costSelector);
|
||||
|
||||
if (!title.value.trim()) {
|
||||
showError(title, 'Title is required');
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (!desc.value.trim()) {
|
||||
showError(desc, 'Description is required');
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (!prep.value.trim() || isNaN(Number(prep.value)) || Number(prep.value) <= 0) {
|
||||
showError(prep, 'Enter a valid prep time');
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (!cooking.value.trim() || isNaN(Number(cooking.value)) || Number(cooking.value) <= 0) {
|
||||
showError(cooking, 'Enter a valid cook time');
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (!servings.value.trim() || isNaN(Number(servings.value)) || Number(servings.value) <= 0) {
|
||||
showError(servings, 'Enter valid servings');
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (!costValue || costValue <= 0) {
|
||||
showError(costSelector, 'Select an estimated cost');
|
||||
valid = false;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
filledIngredientRows.forEach(row => {
|
||||
const nameInput = row.querySelector('.ing-name');
|
||||
const qtyInput = row.querySelector('.ing-qty');
|
||||
clearError(nameInput);
|
||||
clearError(qtyInput);
|
||||
|
||||
if (!nameInput.value.trim()) {
|
||||
showError(nameInput, 'Ingredient name is required');
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (!qtyInput.value.trim() || isNaN(Number(qtyInput.value)) || Number(qtyInput.value) <= 0) {
|
||||
showError(qtyInput, 'Enter a valid quantity');
|
||||
valid = false;
|
||||
}
|
||||
});
|
||||
|
||||
const stepAreas = [...document.querySelectorAll('#steps-container textarea')];
|
||||
const filledSteps = stepAreas.filter(textarea => textarea.value.trim() !== '');
|
||||
if (filledSteps.length === 0 && stepAreas.length > 0) {
|
||||
showError(stepAreas[0], 'Add at least one instruction step');
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (!valid) {
|
||||
document.querySelector('.invalid')?.scrollIntoView({
|
||||
behavior: 'smooth',
|
||||
block: 'center'
|
||||
});
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
|
||||
function buildRecipeJSON(user) {
|
||||
const title = document.getElementById('title').value.trim();
|
||||
const description = document.getElementById('desc').value.trim();
|
||||
const prepTimeMinutes = Number(document.getElementById('prep').value);
|
||||
const cookTimeMinutes = Number(document.getElementById('cooking').value);
|
||||
const servings = Number(document.getElementById('servings').value);
|
||||
const status = "DRAFT";
|
||||
const cost = Number(document.getElementById('cost').value);
|
||||
|
||||
// Ingredients
|
||||
const recipeIngredients = [...document.querySelectorAll('#ingredients-container .dynamic-row')]
|
||||
.map(row => {
|
||||
const qtyValue = Number(row.querySelector('.ing-qty').value.trim());
|
||||
return {
|
||||
ingredient: { name: row.querySelector('.ing-name').value.trim() },
|
||||
quantity: qtyValue,
|
||||
unit: row.querySelector('.ing-unit').value.trim(),
|
||||
notes: row.querySelector('.ing-notes').value.trim()
|
||||
};
|
||||
})
|
||||
.filter(item => item.ingredient.name);
|
||||
|
||||
// Steps
|
||||
const steps = [...document.querySelectorAll('#steps-container textarea')]
|
||||
.map((el, i) => ({ stepNumber: i + 1, instruction: el.value.trim() }))
|
||||
.filter(item => item.instruction);
|
||||
|
||||
// Images
|
||||
|
||||
|
||||
// Tags
|
||||
const tagsInput = tags;
|
||||
const tagsArray = tagsInput.map(t => ({ name: t }));
|
||||
|
||||
return {
|
||||
title,
|
||||
description,
|
||||
prepTimeMinutes,
|
||||
cookTimeMinutes,
|
||||
servings,
|
||||
status,
|
||||
cost,
|
||||
user,
|
||||
recipeIngredients,
|
||||
steps,
|
||||
//images,
|
||||
tags: tagsArray
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// ---- Create recipe with multipart upload ----
|
||||
document.getElementById('publish-btn').addEventListener('click', async function(e) {
|
||||
e.preventDefault();
|
||||
e.preventDefault();
|
||||
clearAllErrors();
|
||||
|
||||
const user = await getLoggedInUser();
|
||||
if (!user) {
|
||||
alert("Unable to fetch logged-in user. Please refresh and try again.");
|
||||
return;
|
||||
if (!validateForm()) {
|
||||
showMessage('Please fix the highlighted fields and try again.', true);
|
||||
return;
|
||||
}
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('title', document.getElementById('title').value.trim());
|
||||
formData.append('description', document.getElementById('desc').value.trim());
|
||||
formData.append('prepTimeMinutes', document.getElementById('prep').value.trim());
|
||||
formData.append('cookTimeMinutes', document.getElementById('cooking').value.trim());
|
||||
formData.append('servings', document.getElementById('servings').value.trim());
|
||||
formData.append('cost', document.getElementById('cost').value.trim());
|
||||
|
||||
document.querySelectorAll('#ingredients-container .dynamic-row').forEach(row => {
|
||||
const ingredientName = row.querySelector('.ing-name').value.trim();
|
||||
if (!ingredientName) return;
|
||||
|
||||
formData.append('ingredientName', ingredientName);
|
||||
formData.append('ingredientQuantity', row.querySelector('.ing-qty').value.trim());
|
||||
formData.append('ingredientUnit', row.querySelector('.ing-unit').value.trim());
|
||||
formData.append('ingredientNotes', row.querySelector('.ing-notes').value.trim());
|
||||
});
|
||||
|
||||
document.querySelectorAll('#steps-container textarea').forEach(textarea => {
|
||||
const instruction = textarea.value.trim();
|
||||
if (instruction) {
|
||||
formData.append('stepInstruction', instruction);
|
||||
}
|
||||
});
|
||||
|
||||
console.log("Logged-in user:", user);
|
||||
const recipeJSON = buildRecipeJSON(user);
|
||||
console.log("Recipe JSON to submit:", JSON.stringify(recipeJSON, null, 2));
|
||||
tags.forEach(tag => formData.append('tags', tag));
|
||||
|
||||
try {
|
||||
|
||||
const csrfToken = document.querySelector('meta[name="_csrf"]').getAttribute('content');
|
||||
const csrfHeader = document.querySelector('meta[name="_csrf_header"]').getAttribute('content');
|
||||
|
||||
if (imgInput.files && imgInput.files[0]) {
|
||||
formData.append('image', imgInput.files[0]);
|
||||
}
|
||||
|
||||
try {
|
||||
const csrfToken = document.querySelector('meta[name="_csrf"]').getAttribute('content');
|
||||
const csrfHeader = document.querySelector('meta[name="_csrf_header"]').getAttribute('content');
|
||||
|
||||
const res = await fetch('http://localhost:8080/api/recipes', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
[csrfHeader]: csrfToken,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(recipeJSON),
|
||||
credentials: 'include'
|
||||
});
|
||||
const res = await fetch('/api/recipes/upload', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
[csrfHeader]: csrfToken
|
||||
},
|
||||
body: formData,
|
||||
credentials: 'include'
|
||||
});
|
||||
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
console.log("Recipe created:", data);
|
||||
alert("Recipe created successfully!");
|
||||
} else {
|
||||
const errorData = await res.json();
|
||||
console.error("Validation errors:", errorData);
|
||||
const responseText = await res.text();
|
||||
|
||||
|
||||
const firstError = Object.values(errorData)[0];
|
||||
alert(firstError);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Network error:", err);
|
||||
alert("Network error. Check console for details.");
|
||||
if (res.ok) {
|
||||
showSuccessAndRedirect('Recipe created successfully!');
|
||||
} else {
|
||||
console.error('Recipe creation failed:', responseText);
|
||||
showMessage(responseText || 'Failed to create recipe.', true);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Network error:', err);
|
||||
showMessage('Network error. Please try again.', true);
|
||||
}
|
||||
});
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
Reference in New Issue
Block a user