-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyle.js
104 lines (100 loc) · 3.36 KB
/
style.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const result = document.getElementById('mealDetails');
// search button
const searchButton = document.getElementById('searchBtn');
searchButton.addEventListener('click', (event) => {
event.preventDefault();
const inputField = document.getElementById('inputField').value;
inputValue(inputField);
});
// input field code here
function inputValue(inputField) {
result.style.display = "none";
const meal = document.getElementById('meal');
let url = "";
if (inputField.length === 1) {
url = `https://www.themealdb.com/api/json/v1/1/search.php?f=${inputField}`;
meal.innerHTML = null;
meal.innerHTML = null;
} else {
url = `https://www.themealdb.com/api/json/v1/1/search.php?s=${inputField}`;
meal.innerHTML = null;
meal.innerHTML = null;
}
fetch(url)
.then(res => res.json())
.then(data => {
displayData(data)
})
}
// all meal box here
const displayData = data => {
let text = "";
if (data.meals) {
data.meals.forEach(item => {
const div = document.createElement('div');
const allMealInfo = text + `
<button onclick="showMealDetails('${item.strMeal}')" class="meal-box">
<div>
<div>
<img class="img-fluid rounded" src="${item.strMealThumb}"/>
<div class="text">
<h5>${item.strMeal}</h5>
</div>
</div>
</div>
</button>`;
div.innerHTML = allMealInfo;
meal.appendChild(div);
});
}
else {
result.style.display = "none"
const div = document.createElement('div');
text = `
<div class="error">
<h3>eisob amader restaurent e nai!</h3>
</div>`;
div.innerHTML = text;
meal.appendChild(div);
}
}
// meal details here
const showMealDetails = (string) => {
url = `https://www.themealdb.com/api/json/v1/1/search.php?s=${string}`;
fetch(url)
.then(res => res.json())
.then(data => {
result.style.display = "block"
const div = document.createElement('div');
let mealInfo;
data.meals.forEach(perItem => {
if (string === perItem.strMeal) {
mealInfo = `
<div>
<img src="${perItem.strMealThumb}" class="img-fluid rounded">
<div class="item-details">
<div class="my-3">
<h3>${perItem.strMeal}</h3>
<b>Ingredients</b>
</div>
<ul>
<li>${perItem.strIngredient1}</li>
<li>${perItem.strIngredient2}</li>
<li>${perItem.strIngredient3}</li>
<li>${perItem.strIngredient4}</li>
<li>${perItem.strIngredient5}</li>
<li>${perItem.strIngredient6}</li>
<li>${perItem.strIngredient7}</li>
<li>${perItem.strIngredient8}</li>
<li>${perItem.strIngredient9}</li>
<li>${perItem.strIngredient10}</li>
</ul>
</div>
</div>`;
}
})
div.innerHTML = mealInfo;
result.appendChild(div);
});
result.innerHTML = null;
}