-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecipe.html
482 lines (422 loc) · 12.4 KB
/
recipe.html
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>NutriHelp</title>
<!-- FAVICON -->
<link rel="icon" type="image/png" href="./assets/images/favicon/meal-64.png" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.14.0/css/all.css"
integrity="sha384-HzLeBuhoNPvSl5KYnjx0BT+WB0QEEqLprO+NBkkk5gbc67FTaL7XIGa2w1L0Xbgc" crossorigin="anonymous" />
<style>
* {
box-sizing: border-box;
}
.logo{
color:#F9B208;
}
.nav{
position: fixed;
z-index: 1000;
top: 0;
right: 0;
left: 0;
height: 80px;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
padding: 0 25px 0 25px;
background-color: #1B1A17
}
.nav .links a{
margin-right: 25px;
font-size: 16px;
font-weight: 600;
color: #DDDDDD;
}
.nav .links .mainlink{
color: #EEEEEE;
}
.nav h4{
font-size: 22px;
font-weight: bold;
margin-left: 25px;
}
body {
background: #DDDDDD;
color: #fff;
font-family: Verdana, Geneva, Tahoma, sans-serif;
margin: 0;
}
.container {
margin: auto;
max-width: flex;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
color: #2d2013;
}
.error-message {
height: 100px;
width: 55%;
color: darkslategrey;
background-color: goldenrod;
font-size: 25px;
font-weight: 850;
text-decoration: underline;
}
.flex {
display: flex;
}
input,
button {
border: 1px solid #dedede;
border-top-left-radius: 4px;
border-bottom-left-radius: 4px;
font-size: 14px;
padding: 8px 10px;
margin: 0;
}
input[type="text"] {
width: 275px;
}
.search-btn {
cursor: pointer;
border-left: 0;
border-radius: 0;
border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
}
.random-btn {
cursor: pointer;
margin-left: 10px;
border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
border: transparent;
}
.meals {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 20px;
margin-top: 20px;
}
.meal {
cursor: pointer;
position: relative;
height: 180px;
width: 180px;
text-align: center;
}
.meal img {
width: 100%;
height: 100%;
border: 4px #fff;
border-radius: 20%;
border: solid;
border-width: 5px;
border-color: #222222;
}
.meal-info {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background: rgba(0, 0, 0, 0.7);
display: flex;
align-items: center;
justify-content: center;
transition: opactiy 0.3s ease-in;
opacity: 0;
}
.meal:hover .meal-info {
opacity: 1;
color: #fff;
border-radius: 20%;
}
.single-meal {
margin: 30px auto;
width: 75%;
}
.single-meal img {
width: 325px;
/* height: 425px; */
margin: 15px;
border: 5px #222222 solid;
border-radius: 20px;
}
.single-meal-info {
margin: 12px;
padding: 10px;
border: 2px #000000 double;
border-radius: 20px;
font-size: 17px;
background-color: #FFFFFF;
}
.single-meal p {
margin: 0;
letter-spacing: 0.5px;
line-height: 1.5;
text-align: center;
}
.single-meal .main p {
margin: 0;
letter-spacing: 0.5px;
line-height: 2;
text-align: justify;
}
.single-meal ul {
padding-left: 0;
list-style-type: none;
}
.single-meal ul li {
border: transparent;
border-radius: 5px;
background-color: #FFFFFF;
display: block;
color: #2d2013;
font-size: 15px;
font-weight: bold;
padding: 20px;
margin: 5px 5px 5px 5px;
}
@media (max-width: 800px) {
.meals {
grid-template-columns: repeat(3, 1fr);
}
}
@media (max-width: 700px) {
.meals {
grid-template-columns: repeat(2, 1fr);
}
.meal {
height: 200px;
width: 200px;
}
}
@media (max-width: 500px) {
input[type="text"] {
width: 100%;
}
.meals {
grid-template-columns: repeat(1, 1fr);
}
.meal {
height: 300px;
width: 300px;
}
}
</style>
</head>
<body>
<div class="nav">
<div class="logo">
<h4>NutriHelp.</h4>
</div>
<div class="links">
<a href="index.php" class="mainlink">HOME</a>
<a href="php/login.php">PROFILE</a>
<a href="#">FEATURE</a>
<a href="php/register.php">SIGN UP</a>
<a href="php/login.php">LOGIN</a>
</div>
</div>
<div id="container" class="container">
<h1>Find Your Recipe</h1>
<h1>Find Your Recipe</h1>
<div class="flex">
<br><br>
<form class="flex" id="submit-form">
<input type="text" id="search-input" placeholder="Search for meals by keywords..." />
<button class="search-btn" id="search-btn" type="submit">
<i class="fas fa-search"></i>
</button>
</form>
<button class="random-btn" id="random-btn">
<i class="fas fa-random"></i>
</button>
</div>
<div id="result-heading"></div>
<div id="meals" class="meals"></div>
<div id="single-meal"></div>
</div>
<script>
//MealDB API
// https://www.themealdb.com/api.php
//global variables for DOM elements
const searchEl = document.querySelector("#search-input"),
submitForm = document.querySelector("#submit-form"),
randomBtn = document.querySelector("#random-btn"),
mealsEl = document.querySelector("#meals"),
resultHeadingEl = document.querySelector("#result-heading"),
single_mealEl = document.querySelector("#single-meal");
containerEl = document.querySelector("#container");
//seach for meal and fetch from API
function searchMeal(e) {
e.preventDefault();
//clear single meal
single_mealEl.innerHTML = "";
//get search term
const searchTerm = searchEl.value;
// console.log(term)
//check for null input
if (searchTerm.trim()) {
//search URL with fetch API
fetch(`https://www.themealdb.com/api/json/v1/1/search.php?s=${searchTerm}`)
//JS promise returned as JSON
.then((res) => res.json())
//JS promise with data
.then((mealData) => {
// console.log(mealData);
//adding the heading with the Search Term included
resultHeadingEl.innerHTML = `<h3>Search results for "${searchTerm}": </h3>`;
//check to see if any meals with searchTerm
if (mealData.meals === null) {
//adding heading with no search terms
resultHeadingEl.innerHTML = `<p>That search yielded no results. Please try again!</p>`;
} else {
//dynamically adding information to meals id from HTML with map through the returned array
mealsEl.innerHTML = mealData.meals
.map(
//template literal to dynamiccaly include html through the map funciton
(meal) => `
<div class="meal">
<img src="${meal.strMealThumb}" alt="${meal.strMeal}"/>
<div class="meal-info" data-mealID="${meal.idMeal}">
<h3>${meal.strMeal}
</h3>
</div>
</div>
`
)
//taking the array and turning into a string
.join("");
}
});
//clear search text
searchEl.value = "";
//add placeholder text again
searchEl.placeholder = "Search for meals by keywords...";
//else logic for what to do if there were no search terms entered
} else {
//create div to contain error message
let errorDiv = document.createElement("div");
//add class of error-message for css styling
errorDiv.classList.add("error-message");
//insert innerHTML with the error message
errorDiv.innerHTML = "<p>Please enter search terms</p>";
//append to the div with class container
containerEl.appendChild(errorDiv);
//time interval to remove error message
setTimeout(clearErrorMessage, 5000);
}
}
//remove error message
function clearErrorMessage() {
//use vanilla JS to select item
let errorMessage = document.querySelector(".error-message");
//remove from HTML
errorMessage.remove();
}
//fetch meal by ID
function getMealByID(mealID) {
//fetch API with meal ID included
fetch(`https://www.themealdb.com/api/json/v1/1/lookup.php?i=${mealID}`)
//JS promise returning response and turning it into JSON
.then((res) => res.json())
//JS promise with JSON data
.then((recipeData) => {
//setting a variable to contain the returned meal from its place in the array
const meal = recipeData.meals[0];
//function to add meal to DOM
addMealToDOM(meal);
});
}
//fetch random meal from API
function getRandomMeal() {
//clear meals and heading
mealsEl.innerHTML = "";
resultHeadingEl.innerHTML = "";
//fetch API used to get random meal
fetch(`https://www.themealdb.com/api/json/v1/1/random.php`)
//JS promise turning response into JSON
.then((res) => res.json())
//JS promise using JSON
.then((randomData) => {
// console.log(randomData);
//variable to put random meal information from array into a variable
const meal = randomData.meals[0];
//funciton to add meal to DOM
addMealToDOM(meal);
});
}
//add recipe to DOM
function addMealToDOM(meal) {
//empty array to hold ingredients
const ingredients = [];
//for loop to go over list of returned ingredients and measurements
//no more than twenty returned
for (let i = 1; i <= 20; i++) {
//logic for if there is an ingredient in the JSON
if (meal[`strIngredient${i}`]) {
//add ingreadient and measurement to the end of the ingredients array
ingredients.push(
`${meal[`strMeasure${i}`]} - ${meal[`strIngredient${i}`]}`
);
//logic for what to do if there is no ingredient/measurement
} else {
//end the function
break;
}
}
//adding inner HTML to display the image, directions, and ingredients
single_mealEl.innerHTML = `
<div class="single-meal">
<h2>${meal.strMeal}</h2>
<img src="${meal.strMealThumb}" alt="${meal.strMeal}" />
<div class="single-meal-info">
${meal.strCategory ? `<p>${meal.strCategory}</p>` : ""}
${meal.strArea ? `<p>${meal.strArea}</p>` : ""}
</div>
<div class="main">
<p>${meal.strInstructions}</p>
<h3>Ingredients</h2>
<ul>
${ingredients
.map((ingredient) => `<li>${ingredient}</li>`)
.join("")}
</ul>
</div>
</div>
`;
}
//add event listeners
//action to complete when the form is submitted
submitForm.addEventListener("submit", searchMeal);
//get random meal
randomBtn.addEventListener("click", getRandomMeal);
//get meal by id for selected meal returned from search
mealsEl.addEventListener("click", (e) => {
//setting a variable mealinfo to see if the selected item has that class
const mealInfo = e.path.find((item) => {
if (item.classList) {
//meal-info dynamically added in search meal function
return item.classList.contains("meal-info");
//returning false if there is no class meal-info
} else {
return false;
}
});
//if there is the class meal-info then this is logic to use the mealid dynamically added
//in the search meal function to get meal by ID and display image, directions, and ingredient/measurements
if (mealInfo) {
const mealID = mealInfo.getAttribute("data-mealid");
getMealByID(mealID);
}
});
</script>
</body>
</html>