-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
75 lines (67 loc) · 2.53 KB
/
script.js
File metadata and controls
75 lines (67 loc) · 2.53 KB
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
const mealsContainer = document.getElementById("meals-container");
const hamburgerBtn = document.getElementById("hamburger-btn");
const mobileMenu = document.getElementById("mobile-menu");
const homeButton = document.getElementById("homeButton");
// Toggle mobile menu visibility
hamburgerBtn.addEventListener("click", () => {
mobileMenu.classList.toggle("hidden");
});
// Function to fetch meals based on the category
async function fetchMeals(category) {
try {
const response = await fetch(
`https://www.themealdb.com/api/json/v1/1/search.php?s=${category}`
);
const data = await response.json();
displayMeals(data.meals); // Call function to display meals
} catch (error) {
console.error("Error fetching meals:", error);
}
}
// Function to display the fetched meals
function displayMeals(meals) {
mealsContainer.innerHTML = ""; // Clear previous content
meals.forEach((meal) => {
// Create a card for each meal
const mealCard = document.createElement("div");
mealCard.classList.add("card", "bg-base-100", "shadow-xl");
mealCard.innerHTML = `
<figure><img src="${meal.strMealThumb}" alt="${
meal.strMeal
}" class="w-full h-40 object-cover"></figure>
<div class="card-body">
<h2 class="card-title">${meal.strMeal}</h2>
<p>${meal.strInstructions.substring(0, 100)}...</p>
<div class="card-actions justify-end">
<a href="${
meal.strYoutube
}" class="btn btn-primary" target="_blank">Watch Recipe</a>
</div>
</div>
`;
mealsContainer.appendChild(mealCard); // Add the card to the container
});
}
// Event listeners for the category buttons
document.querySelectorAll(".btn").forEach((button) => {
button.addEventListener("click", () => {
const category = button.textContent;
fetchMeals(category); // Fetch and display meals for the selected category
});
});
// Show default "Potato" data when the page loads
document.addEventListener("DOMContentLoaded", () => {
fetchMeals("Potato"); // Fetch and display meals for the default category 'Potato'
});
// Handle Carousel Navigation
document.querySelectorAll(".btn.btn-circle").forEach((navButton) => {
navButton.addEventListener("click", () => {
fetchMeals("Potato");
// Re-fetch meals for the current category when carousel navigates
});
});
//home button function
homeButton.addEventListener("click", () => {
fetchMeals("Potato");
console.log("clicked");
});