-
Notifications
You must be signed in to change notification settings - Fork 0
/
mealdbsync.js
78 lines (71 loc) · 2.44 KB
/
mealdbsync.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
73
74
75
76
77
78
const searchFood = async () => {
const searchField = document.getElementById('search-field');
const searchText = searchField.value;
// clear data
searchField.value = '';
if (searchText == '') {
// please write something to display
}
else {
// load data
const url = `https://www.themealdb.com/api/json/v1/1/search.php?s=${searchText}`;
try {
const res = await fetch(url);
const data = await res.json();
displaySearchResult(data.meals)
}
catch (error) {
console.log(error);
}
// fetch(url)
// .then(res => res.json())
// .then(data => displaySearchResult(data.meals));
}
}
const displaySearchResult = meals => {
const searchResult = document.getElementById('search-result');
searchResult.textContent = '';
if (meals.length == 0) {
// show no result found;
}
meals.forEach(meal => {
// console.log(meal);
const div = document.createElement('div');
div.classList.add('col');
div.innerHTML = `
<div onclick="loadMealDetail(${meal.idMeal})" class="card h-100">
<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);
})
}
const loadMealDetail = async mealId => {
const url = `https://www.themealdb.com/api/json/v1/1/lookup.php?i=${mealId}`;
const res = await fetch(url);
const data = await res.json();
displayMealDetail(data.meals[0]);
// fetch(url)
// .then(res => res.json())
// .then(data => displayMealDetail(data.meals[0]));
}
const displayMealDetail = meal => {
console.log(meal);
const mealDetails = document.getElementById('meal-details');
mealDetails.textContent = '';
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, 150)}</p>
<a href="${meal.strYoutube}" class="btn btn-primary">Go somewhere</a>
</div>
`;
mealDetails.appendChild(div);
}