-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
138 lines (129 loc) · 3.83 KB
/
app.js
File metadata and controls
138 lines (129 loc) · 3.83 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const searchBtn = document.getElementById('searchBtb')
const mealsItem = document.getElementById('mealsItem')
const searchInput = document.getElementById('searchInput')
const form = document.getElementById('formSearch')
const singleItem = document.getElementById('singleItem')
const single = document.getElementById('single')
const main = document.getElementById('main')
// UI Element Generate
const uiElement = (html, itemClass) => {
let item = document.createElement('div')
item.className = `${itemClass}`
item.innerHTML = html
mealsItem.appendChild(item)
}
// UI ingredient Generate
const getInfo = (id) => {
singleItem.innerHTML = ''
main.style.display = 'none'
single.style.display = 'block'
fetchById(id)
.then((data) => ingredientFormate(data))
.catch((err) => console.log(err))
}
// UI Formate Generate
const uiFormate = (data) => {
const mealData = data.meals
const selectedMeal = mealData.splice(0, 8)
selectedMeal.forEach((meal) => {
const html = `
<div class="item">
<img onclick="getInfo(${meal.idMeal})" src=${meal.strMealThumb} alt="" />
<h4 class="item-title">
<a onclick="getInfo(${meal.idMeal})">${meal.strMeal}</a>
</h4>
</div>`
uiElement(html, 'col-md-3 main-item')
})
}
const ingredientFormate = (data) => {
const meal = data.meals[0]
// ingredient array to ingredient Value Generate
const ingToValue = (ing) => {
let value = meal[ing]
if (value !== null && value !== '') {
let list = document.createElement('li')
const item = `
<i class="fas fa-check-square"></i>
<span>${value}</span>
`
list.innerHTML = item
html2.appendChild(list)
console.log(value)
}
}
let html1
let html2 = document.createElement('ul')
html1 = `
<img src=${meal.strMealThumb} alt="" />
<h2>${meal.strMeal}</h2>
<h4>Ingredients</h4>
`
const mealKey = Object.keys(meal)
const ingredient = []
// Making ingredient array From Meal object
mealKey.forEach((key) => {
if (key.includes('strIngredient')) {
ingredient.push(key)
}
})
// ingredient array to ingredient Value
ingredient.forEach((ing) => {
ingToValue(ing)
})
let newDiv = document.createElement('div')
newDiv.innerHTML = html1
// Append Child for showing Ing. Output in UI
singleItem.appendChild(newDiv)
singleItem.appendChild(html2)
}
// Meal Result OutPut
const outputResult = (e) => {
mealsItem.innerHTML = ''
// Get Search Input Value
const searchValue = searchInput.value
// Search Simple Validation
if (searchValue == '') {
alert('Please Input Your Meal')
} else {
fetchData(searchValue)
.then((data) => {
// Formate And Generate UI using data
uiFormate(data)
})
.catch((err) => {
const html = `
<div class="item">
<h2>Your Meal Is Not Found In Our List Please Try Another Meal, Thanks </h2>
</div>`
// UI Element Generate
uiElement(html, 'col-md-12 error-item')
})
}
e.preventDefault()
}
// Display Single items event
document.getElementById('back-to-home').addEventListener('click', () => {
main.style.display = 'block'
single.style.display = 'none'
})
// Search Event By Submit Form
form.addEventListener('submit', outputResult)
// Search Event By Click btn
searchBtn.addEventListener('click', outputResult)
// Fetch Data From Api and query by name
async function fetchData(query) {
const response = await fetch(
`https://www.themealdb.com/api/json/v1/1/search.php?s=${query}`,
)
const data = response.json()
return data
}
// Fetch Data By Using Meal Id
async function fetchById(id) {
const response = await fetch(
`https://www.themealdb.com/api/json/v1/1/lookup.php?i=${id}`,
)
const data = response.json()
return data
}