From 0fb887756f8d56fadabc4d3638fb90c868aaa546 Mon Sep 17 00:00:00 2001 From: morgane Date: Thu, 18 Jun 2026 12:55:21 +0000 Subject: [PATCH] Actualiser assets/js/app.js --- assets/js/app.js | 188 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) diff --git a/assets/js/app.js b/assets/js/app.js index f862a0e..72517d4 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -14989,6 +14989,194 @@ async function loadRecipeArchive() { container.innerHTML = html; } +// ===== RECIPE LIBRARY (recettes manuelles, ex: cocktails) ===== + +let _recipeLibraryCache = []; + +async function loadRecipeLibrary() { + const container = document.getElementById('recipe-library-list'); + if (!container) return; + container.innerHTML = `

Chargement…

`; + try { + const result = await api('recipe_library_list', {}, 'GET'); + if (!result.success) { + container.innerHTML = `

Erreur de chargement.

`; + return; + } + _recipeLibraryCache = result.recipes; + renderRecipeLibraryList(result.recipes); + } catch (e) { + container.innerHTML = `

Erreur de chargement.

`; + } +} + +function renderRecipeLibraryList(recipes) { + const container = document.getElementById('recipe-library-list'); + if (!container) return; + if (!recipes || recipes.length === 0) { + container.innerHTML = `

Aucune recette pour l'instant.

`; + return; + } + container.innerHTML = recipes.map(entry => { + const r = entry.recipe; + const favBadge = entry.is_favorite ? `` : ''; + const ingCount = (r.ingredients || []).length; + return ` +
+
+ 🍹 + ${escapeHtml(r.title)} + ${favBadge} +
+
+ ${ingCount} ingrédient${ingCount > 1 ? 's' : ''} +
+
+ `; + }).join(''); +} + +function openRecipeLibraryForm(id = null) { + const existing = id ? _recipeLibraryCache.find(e => e.id === id) : null; + const r = existing ? existing.recipe : { title: '', ingredients: [{ name: '', qty: '' }], steps: [''] }; + + document.getElementById('modal-content').innerHTML = ` + +
+
+ + +
+
+ +
+ ${r.ingredients.map(ing => ` +
+ + + +
+ `).join('')} +
+ +
+
+ +
+ ${r.steps.map(step => ` +
+ + +
+ `).join('')} +
+ +
+ +
+ `; + document.getElementById('modal-overlay').style.display = 'flex'; +} + +function addRecipeLibraryRow(containerId, type) { + const container = document.getElementById(containerId); + if (!container) return; + const row = document.createElement('div'); + row.className = 'barcode-input-row'; + row.style.marginBottom = '6px'; + if (type === 'ingredient') { + row.innerHTML = ` + + + + `; + } else { + row.innerHTML = ` + + + `; + } + container.appendChild(row); +} + +async function submitRecipeLibraryForm(e, id) { + e.preventDefault(); + const title = document.getElementById('rl-title').value.trim(); + if (!title) { showToast('Le titre est obligatoire', 'warning'); return; } + + const ingredients = Array.from(document.querySelectorAll('#rl-ingredients-list .barcode-input-row')).map(row => ({ + name: row.querySelector('.rl-ing-name')?.value.trim() || '', + qty: row.querySelector('.rl-ing-qty')?.value.trim() || '', + })).filter(ing => ing.name); + + const steps = Array.from(document.querySelectorAll('#rl-steps-list .rl-step-text')).map(input => input.value.trim()).filter(Boolean); + + const recipe = { title, ingredients, steps, persons: 1 }; + + showLoading(true); + try { + const result = await api('recipe_library_save', {}, 'POST', { id, recipe }); + showLoading(false); + if (result.success) { + showToast('Recette enregistrée', 'success'); + closeModal(); + loadRecipeLibrary(); + } else { + showToast(result.error || 'Erreur', 'error'); + } + } catch (e) { + showLoading(false); + showToast('Erreur', 'error'); + } +} + +function viewRecipeLibraryItem(id) { + const entry = _recipeLibraryCache.find(e => e.id === id); + if (!entry) return; + const r = entry.recipe; + document.getElementById('modal-content').innerHTML = ` + +
+ + +
+
+ +
    + ${(r.steps || []).map(s => `
  1. ${escapeHtml(s)}
  2. `).join('')} +
+
+
+ + + +
+ `; + document.getElementById('modal-overlay').style.display = 'flex'; +} + +async function toggleRecipeLibraryFavorite(id) { + await api('recipe_library_toggle_favorite', {}, 'POST', { id }); + closeModal(); + loadRecipeLibrary(); +} + +async function deleteRecipeLibraryItem(id) { + if (!confirm('Supprimer cette recette ?')) return; + await api('recipe_library_delete', {}, 'POST', { id }); + closeModal(); + showToast('Recette supprimée', 'success'); + loadRecipeLibrary(); +} + function viewArchivedRecipe(idx) { const pick = _recipeArchiveEntries[idx]; if (!pick) return;