import "./styles.css"; import data from "./data.json"; let spiritsData = []; let recipesData = []; async function loadData() { try { spiritsData = data.spirits.sort((a, b) => a.name > b.name); recipesData = data.recipes.sort((a, b) => a.name > b.name); renderSpirits(); enableButtonsLogic(); } catch (error) { console.error("Error cargando datos:", error); document.getElementById("spiritsGrid").innerHTML = '

Error al cargar los datos. Asegúrate de que data.json está disponible.

'; } } function renderSpirits() { const grid = document.getElementById("spiritsGrid"); grid.innerHTML = spiritsData .map( (spirit, index) => `
${spirit.name}
${spirit.brand}
${spirit.type}
`, ) .join(""); } function toggleSpirit(index, forcedCheckValue) { const card = document.getElementById(`spirit-${index}`); const checkbox = document.getElementById(`check-${index}`); checkbox.checked = forcedCheckValue !== undefined ? forcedCheckValue : !checkbox.checked; card.classList.toggle("selected"); // Update buttons status only on natural toggle if (forcedCheckValue === undefined) { updateMixButtonStatus(); updateMassSelectButtonsStatus(); } } function enableButtonsLogic() { const grid = document.getElementById("spiritsGrid"); // Add toggle logic to cards spiritsData.map((spirit, index) => { grid.querySelector(`#spirit-${index}`).addEventListener("click", (e) => { toggleSpirit(index); }); }); // Add recipes crossing to mix button const section = document.getElementsByClassName("section"); if (section && section.length > 0) { section[0].querySelector(".mix-button").addEventListener("click", (e) => { findRecipes(); }); section[0].querySelector(".mix-button").disabled = true; } // Add logic to select all / none buttons if (section && section.length > 0) { section[0] .querySelector("#select-all-button") .addEventListener("click", (e) => { selectAll(); }); section[0] .querySelector("#select-none-button") .addEventListener("click", (e) => { selectNone(); }); updateMassSelectButtonsStatus(); } } function updateMixButtonStatus(forcedDisabledValue) { const section = document.getElementsByClassName("section"); const selected = getSelectedSpiritsTypes(); if (section && section.length > 0) { section[0].querySelector(".mix-button").disabled = forcedDisabledValue ?? selected.length === 0; } } function updateMassSelectButtonsStatus() { const section = document.getElementsByClassName("selection-options"); const selected = getSelectedSpiritsNames(); if (section && section.length > 0) { section[0].querySelector("#select-all-button").disabled = selected.length === spiritsData.length; section[0].querySelector("#select-none-button").disabled = selected.length === 0; } } function getSelectedSpiritsTypes() { const selectedSpirits = []; spiritsData.forEach((spirit, index) => { const checkbox = document.getElementById(`check-${index}`); if (checkbox.checked) { selectedSpirits.push(spirit.type); } }); return selectedSpirits; } function getSelectedSpiritsNames() { const selectedSpirits = []; spiritsData.forEach((spirit, index) => { const checkbox = document.getElementById(`check-${index}`); if (checkbox.checked) { selectedSpirits.push(spirit.name); } }); return selectedSpirits; } function selectAll() { const selectAllButton = document.getElementById("select-all-button"); if (selectAllButton) { const selectedNames = getSelectedSpiritsNames(); spiritsData.forEach((spirit, index) => { if (!selectedNames.includes(spirit.name)) { toggleSpirit(index, true); } }); } updateMixButtonStatus(false); updateMassSelectButtonsStatus(); return; } function selectNone() { const selectNoneButton = document.getElementById("select-none-button"); if (selectNoneButton) { const selectedNames = getSelectedSpiritsNames(); spiritsData.forEach((spirit, index) => { if (selectedNames.includes(spirit.name)) { toggleSpirit(index, false); } }); } updateMixButtonStatus(true); updateMassSelectButtonsStatus(); return; } function findRecipes() { const selectedSpirits = getSelectedSpiritsTypes(); if (selectedSpirits.length === 0) { document.getElementById("recipesContainer").innerHTML = '

Por favor, selecciona al menos un licor para encontrar recetas.

'; return; } const availableRecipes = recipesData.filter((recipe) => { const alcoholIngredients = recipe.ingredients.filter( (ing) => ing.isAlcohol, ); return alcoholIngredients.every((ingredient) => selectedSpirits.includes(ingredient.type), ); }); renderRecipes(availableRecipes); document.getElementById("recipesContainer").scrollIntoView({ behavior: "smooth", block: "start", }); } function renderRecipes(recipes) { const container = document.getElementById("recipesContainer"); if (recipes.length === 0) { container.innerHTML = '

No se encontraron recetas con los licores seleccionados. ¡Intenta agregar más licores!

'; return; } container.innerHTML = recipes .map((recipe) => { const alcoholIngredients = recipe.ingredients.filter( (ing) => ing.isAlcohol, ); const nonAlcoholIngredients = recipe.ingredients.filter( (ing) => !ing.isAlcohol, ); return `
${recipe.name}
Licores:
${alcoholIngredients .map( (ing) => `
${ing.amount} ${ing.name}
`, ) .join("")} ${ nonAlcoholIngredients.length > 0 ? `
Otros ingredientes:
${nonAlcoholIngredients .map( (ing) => `
${ing.amount ? `${ing.amount}` : ""} ${ing.name}
`, ) .join("")}
` : "" }
${ recipe.instructions ? `
Preparación: ${recipe.instructions}
` : "" }
`; }) .join(""); } // Inicializar al cargar la página document.addEventListener("DOMContentLoaded", loadData);