├── assets ├── app.js └── style.css └── index.html /assets/app.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const data_container = document.getElementById('foods'); 3 | const searchBtn = document.getElementById('searchBtn'); 4 | const warning = document.getElementById('warning'); 5 | 6 | // Search Btn Click Function 7 | searchBtn.addEventListener('click', function () { 8 | const keyword = document.getElementById('keyword').value; 9 | data_container.innerHTML = ''; 10 | if (keyword === '') { 11 | warning.style.display = 'block'; 12 | } else { 13 | getFood(keyword); 14 | warning.style.display = 'none'; 15 | } 16 | }); 17 | // Details for Foods 18 | const displayDetails = name => { 19 | const url = `https://www.themealdb.com/api/json/v1/1/lookup.php?i=${name}`; 20 | fetch(url) 21 | .then(res => res.json()) 22 | .then(data => { 23 | renderFoodInfo(data.meals[0]); 24 | console.log(data.meals[0]); 25 | }); 26 | }; 27 | // Render Single Food Info 28 | const renderFoodInfo = (food) => { 29 | // Get all ingredients from the object. Up to 20 30 | const ingredients = []; 31 | for (let i = 1; i <= 20; i++) { 32 | if (food[`strIngredient${i}`]) { 33 | ingredients.push(`${food[`strIngredient${i}`]} - ${food[`strMeasure${i}`]}`); 34 | } else { 35 | // Stop if there are no more ingredients 36 | break; 37 | } 38 | } 39 | const foodDetailsDiv = document.getElementById('foodsDetails'); 40 | foodDetailsDiv.innerHTML = ` 41 | 42 |

${food.strMeal}

43 | 44 |
Ingredients
45 | 48 | `; 49 | }; 50 | // Foods Loop 51 | function getFood(mealId) { 52 | //const mainApi = `https://www.themealdb.com/api/json/v1/1/search.php?s=${keyword.value}`; 53 | const mainApi = `https://www.themealdb.com/api/json/v1/1/search.php?s=${mealId}`; 54 | 55 | fetch(mainApi) 56 | .then(res => res.json()) 57 | .then(data => { 58 | displayFoods(data.meals); 59 | }); 60 | 61 | const displayFoods = foods => { 62 | const foodsDiv = document.getElementById('foods'); 63 | if (foods != null) { 64 | foods.map(food => { 65 | const foodDiv = document.createElement('div'); 66 | foodDiv.className = 'col-md-3'; 67 | const foodInfo = ` 68 |
69 | 70 |

${food.strMeal}

71 |
72 | `; 73 | foodDiv.innerHTML = foodInfo; 74 | foodsDiv.appendChild(foodDiv); 75 | }); 76 | } else { 77 | warning.style.display = 'block'; 78 | } 79 | }; 80 | } 81 | -------------------------------------------------------------------------------- /assets/style.css: -------------------------------------------------------------------------------- 1 | * { 2 | margin: 0; 3 | padding: 0; 4 | box-sizing: border-box; 5 | } 6 | 7 | a { 8 | text-decoration: none; 9 | transition: 0.2s; 10 | color: #e74c3c; 11 | } 12 | 13 | body { 14 | font-family: 'Montserrat', sans-serif; 15 | } 16 | 17 | /* ******************* 18 | * Search Bar Style 19 | **********************/ 20 | 21 | .inner-form { 22 | display: -ms-flexbox; 23 | display: flex; 24 | width: 100%; 25 | -ms-flex-pack: justify; 26 | justify-content: space-between; 27 | -ms-flex-align: center; 28 | align-items: center; 29 | /* box-shadow: 0px 8px 20px 0px rgba(0, 0, 0, 0.15); */ 30 | border-radius: 5px; 31 | overflow: hidden; 32 | margin-bottom: 30px; 33 | } 34 | 35 | .inner-form .input-field { 36 | height: 48px; 37 | } 38 | 39 | .inner-form .input-field input { 40 | height: 100%; 41 | background: transparent; 42 | border: 0; 43 | display: block; 44 | width: 100%; 45 | padding: 10px 0; 46 | font-size: 16px; 47 | color: #000; 48 | } 49 | 50 | .inner-form .input-field input.placeholder { 51 | color: #222; 52 | font-size: 16px; 53 | } 54 | 55 | .inner-form .input-field input:-moz-placeholder { 56 | color: #222; 57 | font-size: 16px; 58 | } 59 | 60 | .inner-form .input-field input::-webkit-input-placeholder { 61 | color: #222; 62 | font-size: 16px; 63 | } 64 | 65 | .inner-form .input-field input:hover, 66 | .inner-form .input-field input:focus { 67 | box-shadow: none; 68 | outline: 0; 69 | } 70 | 71 | .inner-form .input-field.first-wrap { 72 | -ms-flex-positive: 1; 73 | flex-grow: 1; 74 | display: -ms-flexbox; 75 | display: flex; 76 | -ms-flex-align: center; 77 | align-items: center; 78 | background: #f1f1f1; 79 | } 80 | 81 | .inner-form .input-field.first-wrap input { 82 | -ms-flex-positive: 1; 83 | flex-grow: 1; 84 | } 85 | 86 | .inner-form .input-field.first-wrap .svg-wrapper { 87 | min-width: 80px; 88 | display: -ms-flexbox; 89 | display: flex; 90 | -ms-flex-pack: center; 91 | justify-content: center; 92 | -ms-flex-align: center; 93 | align-items: center; 94 | } 95 | 96 | .inner-form .input-field.first-wrap svg { 97 | width: 25px; 98 | height: 25px; 99 | fill: #777; 100 | } 101 | 102 | .inner-form .input-field.second-wrap { 103 | min-width: 130px; 104 | } 105 | 106 | @media screen and (max-width: 767px) { 107 | .inner-form .input-field.second-wrap { 108 | min-width: 100px; 109 | } 110 | } 111 | .inner-form .input-field.second-wrap .btn-search { 112 | height: 100%; 113 | width: 100%; 114 | white-space: nowrap; 115 | font-size: 16px; 116 | color: #fff; 117 | border: 0; 118 | cursor: pointer; 119 | position: relative; 120 | z-index: 0; 121 | background: #e74c3c; 122 | transition: all 0.2s ease-out, color 0.2s ease-out; 123 | font-weight: 300; 124 | } 125 | 126 | .inner-form .input-field.second-wrap .btn-search:hover { 127 | background: #e74c3c; 128 | } 129 | 130 | .inner-form .input-field.second-wrap .btn-search:focus { 131 | outline: 0; 132 | box-shadow: none; 133 | } 134 | 135 | ul li { 136 | padding: 5px 0; 137 | } 138 | 139 | .icon-check { 140 | color: #e74c3c; 141 | margin-right: 5px; 142 | } 143 | 144 | footer { 145 | padding: 50px 30px; 146 | text-align: center; 147 | } 148 | 149 | #warning { 150 | display: none; 151 | } 152 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Hungry Monster API Practice Project by Monir H 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 41 | 42 | 43 | 44 |
45 |
46 |
47 | 48 |
49 |
50 |
51 | 52 | 53 | 54 |
55 | 56 |
57 |
58 | 59 |
60 |
61 | 62 |
63 |
64 |
65 | 66 | 67 | 68 |
69 |
70 |

Please enter valid food 🍔 name.

71 |
72 | 73 | 74 | 75 | 78 | 79 | 80 | 81 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | --------------------------------------------------------------------------------