-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
301 lines (268 loc) · 11.5 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Global Variables for DOM manipulation
const searchField = document.getElementById("searchField");
const displayAutoComplete = document.getElementById("displaySuggestions");
const displayResult = document.getElementById("displayResult");
const saveFavoritesDiv = document.getElementById("saveFavoritesDiv");
const saveRecipes = document.querySelector(".saveRecipes");
// const removeRecipes = document.querySelectorAll(".removeRecipes");
/*
/------------------------------------------------------------
/ Favorite Classes
/------------------------------------------------------------
/ Used for storing data to LocalStorage
*/
class Favorite {
constructor(id, meal) {
this.meal = meal;
this.id = id;
}
}
/*
/------------------------------------------------------------
/ StoreData Classes
/------------------------------------------------------------
/ Set/Get/Remove date from LocalStorage.
*/
class StoreData {
static getData() {
let meals;
if (localStorage.getItem("meals") === null) {
// create local storage array called "meals" and set it = []
meals = [];
} else {
meals = JSON.parse(localStorage.getItem("meals")); // if "meals" array exists return it to addData()
}
return meals;
}
static addData(meal) {
const saveFavoritesBtn = document.getElementById("saveBtn");
saveFavoritesBtn.addEventListener("click", (e) => {
saveFavoritesDiv.classList.add("show");
saveFavoritesBtn.innerHTML = ` <svg width="1.5em" height="1.5em" viewBox="0 0 16 16" class="bi bi-heart-fill" fill="red" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" d="M8 1.314C12.438-3.248 23.534 4.735 8 15-7.534 4.736 3.562-3.248 8 1.314z"/>
</svg>
`;
let hasId = false;
const meals = StoreData.getData(); //array from local storage
if (meals.length !== 0) {
meals.forEach((e) => {
if (hasId === true) {
return;
} else {
hasId = e.id.includes(meal.id);
}
});
}
if (hasId === false) {
meals.push(meal);
localStorage.setItem("meals", JSON.stringify(meals));
StoreData.saveFavoritesRecipe();
}
});
}
static saveFavoritesRecipe() {
saveRecipes.innerHTML = "";
StoreData.getData().forEach((e) => {
saveRecipes.innerHTML += `
<div class="recipeContainer">
<button id="${e.id}" class="savedRecipe">${e.meal}</button>
<div class="removeRecipes">X</div>
</div>`;
});
}
static removeData(el) {
// el parameter is coming remove button Onclick
const meals = StoreData.getData(); //array from local storage
meals.forEach((meal, index) => {
// loops iterate over each item in local storage array
if (meal.id == el.id) {
// if local storage array = to button e.target remove it from array
meals.splice(index, 1);
}
});
localStorage.setItem("meals", JSON.stringify(meals)); //push new data from user to local storage
}
}
/*
/------------------------------------------------------------
/ UI Classes
/------------------------------------------------------------
/ Update UI dynamical as user types. Display Recipe based on button click
/ Update DOM with API data and store to localStorage if needed
*/
class UI {
// Get Recipe using API data
static getRecipe = async (searchRecipe, fromWhere) => {
let mealArray = [];
try {
let res = await fetch(`https://www.themealdb.com/api/json/v1/1/search.php?s=${searchRecipe}`);
let data = await res.json();
mealArray = Object.values(data.meals);
displayAutoComplete.innerHTML = "";
mealArray.forEach((element) => {
displayAutoComplete.innerHTML += `<button id="${element.idMeal}" type="submit" class="btn btn-outline-light p-3 m-2 rounded-pill">${element.strMeal}</button>`;
});
if (fromWhere === "fromSavedRecipe") {
UI.dipslayRecipe(mealArray[0]);
} else {
UI.showRecipeOptions(mealArray);
}
} catch (err) {
// catches errors both in fetch and response.json
UI.noRecipeFound();
console.log(err);
}
};
// Alert user if no match is found
static noRecipeFound() {
const alertMsg = `<img src="Images/alert-circle.svg" width="50px" class="border-0 mb-3"/>
<h1 class="display-5 text-white p-3 border border-danger h3">No Result Found</h1>`;
displayAutoComplete.innerHTML = alertMsg;
}
// Autocomplete users search and display to UI
static showRecipeOptions(mealList) {
document.querySelectorAll("button").forEach((btnElement) => {
btnElement.addEventListener("click", (el) => {
mealList.forEach((meal) => {
meal.idMeal === el.target.id ? UI.dipslayRecipe(meal) : null;
displayAutoComplete.classList.add("hidden");
});
});
});
}
// API data recived from dipslayRecipe().
// Remove null and empty values and return
static filteredRecipeObjects = (obj, filter) => {
let key,
keys = [];
for (key in obj)
if (filter.test(key) && obj[key] !== "" && obj[key] !== " " && obj[key] !== null) {
let IngredientMeasurement = `<div class="text-white h4">
${obj[key]}
</div>`;
keys.push(IngredientMeasurement);
}
return keys.join("");
};
//Get meal recipe data from API and add it to DOM
static dipslayRecipe(mealUI) {
//Change save button icon based on localstorage
//----------------------- save button icon ---------------------------
let isSavedrecipe = false;
let saveIcon = null;
const isSavedIcon = `<svg width="1.5em" height="1.5em" viewBox="0 0 16 16" class="bi bi-heart-fill" fill="red" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" d="M8 1.314C12.438-3.248 23.534 4.735 8 15-7.534 4.736 3.562-3.248 8 1.314z"/>
</svg> `;
const notSavedIcon = `<svg width="1.5em" height="1.5em" viewBox="0 0 16 16" class="bi bi-heart" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" d="M8 2.748l-.717-.737C5.6.281 2.514.878 1.4 3.053c-.523 1.023-.641 2.5.314 4.385.92 1.815 2.834 3.989 6.286 6.357 3.452-2.368 5.365-4.542 6.286-6.357.955-1.886.838-3.362.314-4.385C13.486.878 10.4.28 8.717 2.01L8 2.748zM8 15C-7.333 4.868 3.279-3.04 7.824 1.143c.06.055.119.112.176.171a3.12 3.12 0 0 1 .176-.17C12.72-3.042 23.333 4.867 8 15z"/>
</svg>`;
const savedRecipeList = StoreData.getData();
savedRecipeList.forEach((e) => {
if (mealUI.idMeal === e.id) {
return (isSavedrecipe = true);
}
});
if (isSavedrecipe === true) {
saveIcon = isSavedIcon;
} else {
saveIcon = notSavedIcon;
}
//---------------------------------------------------------------
const savedMeal = new Favorite(mealUI.idMeal, mealUI.strMeal);
//Filtered data for Ingredient Measurement list
let filteredIngredient = UI.filteredRecipeObjects(mealUI, /strIngredient/);
let filteredMeasure = UI.filteredRecipeObjects(mealUI, /strMeasure/);
// clear input field
searchField.value = "";
// Add API data to UI
displayResult.innerHTML = `
<img src="${mealUI.strMealThumb}" class="img-fluid mt-4 rounded-pill" alt="Responsive image" width="200px">
<button type="button" class="btn btn-outline-light m-4 rounded-pill" id="saveBtn">
${saveIcon}
</button>
<h1 class="text-white mb-3"> ${mealUI.strMeal}</h1>
<h4 class="text-white mb-5 mx-auto">
<img src="Images/map-pin2.svg" width="25px"/>
${mealUI.strArea}<span class="h2 text-white"> | </span>
<img src="Images/grid2.svg" width="25px">
${mealUI.strCategory}
</h4>
<p class="h4 text-white mb-3" id="instruction">
<span class="h3 text-white">Instructions: </span>
${mealUI.strInstructions.replace(/\./g, ".<br>")}
</p>
<table class="table mx-auto w-100 text-center">
<thead>
<tr class="text-center">
<th class="pb-1" scope="col">Ingredient</th>
<th class="pb-1" scope="col" >Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td class="text-center px-0">${filteredIngredient}</td>
<td class="text-center px-0">${filteredMeasure}</td>
</tr>
</tbody>
</table>
<div class="source">
<img src="Images/link-2.svg" width="20px" class="border-0 mb-2 mr-2"/>
<a href="${mealUI.strSource}" target="_blank" class="text-white mr-4">Source</a>
<img src="Images/youtube.svg" width="20px" class="border-0 mb-2 mr-2"/>
<a href="${mealUI.strYoutube}" target="_blank" class="text-white mr-2">YouTube</a>
</div>
`;
// After "Save Recipe" button creation
// pass data to StoreData class and save it to LocalStorage
StoreData.addData(savedMeal);
}
//Remove meal from UI and DOM but not from the local storage.
static removemeal(el) {
el.parentElement.remove();
}
}
/*
/------------------------------------------------------------
/ Events
/------------------------------------------------------------
/ Pass Input Field value to UI class
/
*/
document.addEventListener("DOMContectLoaded", StoreData.saveFavoritesRecipe());
searchField.addEventListener("input", () => {
if (searchField.value.length === 0) {
//IF searchField is empty = hid it
displayAutoComplete.classList.add("hidden");
} else {
// User inputs and UI functions
UI.getRecipe(searchField.value, "fromAutoSearch");
displayAutoComplete.classList.remove("hidden");
}
});
//Toggle saved recipe list
document.querySelector(".showSavedRecipes").addEventListener("click", (e) => {
saveFavoritesDiv.classList.toggle("show");
});
//Display Recipe when user select from saved recipe list
saveRecipes.addEventListener("click", (e) => {
if (e.target.classList.contains("savedRecipe")) {
searchField.value = e.target.firstChild.data;
displayAutoComplete.classList.add("hidden");
saveFavoritesDiv.classList.remove("show");
UI.getRecipe(searchField.value, "fromSavedRecipe");
}
});
//Remove meal from UI and DOM but not from the local storage.
saveRecipes.addEventListener("click", (e) => {
if (e.target.classList.contains("removeRecipes")) {
UI.removemeal(e.target);
StoreData.removeData(e.target.previousElementSibling);
}
});
//hide save recipes on window scroll
saveFavoritesDiv.addEventListener("mouseleave", (e) => {
console.log(e);
window.addEventListener("scroll", (e) => {
saveFavoritesDiv.classList.remove("show");
});
});