-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
260 lines (228 loc) · 8.7 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
$(document).ready(function(){
// Uses the fetch() API to request category recipes from TheMealsDB.com API
fetch('https://www.themealdb.com/api/json/v1/1/list.php?c=list')
.then(res => res.json())
.then(res => {
res.meals.forEach(meal => {
let listCategory = ''
listCategory += `
<li class="navbar-item">
<a onclick="fetchCategoryMeal('${meal.strCategory}')"
class="navbar-link-category" tabindex="0" href="#mealCardsSection">${meal.strCategory}</a>
</li>`;
NavBarCategory.innerHTML += listCategory;
});
})
// Fetches random recipe
$('.btnRandomRecipe').on('click', function(){
fetchMeal('r');
// Textual updates
$('#dynamicTitle').text('The Random Recipe');
});
// Fetch searched recipe
$('.btnSearchRecipe').on('click', function(){
fetchMeal('u');
})
//also this could be easily refactored, maybe open issue for this too
// Fetch content after 3s
setTimeout(getData(['u', 'r']), 1000);
});
// Get recipe list based on search input
$(document).keypress(function(e) {
if( e.which == 13 && $.trim($('#searchRecipe').val()) !== '' ) {
fetchMeal('u');
}
});
// Show recipe of clicked meal
$(document).on('click','.mealCardRecipeBtn',function(){
let meal = $(this).data('meal');
if(meal.strCategory === undefined){
fetch('https://www.themealdb.com/api/json/v1/1/lookup.php?i='+meal.idMeal)
.then( res => res.json() )
.then( res => {
meal = res.meals[0];
window.scrollTo(0,$('#random').offset().top);
createMeal(meal,'r');
// Textual updates
$('#dynamicTitle').text(meal.strMeal);
})
} else {
window.scrollTo(0,$('#random').offset().top);
createMeal(meal,'r');
// Textual updates
$('#dynamicTitle').text(meal.strMeal);
}
});
// Clear search box on button press
$(document).on('click','.clear-field',function(){
document.getElementById('searchRecipe').value = '';
});
// Uses the fetch() API to request random meal recipe from TheMealsDB.com API
function fetchMeal(type){
let url = '';
if ( type === 'r') { url = 'https://www.themealdb.com/api/json/v1/1/random.php'; }
if ( type === 'r' ) {
fetch(url)
.then( res => res.json() )
.then( res => {
createMeal(res.meals[0], type);
setCache(res.meals[0], type);
})
.catch( e => console.warn(e) );
} else {
fetch('https://www.themealdb.com/api/json/v1/1/search.php?s='+$.trim($('#searchRecipe').val()))
.then( res => res.json() )
.then( res => {
let user_search_term = $.trim($('#searchRecipe').val());
if (res.meals) {
$("#errorMessageContainer").remove();
createMealCards(res.meals);
window.scrollTo(0,$('#mealCardsSection').offset().top);
$('#userInput').text(user_search_term);
setCache(res.meals, type);
} else {
$("#mealCardsSection .container").hide();
$("#mealCardsSection").prepend("<div id='errorMessageContainer' style='display:flex;'> <p id='errorMessageText'>No recipes match the search term '" + user_search_term + "'</p> <a id='errorMessageBtn' class='button' href='#landing' title='Search again' >Search again</a> </div>")
}
})
.catch( e => console.warn(e) );
}
}
// remove error message
$(document).on('click','#errorMessageBtn',function(){
$("#errorMessageContainer").remove();
});
// Function to save the data in the cache
const setCache = (meal, type) => {
let mealJson = JSON.stringify(meal);
if( type === 'u' ){
sessionStorage.setItem("search", $.trim($('#searchRecipe').val()));
sessionStorage.setItem(type, mealJson);
} else setCookie(type, mealJson);
}
// Function to set the cookie
const setCookie = (key, value, exDays = 3) => {
let date = new Date();
date.setTime(date.getTime() + exDays*24*60*60*1000);
document.cookie = key + "=" + value + "; expires=" + date.toUTCString() + ";path=/";
}
// Function to get cookie
const getCookie = (key) => {
key = key + "=";
var cookies = document.cookie.split(';');
for(var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
while (cookie.charAt(0) == ' ') cookie = cookie.substring(1);
if (cookie.indexOf(key) == 0) { return cookie.substring(key.length, cookie.length) };
}
return null;
}
// Function to get cache data if it exists, otherwise, fetch from the API
const getData = (types) => {
types.forEach(type => {
if( type === "u" ) {
let mealData = JSON.parse(sessionStorage.getItem(type));
if( mealData !== null ) {
createMealCards(mealData);
window.scrollTo(0,$('#mealCardsSection').offset().top);
$('#userInput').text(sessionStorage.getItem("search"));
}
}
else {
let mealData = null;
try {
mealData = JSON.parse(getCookie(type));
} catch (error) { console.warn(error) };
mealData !== null ? createMeal(mealData, type) : fetchMeal(type);
}
})
}
function fetchCategoryMeal(category){
fetch('https://www.themealdb.com/api/json/v1/1/filter.php?c=' + category)
.then(res => res.json())
.then(res => {
createMealCards(res.meals);
window.scrollTo(0, $('#mealCardsSection').offset().top);
})
.catch(e => console.warn(e));
$('#userInput').text(category);
}
// Function to generate the random meal UI component
const createMeal = (meal,type) => {
// Set meal thumbnail
setMealThumbnail(meal,type);
let mealMetadata = '', mealInstr = '';
// Fill meal name
if ( meal.strMeal ) {
mealMetadata = `<span>Name:</span> ${meal.strMeal} <br/>`
}
// Fill Area
if ( meal.strArea ) {
mealMetadata += `<span>Area:</span> ${meal.strArea} <br/>`
}
// Fill category
if ( meal.strCategory ) {
mealMetadata += `<span>Category:</span> ${meal.strCategory} <br/>`
}
// Format tags with comma-whitespace separator
if ( meal.strTags ) {
mealMetadata += `<span>Tags:</span> ${meal.strTags.split(',').join(', ')} <br/>`
}
// Set YouTube link
if ( meal.strYoutube ) {
mealMetadata +=`<span>YouTube:</span> <a href='${meal.strYoutube}' target="_blank" title="Watch how to cook ${meal.strMeal}">${meal.strYoutube}</a><br/>`
}
// Set Source link
if ( meal.strSource ) {
mealMetadata +=`<span>Source:</span> <a href='${meal.strSource}' target="_blank" title="Watch how to cook ${meal.strMeal}">${meal.strSource}</a><br/>`
}
// Fill ingredients
let ingredients = [];
setIngredients(meal, ingredients);
if ( ingredients.length > 0 ) {
mealMetadata +=`<span>Ingredients:</span> <br/> <ul>${ingredients.join('')}</ul>`
}
// Set instructions
if ( meal.strInstructions ) {
mealInstr =`<span>Instructions:</span> <br/> ${meal.strInstructions}`
}
if ( type === 'r') {
$('#randomMealMetadata').html(mealMetadata);
$('#randomMealInstructions').html(mealInstr);
}
}
// Sets random meal's thumbnail image
const setMealThumbnail = (meal,type) => {
let imgSrc = `<img src="${meal.strMealThumb}" alt="${meal.strMeal}" title="${meal.strMeal}" />`;
if ( type === 'r') { $('#randomMealImg').html(imgSrc); }
}
// Gets ingredients of the random meal
const setIngredients = (meal,ingredients) => {
// API returns max. 20 ingredients
for(let i = 1; i <= 20; i++){
if(meal[`strIngredient${i}`]){
ingredients.push(
`<li>${meal[`strIngredient${i}`]} - ${meal[`strMeasure${i}`]}</li>`
);
} else { break; }
if ( i % 2 === 0 ) { ingredients.push('<br/>'); }
}
}
// Creates meal cards based on search form
const createMealCards = meals => {
let mealCards = '';
meals.forEach(meal => {
mealData = JSON.stringify(meal);
mealData = mealData.replace(/(['])/g, "’");
mealCards +=
`<div class="four columns"><div class="card">
<img src="${meal.strMealThumb}" alt="${meal.strMeal}" title="${meal.strMeal}" class="u-max-full-width" />
<div class="card-body">
<div class="cardTitle">${meal.strMeal}</div>
<button class="button mealCardRecipeBtn" data-meal='${mealData}'>Recipe</button>
</div>
</div></div>`;
});
$('.mealCards').html(mealCards);
$('#mealCardsSection .container').show();
}