-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
194 lines (137 loc) · 4.96 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
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
const formSubmit = document.querySelector(".flex-form"),
search = document.querySelector(".input-1"),
categoryBtn = document.querySelectorAll(".catli .category-btn"),
areaBtn = document.querySelectorAll(" .areali .area-btn"),
navbarToggler = document.querySelector(".navbar-toggler"),
navbarMenu = document.querySelector(".navbar"),
searchme = document.querySelector(".header-container .searchme"),
form = document.querySelector("form"),
randomMeal = document.querySelector(".random");
eventListeners();
function eventListeners() {
formSubmit.addEventListener("submit", showSearchReaslut);
navbarToggler.addEventListener("click", navbarTogglerClick);
searchme.addEventListener("click", searchClick);
}
function navbarTogglerClick() {
navbarToggler.classList.toggle("open-navbar-toggler");
navbarMenu.classList.toggle("active");
}
function searchClick() {
searchme.classList.toggle("fa-times");
form.classList.toggle("formActive");
}
function setAttribute(element, attributes) {
for (const key in attributes) {
element.setAttribute(key, attributes[key]);
}
}
function showSearchReaslut(e) {
e.preventDefault();
const input = search.value;
console.log(input);
if (input.trim()) {
console.log(input);
/* window.open("search.html?" + input);*/
window.location = "search.html?" + input;
} else {
alert("Value please")
}
search.value = "";
}
class Meals {
static showReasult(mealsme, cont) {
for (let meals of mealsme) {
const card = document.createElement("div");
card.setAttribute("class", "meal-card");
const image = document.createElement("div");
image.setAttribute("class", "image");
const text = document.createElement("div");
text.setAttribute("class", "text");
const img = document.createElement("img");
setAttribute(img, {
class: "myimg",
src: `${meals.strMealThumb}`,
alt: `${meals.strMeal}`
})
const h4 = document.createElement("h4");
h4.innerText = `${meals.strMeal}`;
const btn = document.createElement("button");
btn.setAttribute("class", "mybtn ");
btn.innerText = "Read more";
image.appendChild(img);
text.appendChild(h4);
text.appendChild(btn);
card.appendChild(image);
card.appendChild(text);
cont.appendChild(card);
clickBtn(meals, btn);
}
}
}
function clickBtn(meal, btn) {
btn.addEventListener("click", () => {
localStorage.setItem(meal.idMeal, JSON.stringify(meal));
console.log(localStorage);
window.open("ingredient.html?" + meal.idMeal);
})
}
function selectBtn(click) {
click.forEach(btn => {
btn.addEventListener("click", e => {
const clickBtn = btn.innerText;
console.log(clickBtn)
if (clickBtn) {
if (click === categoryBtn) {
let harf = "c";
window.open(`category.html? btn=${clickBtn}&harfi=${harf}`);
} else {
let harf = "a";
window.open(`category.html? btn=${clickBtn}&harfi=${harf}`);
}
} else {
console.log("Not found");
}
})
});
}
selectBtn(categoryBtn);
selectBtn(areaBtn);
async function getRandomApi() {
const response = await fetch(`https://www.themealdb.com/api/json/v1/1/random.php`);
const data = await response.json();
return data;
}
function showRandomMeal() {
getRandomApi().then(res => {
const ranmeal = res.meals[0];
console.log(ranmeal);
const h3 = document.createElement("h3");
h3.innerText = "Random Recipe";
const rancard = document.createElement("div");
rancard.setAttribute("class", "card random-card");
rancard.style.width = "18rem";
const ranimg = document.createElement("img");
setAttribute(ranimg, {
class: "card-img-top c-img",
src: `${ranmeal.strMealThumb}`,
alt: `${ranmeal.strMeal}`
});
const cardBody = document.createElement("div");
cardBody.setAttribute("class", "card-body c-body");
const title = document.createElement("h5");
const button = document.createElement("button");
button.setAttribute("class", "mybtn");
button.innerText = "Read more";
title.setAttribute("class", "c-title");
title.innerText = `${ranmeal.strMeal}`;
cardBody.appendChild(title);
cardBody.appendChild(button);
rancard.appendChild(ranimg);
rancard.appendChild(cardBody);
randomMeal.appendChild(h3);
randomMeal.appendChild(rancard);
clickBtn(ranmeal, button);
}).catch(err => console.log(err));
}
showRandomMeal();