htmx.on('htmx:afterSwap', (event) => { if (event.target.id === 'instructions-container') { console.log('Instructions reloaded.'); } if (event.target.id === 'ingredients-container') { console.log('Ingredients reloaded.'); } }); let recipeId; function setRecipeId(id) { recipeId = id; } async function moveInstructionUp(instructionId, recipeIdParam) { const idToUse = recipeIdParam || recipeId; if (!idToUse) { alert('Recipe ID is not set. Please select a recipe first.'); return; } try { const response = await fetch(`/Recipe/${idToUse}/Instruction/${instructionId}/move-up`, { method: 'POST', headers: { 'RequestVerificationToken': document.querySelector('input[name="__RequestVerificationToken"]').value } }); if (response.ok) { htmx.ajax('GET', `/Recipe/${idToUse}/Instructions`, '#instructions-container'); } else { const error = await response.json(); alert(error.Error || 'Failed to move instruction up.'); } } catch (error) { console.error('Error moving instruction up:', error); } } async function moveInstructionDown(instructionId, recipeIdParam) { const idToUse = recipeIdParam || recipeId; if (!idToUse) { alert('Recipe ID is not set. Please select a recipe first.'); return; } try { const response = await fetch(`/Recipe/${idToUse}/Instruction/${instructionId}/move-down`, { method: 'POST', headers: { 'RequestVerificationToken': document.querySelector('input[name="__RequestVerificationToken"]').value } }); if (response.ok) { htmx.ajax('GET', `/Recipe/${idToUse}/Instructions`, '#instructions-container'); } else { const error = await response.json(); alert(error.Error || 'Failed to move instruction down.'); } } catch (error) { console.error('Error moving instruction down:', error); } } async function removeInstruction(instructionId, recipeIdParam) { const idToUse = recipeIdParam || recipeId; if (!idToUse) { alert('Recipe ID is not set. Please select a recipe first.'); return; } if (!confirm('Möchtest du diesen Schritt wirklich löschen?')) return; try { const response = await fetch(`/${idToUse}/RemoveInstruction/${instructionId}`, { method: 'POST', headers: { 'RequestVerificationToken': document.querySelector('input[name="__RequestVerificationToken"]').value } }); if (response.ok) { const element = document.getElementById(`instruction-${instructionId}`); if (element) { element.remove(); } } else { const error = await response.json(); alert(error.Error || 'Fehler beim Löschen der Anweisung.'); } } catch (error) { console.error('Fehler beim Löschen:', error); } } function addInstruction() { const container = document.getElementById('instructions-container'); const index = document.querySelectorAll('.instruction-item').length; const newInstructionHtml = `