|
| 1 | +const searchBtn = document.getElementById("searchBtn"); |
| 2 | +const ingredientInput = document.getElementById("ingredient"); |
| 3 | +const resultsDiv = document.getElementById("results"); |
| 4 | +const modal = document.getElementById("recipeModal"); |
| 5 | +const modalBody = document.getElementById("modalBody"); |
| 6 | +const closeModal = document.getElementById("closeModal"); |
| 7 | + |
| 8 | +function doSearch() { |
| 9 | + const input = ingredientInput.value.trim().toLowerCase(); |
| 10 | + const ingredients = input.split(/\s+/); |
| 11 | + resultsDiv.innerHTML = ""; |
| 12 | + |
| 13 | + if (!ingredients[0]) { |
| 14 | + resultsDiv.innerHTML = "<p>Please enter at least one ingredient.</p>"; |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + resultsDiv.innerHTML = "<p>Searching recipes...</p>"; |
| 19 | + |
| 20 | + Promise.all( |
| 21 | + ingredients.map((ing) => |
| 22 | + fetch(`https://www.themealdb.com/api/json/v1/1/filter.php?i=${ing}`) |
| 23 | + .then((res) => res.json()) |
| 24 | + .then((data) => data.meals || []) |
| 25 | + ) |
| 26 | + ) |
| 27 | + .then((results) => { |
| 28 | + if (results.some((arr) => arr.length === 0)) { |
| 29 | + resultsDiv.innerHTML = "<p>No recipes found with those ingredients.</p>"; |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + const commonMeals = results.reduce((acc, curr) => { |
| 34 | + const currIds = curr.map((m) => m.idMeal); |
| 35 | + return acc.filter((m) => currIds.includes(m.idMeal)); |
| 36 | + }, results[0]); |
| 37 | + |
| 38 | + if (!commonMeals.length) { |
| 39 | + resultsDiv.innerHTML = "<p>No recipes found with those ingredients.</p>"; |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + resultsDiv.innerHTML = "<p>Loading recipe details...</p>"; |
| 44 | + |
| 45 | + return loadMealDetailsWithVegFlag(commonMeals); |
| 46 | + }) |
| 47 | + .then((mealsWithVegInfo) => { |
| 48 | + if (!mealsWithVegInfo) return; |
| 49 | + |
| 50 | + resultsDiv.innerHTML = ""; |
| 51 | + mealsWithVegInfo.forEach(({ mealDetails, isVegetarian }) => { |
| 52 | + const card = document.createElement("div"); |
| 53 | + card.className = "recipe"; |
| 54 | + card.innerHTML = ` |
| 55 | + <h3>${mealDetails.strMeal}</h3> |
| 56 | + <img src="${mealDetails.strMealThumb}" alt="${mealDetails.strMeal}" /> |
| 57 | + ${isVegetarian ? '<span class="veg-badge">🌱 Vegetarian</span>' : ''} |
| 58 | + `; |
| 59 | + card.addEventListener("click", () => showRecipe(mealDetails.idMeal)); |
| 60 | + resultsDiv.appendChild(card); |
| 61 | + }); |
| 62 | + }) |
| 63 | + .catch((err) => { |
| 64 | + console.error(err); |
| 65 | + resultsDiv.innerHTML = "<p>Error loading recipe details.</p>"; |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +function loadMealDetailsWithVegFlag(meals) { |
| 70 | + const meatKeywords = [ |
| 71 | + "chicken", "beef", "egg", "eggs", "prawns", "pork", "fish", "shrimp", "meat", |
| 72 | + "bacon", "ham", "lamb", "turkey", "anchovy", "crab", "duck", "salmon", |
| 73 | + "sausage", "veal", "venison", "shellfish", "octopus", "squid" |
| 74 | + ]; |
| 75 | + |
| 76 | + return Promise.all( |
| 77 | + meals.map((meal) => |
| 78 | + fetch(`https://www.themealdb.com/api/json/v1/1/lookup.php?i=${meal.idMeal}`) |
| 79 | + .then((res) => res.json()) |
| 80 | + .then((data) => { |
| 81 | + const mealDetails = data.meals[0]; |
| 82 | + let isVegetarian = true; |
| 83 | + |
| 84 | + for (let i = 1; i <= 20; i++) { |
| 85 | + const ing = mealDetails[`strIngredient${i}`]; |
| 86 | + if (ing && meatKeywords.some((keyword) => ing.toLowerCase().includes(keyword))) { |
| 87 | + isVegetarian = false; |
| 88 | + break; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return { mealDetails, isVegetarian }; |
| 93 | + }) |
| 94 | + ) |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +function showRecipe(idMeal) { |
| 99 | + fetch(`https://www.themealdb.com/api/json/v1/1/lookup.php?i=${idMeal}`) |
| 100 | + .then((res) => res.json()) |
| 101 | + .then((data) => { |
| 102 | + const meal = data.meals[0]; |
| 103 | + let ingredients = "<ul>"; |
| 104 | + |
| 105 | + for (let i = 1; i <= 20; i++) { |
| 106 | + const ing = meal[`strIngredient${i}`]; |
| 107 | + const measure = meal[`strMeasure${i}`]; |
| 108 | + if (ing && ing.trim()) { |
| 109 | + ingredients += `<li>${measure} ${ing}</li>`; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + ingredients += "</ul>"; |
| 114 | + |
| 115 | + modalBody.innerHTML = ` |
| 116 | + <h2>${meal.strMeal}</h2> |
| 117 | + <img src="${meal.strMealThumb}" alt="${meal.strMeal}" style="max-width: 100%; margin: 1rem 0;" /> |
| 118 | + <h4>Ingredients:</h4> |
| 119 | + ${ingredients} |
| 120 | + <h4>Instructions:</h4> |
| 121 | + <p>${meal.strInstructions}</p> |
| 122 | + `; |
| 123 | + |
| 124 | + modal.style.display = "block"; |
| 125 | + ingredientInput.focus(); |
| 126 | + }) |
| 127 | + .catch((err) => { |
| 128 | + console.error(err); |
| 129 | + modalBody.innerHTML = "<p>Error loading recipe details.</p>"; |
| 130 | + modal.style.display = "block"; |
| 131 | + }); |
| 132 | +} |
| 133 | + |
| 134 | +searchBtn.addEventListener("click", doSearch); |
| 135 | + |
| 136 | +ingredientInput.addEventListener("keydown", (e) => { |
| 137 | + if (e.key === "Enter") { |
| 138 | + doSearch(); |
| 139 | + } |
| 140 | +}); |
| 141 | + |
| 142 | +closeModal.addEventListener("click", () => { |
| 143 | + modal.style.display = "none"; |
| 144 | +}); |
| 145 | + |
| 146 | +window.addEventListener("click", (e) => { |
| 147 | + if (e.target === modal) { |
| 148 | + modal.style.display = "none"; |
| 149 | + } |
| 150 | +}); |
| 151 | + |
| 152 | +window.addEventListener("keydown", (e) => { |
| 153 | + if (e.key === "Escape" && modal.style.display === "block") { |
| 154 | + modal.style.display = "none"; |
| 155 | + } |
| 156 | +}); |
0 commit comments