-
Notifications
You must be signed in to change notification settings - Fork 0
/
mealdb.js
72 lines (69 loc) · 2.61 KB
/
mealdb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
71
72
const searchFood = () => {
const searchField = document.getElementById('search-field');
const searchText = searchField.value;
// clear the search field
searchField.value = '';
if (searchText == ''){
const searchResult = document.getElementById('search-result');
searchResult.innerHTML = `
<div class="alert alert-warning text-center mx-auto" role="alert">
<h5>Please Write Something!</h5>
</div>
`;
}else{
// create dynamic search url
const url = `https://www.themealdb.com/api/json/v1/1/search.php?s=${searchText}`;
fetch(url)
.then(res => res.json())
.then(data => displaySearchResult(data.meals));
}
};
const displaySearchResult = (meals) => {
const searchResult = document.getElementById('search-result');
// clear the search result
// searchResult.innerHTML = '';
searchResult.textContent = '';
if (meals !== null) {
console.log(meals.length)
meals.forEach(meal => {
const div = document.createElement('div');
div.classList.add('col');
div.innerHTML = `
<div onclick="loadMealDetail('${meal.idMeal}')" class="card">
<img src="${meal.strMealThumb}" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">${meal.strMeal}</h5>
<p class="card-text">${meal.strInstructions.slice(0, 200)}</p>
</div>
</div>
`;
searchResult.appendChild(div)
})
} else {
searchResult.innerHTML = `
<div class="alert alert-warning text-center mx-auto" role="alert">
<h5>No Result Found!</h5>
</div>
`;
}
};
const loadMealDetail = (mealId) => {
const url = `https://www.themealdb.com/api/json/v1/1/lookup.php?i=${mealId}`;
fetch(url).then((response) => response.json()).then((data => {
displayMealDetail(data.meals[0])
}))
};
const displayMealDetail = meal => {
const mealDetail = document.getElementById('meal-detail');
const div = document.createElement('div');
div.classList.add('card');
div.innerHTML = `
<img src="${meal.strMealThumb}" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">${meal.strMeal}</h5>
<p class="card-text">${meal.strInstructions.slice(0, 100)}</p>
<a href="${meal.strYoutube}" class="btn btn-primary">Go somewhere</a>
</div>
`;
mealDetail.appendChild(div);
}