-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeals.js
112 lines (95 loc) · 3.24 KB
/
meals.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
import { addToCart } from './storage.js';
let allMeals = [];
let currentPage = 1;
let currentSearch = '';
const mealsPerPage = 9;
export async function fetchAllMeals() {
try {
const response = await fetch('https://www.themealdb.com/api/json/v1/1/search.php?s=');
const data = await response.json();
allMeals = data.meals || [];
return allMeals;
} catch (error) {
console.error('Error fetching meals:', error);
return [];
}
}
function searchMeals(searchTerm) {
return allMeals.filter(meal =>
meal.strMeal.toLowerCase().includes(searchTerm.toLowerCase()) ||
meal.strCategory.toLowerCase().includes(searchTerm.toLowerCase()) ||
meal.strArea.toLowerCase().includes(searchTerm.toLowerCase())
);
}
export function renderMeals(meals) {
const container = document.getElementById('mealsContainer');
if (!meals || meals.length === 0) {
container.innerHTML = `
<div class="error-message">
🍳 No recipes found! Try a different search.
</div>
`;
return;
}
container.innerHTML = meals.map(meal => `
<div class="meal-card" data-id="${meal.idMeal}">
<img src="${meal.strMealThumb}/preview" alt="${meal.strMeal}">
<h3>${meal.strMeal}</h3>
</div>
`).join('');
container.addEventListener('click', (e) => {
const mealCard = e.target.closest('.meal-card');
if (mealCard) {
const mealId = mealCard.dataset.id;
window.location.href = `meal-details.html?id=${mealId}`;
}
});
}
export function setupSearch() {
const searchInput = document.getElementById('searchInput');
searchInput.addEventListener('input', (e) => {
currentSearch = e.target.value.trim();
currentPage = 1;
updateDisplay();
});
}
function updatePaginationControls(filteredMeals) {
const prevButton = document.getElementById('prevPage');
const nextButton = document.getElementById('nextPage');
const pageDisplay = document.getElementById('currentPage');
const totalPages = Math.ceil(filteredMeals.length / mealsPerPage);
pageDisplay.textContent = `Page ${currentPage} of ${totalPages}`;
prevButton.disabled = currentPage === 1;
nextButton.disabled = currentPage === totalPages;
}
export function updateDisplay() {
let filteredMeals = allMeals;
if (currentSearch) {
filteredMeals = searchMeals(currentSearch);
}
renderMeals(filteredMeals);
updatePaginationControls(filteredMeals);
}
export function setupPagination() {
document.getElementById('prevPage').addEventListener('click', () => {
if (currentPage > 1) {
currentPage--;
updateDisplay();
}
});
document.getElementById('nextPage').addEventListener('click', () => {
const totalPages = Math.ceil(
(currentSearch ? searchMeals(currentSearch) : allMeals).length / mealsPerPage
);
if (currentPage < totalPages) {
currentPage++;
updateDisplay();
}
});
}
document.addEventListener('DOMContentLoaded', async () => {
await fetchAllMeals();
updateDisplay();
setupSearch();
setupPagination();
});