-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
206 lines (175 loc) · 6.36 KB
/
app.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const mealEl_container = document.querySelector('.meal')
const fav_meals_container = document.querySelector('.fav-meals');
const search_input = document.querySelector('.search-input');
const search_icon = document.querySelector('.search-icon');
const popup_container = document.querySelector('.pop-up-container');
const close_popup_btn = document.querySelector('.pop-up > i');
const popup = document.querySelector('.pop-up-inner');
const lightDarkModeSpan = document.querySelector('.light-dark-mode');
const lightDarkModeIcon = document.querySelector('.light-dark-mode > i');
getRandomMeal()
fetchFavMeals()
async function getRandomMeal () {
const resp = await fetch('https://www.themealdb.com/api/json/v1/1/random.php');
const respData = await resp.json()
const random_meal = respData.meals[0];
console.log(random_meal);
addMeal(random_meal)
}
async function getMealById (id) {
const resp = await fetch(`https://www.themealdb.com/api/json/v1/1/lookup.php?i=${id}`);
const respData = await resp.json()
const meal = respData.meals[0];
return meal;
}
async function getMealsBySearch (term) {
const resp = await fetch(`https://www.themealdb.com/api/json/v1/1/search.php?s=${term}`);
const respData = await resp.json()
const meals = respData.meals;
return meals;
}
function addMeal (meal) {
const meal_card = document.createElement('div');
meal_card.classList.add('meal-card');
meal_card.innerHTML = `
<div class="meal-card-img-container">
<img src="${meal.strMealThumb}">
</div>
<div class="meal-name">
<p>${meal.strMeal}</p>
<i class="fa-regular fa-heart"></i>
</div>
`
const btn = meal_card.querySelector('.fa-heart');
btn.addEventListener('click', () => {
if (btn.classList.contains('fa-regular')) {
btn.setAttribute('class', 'fa-solid fa-heart')
addMealLS(meal.idMeal)
} else {
btn.setAttribute('class', 'fa-regular fa-heart')
removeMealLS(meal.idMeal)
}
fetchFavMeals()
})
meal_card.firstChild.nextSibling.addEventListener('click', () => {
showMealPopup(meal)
})
mealEl_container.appendChild(meal_card)
}
function addMealLS (mealID) {
const mealIds = getMealLS()
localStorage.setItem('mealIds', JSON.stringify([...mealIds, mealID]))
}
function removeMealLS (mealID) {
const mealIds = getMealLS()
localStorage.setItem('mealIds', JSON.stringify(mealIds.filter(id => id !== mealID)))
}
function getMealLS () {
const mealIds = JSON.parse(localStorage.getItem('mealIds'));
return mealIds === null ? [] : mealIds
}
async function fetchFavMeals () {
fav_meals_container.innerHTML = '';
const mealsIds = getMealLS();
const meals = [];
for(let i = 0; i < mealsIds.length; i++) {
const mealID = mealsIds[i];
meal = await getMealById(mealID)
addMealToFav(meal)
meals.push(meal)
}
}
function addMealToFav (meal) {
const fav_meals = document.createElement('div');
fav_meals.innerHTML = `
<div class="single">
<div class="top">
<div class="img-container">
<img src="${meal.strMealThumb}">
</div>
<div class="text">
<p>${meal.strMeal}</p>
</div>
</div>
<i class="fa-solid fa-x"></i>
</div>
`
const x = fav_meals.querySelector('.fa-x');
x.addEventListener('click', () => {
removeMealLS(meal.idMeal)
const heart_btns = document.querySelectorAll('.fa-heart');
heart_btns.forEach(heart_btn => {
heart_btn.setAttribute('class', 'fa-regular fa-heart');
})
fetchFavMeals()
})
fav_meals.firstChild.nextSibling.firstChild.nextSibling.addEventListener('click', () => {
showMealPopup(meal)
})
fav_meals_container.appendChild(fav_meals)
}
search_icon.addEventListener('click', async () => {
mealEl_container.innerHTML = '';
const searchVal = search_input.value;
const meals = await getMealsBySearch(searchVal)
if (meals) {
meals.forEach(meal => {
addMeal(meal)
})
document.querySelector('.meals-container > h2').innerText = 'Search Results...'
} else {
document.querySelector('.meals-container > h2').innerText = 'No Meals Found'
mealEl_container.innerHTML = '';
}
})
close_popup_btn.addEventListener('click', () => {
popup_container.style.display = 'none';
})
function showMealPopup (meal) {
popup.innerHTML = ''
const newPopup = document.createElement('div');
newPopup.classList.add('pop-up-inner');
const ingredients = [];
for(let i = 1; i <= 20; i++) {
if (meal[`strIngredient${i}`]) {
ingredients.push(`${meal[`strIngredient${i}`]} - ${meal[`strMeasure${i}`]}`)
} else {
break
}
}
newPopup.innerHTML = `
<div class="left">
<div class="meal-card">
<div class="meal-card-img-container">
<img src="${meal.strMealThumb}" alt="">
</div>
<div class="meal-name">
<p>${meal.strMeal}</p>
<i class="fa-regular fa-heart"></i>
</div>
</div>
</div>
<div class="right">
<div>
<h2>Intructions</h2>
<p class="meal-info">${meal.strInstructions}</p>
</div>
<div>
<h2>Ingredients / Measures</h2>
<ul>
${ingredients.map(e => `<li>${e}</li>`).join('')}
</ul>
</div>
</div>
`
popup.appendChild(newPopup);
popup_container.style.display = 'flex';
}
lightDarkModeSpan.addEventListener('click', () => {
if (lightDarkModeIcon.classList.contains('fa-sun')) {
lightDarkModeIcon.setAttribute('class', 'fa-solid fa-moon')
} else {
lightDarkModeIcon.setAttribute('class', 'fa-solid fa-sun')
}
document.documentElement.classList.toggle('light-theme');
})