Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
214 lines (198 loc) · 7.5 KB
/
Copy pathscript.js
File metadata and controls
214 lines (198 loc) · 7.5 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
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
207
208
209
210
211
212
213
214
// imports search bar
const search_bar = document.getElementById("search-bar");
// imports submit /search button
const search_btn = document.getElementById("search-btn");
// imports show favourite button
const favourites = document.getElementById("show-fav");
// imports meal container
const mealsContainer = document.getElementById("meals");
// imports the heading of search results
const resultHeading = document.getElementById("search-result-info");
// imports single meal container
const single_meal_el = document.getElementById("single-meal");
// imports favourite list (aside one)
const favList = document.getElementById("fav-list");
// makes an empty array of name meals
let meals = [];
// searchh meal fn
function searchMeal(e) {
e.preventDefault();
// clear previous search
single_meal_el.innerHTML = "";
// get search meal results
const searchValue = search_bar.value;
// check for empty searchh bar
if (searchValue.trim()) {
fetch(`https://www.themealdb.com/api/json/v1/1/search.php?s=${searchValue}`
)
.then((res) => res.json())
.then((data) => {
resultHeading.innerHTML = `<h2>Search results for : ${searchValue}</h2>`
if (data.meals === null) {
resultHeading.innerHTML = `<h2>There are no such items as ${searchValue}</h2>`
} else {
mealsContainer.innerHTML = data.meals.map((meal) =>
`<div class="meal">
<img src="${meal.strMealThumb}" alt="${meal.strMeal}">
<div class="meal-info" data-mealID="${meal.idMeal}" onclick="openBox()">
<a href="#">
<h3 class="meal-name">${meal.strMeal}</h3>
</a>
</div>
<div class="fav" data-mealID="${meal.idMeal}" onclick="addToFav(event)">Add to favourites <i class="fa-regular fa-heart"></i></div>
</div>`
).join("")
}
})
// will give an alert box if the search bar is empty and button is clicked to search
} else {
alert("please insert some valid input")
}
}
// getting meal by id
function getMealById(mealId) {
fetch(
`https://www.themealdb.com/api/json/v1/1/lookup.php?i=${mealId}`
)
.then((res) => res.json())
.then((data) => {
const meal = data.meals[0];
addMealToDOM(meal);
})
}
// adding meal to dom when clicked in meals name
function addMealToDOM(meal) {
const ingredients = [];
for (let i = 1; i <= 20; i++) {
if (meal[`strIngredient${i}`]) {
ingredients.push(`
${meal[`strIngredient${i}`]}
-${meal[`strMeasure${i}`]
}
`);
} else {
break;
}
}
single_meal_el.innerHTML = `
<div class="single-meal-box">
<i class="fa fa-window-close box-close-btn" onclick="closeBox()"></i>
<div class="meal-name_img">
<h1>${meal.strMeal}</h1>
<img src="${meal.strMealThumb}" alt="${meal.strMeal}">
</div>
<div class="meal_ins_ing">
<h2>Instructions</h2>
<p>${meal.strInstructions}</p>
<h2>Ingredients</h2>
<ul>${ingredients.map((ing) => `<li>${ing}</li>`)}</ul>
</div>
</div>`
}
// function to open box when clicked in meal name
function openBox() {
document.getElementById("single-meal").classList.remove("inactive");
}
// function to open box when clicked in close button
function closeBox() {
document.getElementById("single-meal").classList.add("inactive");
}
// function to open favourites when clicked in show fav
function showFav() {
document.getElementById("aside").classList.remove("inactive")
}
// function to close favourites when clicked in close button
function closeFav() {
document.getElementById("aside").classList.add("inactive")
}
// function to add the meals to favourite list
function addToFav(e) {
const mealInfo = e.composedPath().find((item) => {
if (item.classList) {
return item.classList.contains('fav');
} else {
return false;
}
});
if (mealInfo) {
const mealID = mealInfo.getAttribute('data-mealID');
fetch(`https://www.themealdb.com/api/json/v1/1/lookup.php?i=${mealID}`)
.then((res) => res.json())
.then((data) => {
const meal = data.meals[0];
// store object in local store
let storeMeal = {
thumbnail: meal.strMealThumb,
heading: meal.strMeal,
};
// Push to global Array
meals.push(storeMeal);
window.localStorage.setItem('meal', JSON.stringify(meals));
let div = document.createElement('div');
div.innerHTML = `
<div class='fav-container'>
<img src = '${storeMeal.thumbnail}' alt = '${storeMeal.strMeal}' />
<h1 class='heading'>${storeMeal.heading}</h1>
<span class="del-fav" onclick="deleteFav(event)"><i class="fa-regular fa-trash-can"></i></span>
</div>`
// Rendering on DOM element
favList.appendChild(div);
});
}
}
// this delete function will delete an item fro meal array stored in the local storage
function deleteFav(e) {
console.log(e);
const mealInfo = e.composedPath().find((item) => {
if (item.classList) {
return item.classList.contains("fav-container");
} else {
return false;
}
});
let mealName = mealInfo.querySelector('h1').innerText;
// returns all heading except selected one of delete button
meals = meals.filter((meal) => {
return meal.heading != mealName;
});
// Updating local storage before removing favourite meal from list
window.localStorage.setItem('meal', JSON.stringify(meals));
mealInfo.remove();
}
// this function will load the arra saved with meal and it willl load on refresh without getting the favourite list getting deleted
function initialize() {
const mealsData = JSON.parse(window.localStorage.getItem('meal'));
// spread operator used to get the meals to run in for loop
meals = [...mealsData];
for (let i = 0; i < meals.length; i++) {
let getMeal = meals[i];
console.log(getMeal);
let div = document.createElement('div');
div.innerHTML = `
<div class='fav-container'>
<img src = '${getMeal.thumbnail}'/>
<h1 class='heading'>${getMeal.heading}</h1>
<span class="del-fav" onclick="deleteFav(event)"><i class="fa-regular fa-trash-can"></i></span>
</div>
`;
favList.appendChild(div);
}
};
// events listeners to search button
search_btn.addEventListener("click", searchMeal);
// event listener for meal clicked
mealsContainer.addEventListener("click", (e) => {
// clear previous search
single_meal_el.innerHTML = "";
const mealInfo = e.composedPath().find((item) => {
if (item.classList) {
return item.classList.contains("meal-info");
} else {
return false;
}
});
if (mealInfo) {
const mealId = mealInfo.getAttribute("data-mealID");
getMealById(mealId);
}
});