-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
148 lines (125 loc) · 3.92 KB
/
script.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
console.clear();
let singleMeal = document.getElementById("single-meal");
let mealsWrapper = document.getElementById("meals");
let random = document.getElementById("random");
const submit = document.getElementById("submit"),
search = document.getElementById("search");
const lottie = `
<div class="video-wrapper">
<lottie-player src="https://assets1.lottiefiles.com/packages/lf20_snmohqxj/lottie_step2/data.json" background="transparent" speed="1" style="width: 500px; height: 100%;" loop autoplay></lottie-player>
</div>
`;
mealsWrapper.innerHTML = lottie;
// Generate meals by search
async function getMealFromSearch(e) {
e.preventDefault();
let searchTerm = search.value;
mealsWrapper.innerHTML = "";
hideSingleMealAnimation();
singleMeal.innerHTML = "";
document.getElementById("search-alert").innerHTML = "";
document.getElementById("search-alert").style.display = "none";
if (searchTerm.trim()) {
let response = await fetch(
`https://www.themealdb.com/api/json/v1/1/search.php?s=${searchTerm}`
);
let data = await response.json();
let meals = data.meals;
if (meals != null) {
meals.map((meal) => {
let mealLink = document.createElement("div");
mealLink.classList.add("meal");
mealLink.innerHTML = `
<figure>
<div class="color-overlay"></div>
<img src="${meal.strMealThumb}" alt="${meal.strMeal}" />
<figcaption>${meal.strMeal}</figcaption>
</figure>
`;
mealsWrapper.appendChild(mealLink);
mealLink.addEventListener(
"click",
() => {
addMealToDom(meal);
},
false
);
});
} else {
document.getElementById("search-alert").style.display = "block";
document.getElementById(
"search-alert"
).innerHTML = `We have no meals in our database that match ${searchTerm}, maybe you can try searching for something different?`;
}
} else {
alert("Please type in a meal or ingredient");
}
}
// Generate random meal
async function getRandomMeal() {
mealsWrapper.innerHTML = "";
let response = await fetch(
"https://www.themealdb.com/api/json/v1/1/random.php"
);
let data = await response.json();
data = await data.meals;
let randomMeal = await data[0];
addMealToDom(randomMeal);
}
// Helper function that adds a meal to the DOM
function addMealToDom(meal) {
let mealMedia = `<img src="${meal.strMealThumb}" alt="${meal.strMeal}" />`;
if (meal.strYoutube != null) {
mealMedia = `<iframe frameBorder="0" width=100% height=400 src='${watchToEmbed(
meal.strYoutube
)}'></iframe>`;
}
const ingredients = [];
for (let i = 1; i <= 20; i++) {
if (meal[`strIngredient${i}`]) {
ingredients.push(
`${meal[`strIngredient${i}`]} - ${meal[`strMeasure${i}`]}`
);
} else {
break;
}
}
singleMeal.innerHTML = `<button class="back-btn" onclick="hideSingleMealAnimation()"> ← Back </button>
<h1>${meal.strMeal}</h1>
${mealMedia}
<ul>
${ingredients.map((ingredient) => `<li>${ingredient}</li>`).join("")}
</ul>
<p>${meal.strInstructions}</p>
`;
showSingleMealsAnimation();
}
//Hide single meals animation
function hideSingleMealAnimation() {
gsap.to(".single-meals-wrapper", {
display: "none",
opacity: "0",
ease: "power1",
duration: ".3",
});
document.body.style.overflowY = "scroll";
}
// Show single meals animation
function showSingleMealsAnimation() {
let tl = gsap.timeline();
tl.to(".single-meals-wrapper", {
display: "flex",
opacity: "1",
ease: "power1",
duration: ".3",
});
window.scrollTo({ top: 0 });
document.body.style.overflowY = "hidden";
}
// Make YouTube videos embeddable
function watchToEmbed(string) {
return string.replace("watch?v=", "embed/");
}
// Event listeners
random.addEventListener("click", getRandomMeal, false);
submit.addEventListener("submit", getMealFromSearch, false);