From 1c9381d70e67f4f9df214ec6dc43028fadd144fd Mon Sep 17 00:00:00 2001 From: JSriwongsa Date: Wed, 2 Aug 2023 21:41:58 -0500 Subject: [PATCH] updated js file --- recipe_website/main.js | 62 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/recipe_website/main.js b/recipe_website/main.js index 9efd15f..fb0a441 100644 --- a/recipe_website/main.js +++ b/recipe_website/main.js @@ -3,3 +3,65 @@ const showMenu = document.querySelector('.site'); menu.addEventListener('click', function(){ showMenu.classList.toggle('showmenu') }) +//recipe +const search_btn = document.getElementById('search-btn'); +const meal_list = document.getElementById('meal'); +const meal_detail = document.querySelector('.meal-content'); +const recipe_btn = document.getElementById('recipe-button'); + +search_btn.addEventListener('click', getMealList); +meal_list.addEventListener('click', getMealRecipe); +recipe_btn.addEventListener('click', () =>{ + meal_detail.parentElement.classList.remove('showRecipe'); +}); + +function getMealList(){ + let searchInputText = document.getElementById('search-input').value.trim(); + fetch(`https://www.themealdb.com/api/json/v1/1/search.php?s=${searchInputText}`).then(response => response.json()).then(data => { + let html = ""; + if(data.meals){ + data.meals.forEach(meal => { + html += `
+
+ +
+
+

${meal.strMeal}

+ Get Recipe +
+
`; + }); + meal_list.classList.remove('notFound') + } + else{ + html = "Sorry! we do not have this meal" + meal_list.classList.add('notFound'); + } + meal_list.innerHTML = html; + + }); +} +function getMealRecipe(x){ + x.preventDefault(); + if(x.target.classList.contains('recipe-btn')){ + let meal_item = x.target.parentElement.parentElement; + fetch(`https://www.themealdb.com/api/json/v1/1/lookup.php?i=${meal_item.dataset.id}`).then(respond => respond.json()).then(data => mealRecipeModel(data.meals)); + } +} +function mealRecipeModel(meal){ + meal = meal[0]; + let html = `

${meal.strMeal}

+

${meal.strCategory}

+
+

Directions:

+

${meal.strInstructions}

+
+
+ +
+ `; + meal_detail.innerHTML = html; + meal_detail.parentElement.classList.add('showRecipe'); +}