|
| 1 | +const search = document.getElementById("search"); |
| 2 | +const submit = document.getElementById("submit"); |
| 3 | +const random = document.getElementById("random"); |
| 4 | +const mealsElement = document.getElementById("meals"); |
| 5 | +const resultHeading = document.getElementById("result-heading"); |
| 6 | +const singleMealElement = document.getElementById("single-meal"); |
| 7 | + |
| 8 | +function searchMeal(e) { |
| 9 | + e.preventDefault(); |
| 10 | + singleMealElement.innerHTML = ""; |
| 11 | + const term = search.value; |
| 12 | + if (term.trim()) { |
| 13 | + fetch(`https://www.themealdb.com/api/json/v1/1/search.php?s=${term}`) |
| 14 | + .then((res) => res.json()) |
| 15 | + .then((data) => { |
| 16 | + resultHeading.innerHTML = `<h2>Search results for '${term}':</h2>`; |
| 17 | + if (data.meals === null) { |
| 18 | + resultHeading.innerHTML = |
| 19 | + "<p>There are no search results. Try again!</p>"; |
| 20 | + } else { |
| 21 | + mealsElement.innerHTML = data.meals |
| 22 | + .map( |
| 23 | + (meal) => ` |
| 24 | + <div class="meal"> |
| 25 | + <img src="${meal.strMealThumb}" alt="${meal.strMeal}" /> |
| 26 | + <div class="meal-info" data-mealID="${meal.idMeal}"> |
| 27 | + <h3>${meal.strMeal}</h3> |
| 28 | + </div> |
| 29 | + </div> |
| 30 | + ` |
| 31 | + ) |
| 32 | + .join(""); |
| 33 | + } |
| 34 | + }); |
| 35 | + search.value = ""; |
| 36 | + } else { |
| 37 | + alert("Please enter a search term"); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +function getMealById(mealID) { |
| 42 | + fetch(`https://www.themealdb.com/api/json/v1/1/lookup.php?i=${mealID}`) |
| 43 | + .then((res) => res.json()) |
| 44 | + .then((data) => { |
| 45 | + const meal = data.meals[0]; |
| 46 | + addMealToDOM(meal); |
| 47 | + singleMealElement.scrollIntoView(); |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +function getRandomMeal() { |
| 52 | + mealsElement.innerHTML = ""; |
| 53 | + resultHeading.innerHTML = ""; |
| 54 | + fetch("https://www.themealdb.com/api/json/v1/1/random.php") |
| 55 | + .then((res) => res.json()) |
| 56 | + .then((data) => { |
| 57 | + const meal = data.meals[0]; |
| 58 | + addMealToDOM(meal); |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +function addMealToDOM(meal) { |
| 63 | + const ingredients = []; |
| 64 | + for (let i = 1; i <= 20; i++) { |
| 65 | + if (meal[`strIngredient${i}`]) { |
| 66 | + ingredients.push( |
| 67 | + `${meal[`strIngredient${i}`]} - ${meal[`strMeasure${i}`]}` |
| 68 | + ); |
| 69 | + } else { |
| 70 | + break; |
| 71 | + } |
| 72 | + } |
| 73 | + singleMealElement.innerHTML = ` |
| 74 | + <div class="single-meal"> |
| 75 | + <h1>${meal.strMeal}</h1> |
| 76 | + <img src="${meal.strMealThumb}" alt="${meal.strMeal}" /> |
| 77 | + <div class="main"> |
| 78 | + <h2>Ingredients</h2> |
| 79 | + <ul> |
| 80 | + ${ingredients.map((ingredient) => `<li>${ingredient}</li>`).join("")} |
| 81 | + </ul> |
| 82 | + <p>${meal.strInstructions}</p> |
| 83 | + </div> |
| 84 | + <div class="single-meal-info"> |
| 85 | + ${meal.strCategory ? `<p>${meal.strCategory}</p>` : ""} |
| 86 | + ${meal.strArea ? `<p>${meal.strArea}</p>` : ""} |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + `; |
| 90 | +} |
| 91 | + |
| 92 | +submit.addEventListener("submit", searchMeal); |
| 93 | +random.addEventListener("click", getRandomMeal); |
| 94 | +mealsElement.addEventListener("click", (e) => { |
| 95 | + const mealInfo = e.path.find((item) => { |
| 96 | + if (item.classList) { |
| 97 | + return item.classList.contains("meal-info"); |
| 98 | + } else { |
| 99 | + return false; |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + if (mealInfo) { |
| 104 | + const mealID = mealInfo.getAttribute("data-mealid"); |
| 105 | + getMealById(mealID); |
| 106 | + } |
| 107 | +}); |
| 108 | + |
| 109 | +// Init |
| 110 | +getRandomMeal(); |
0 commit comments