forked from Technigo/js-project-recipe-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
218 lines (189 loc) · 6.77 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// === DOM SELECTORS ===
const filterButtons = document.querySelectorAll('.filter-checkbox');
const sortButtons = document.querySelectorAll('.sort-radio');
const lazyButton = document.getElementById('lazy');
const allButton = document.getElementById('all');
const recipeContainer = document.getElementById('recipe-container');
const loadingSpinner = document.getElementById('loading-spinner');
// === GLOBAL VARIABLES ===
let activeFilters = [];
let allRecipes = [];
let workingArray = [];
let selectedSort = 'ascending';
const BASE_URL = "https://api.spoonacular.com/recipes/random";
const API_KEY = "765002ba2ca14dffb9a0e1dd128843f1";
const URL = `${BASE_URL}/?apiKey=${API_KEY}&number=70`;
// === FETCH FROM API ===
const fetchData = async () => {
try {
showLoading(true);
const storedRecipes = localStorage.getItem("recipes");
const storedTimestamp = localStorage.getItem("recipesTimestamp");
const now = new Date();
if (storedRecipes && storedTimestamp) {
const isFresh = now - new Date(storedTimestamp) < 24 * 60 * 60 * 1000;
if (isFresh) {
allRecipes = JSON.parse(storedRecipes);
workingArray = [...allRecipes];
applySortAndRender();
return;
}
}
const response = await fetch(URL);
if (!response.ok) {
if (response.status === 402) {
throw new Error("API quota reached. Please try again tomorrow.");
} else {
throw new Error(`Error! Status: ${response.status}`);
}
}
const data = await response.json();
allRecipes = data.recipes;
localStorage.setItem("recipes", JSON.stringify(allRecipes));
localStorage.setItem("recipesTimestamp", now.toISOString());
workingArray = [...allRecipes];
applySortAndRender();
} catch (error) {
console.error('Fetch error:', error.message);
recipeContainer.innerHTML = `<p>${error.message}</p>`;
} finally {
showLoading(false);
}
};
const showLoading = (show) => {
loadingSpinner.style.display = show ? 'flex' : 'none';
};
// === RENDER FUNCTIONS ===
const renderIngredients = (array) => {
return `<ul>
${array.map(ingredient => {
const amount = ingredient.measures?.metric?.amount ? Math.ceil(ingredient.measures.metric.amount) : "?";
const unit = ingredient.measures?.metric?.unitShort || "";
return `<li>${amount} ${unit} ${ingredient.name}</li>`;
}).join('')}
</ul>`;
};
const renderInstructions = (recipe) => {
if (!recipe.analyzedInstructions || recipe.analyzedInstructions.length === 0) return "No instructions available.";
const steps = recipe.analyzedInstructions[0].steps;
return `<ol>${steps.map(step => `<li>${step.step}</li>`).join('')}</ol>`;
};
const loadRecipes = (array) => {
recipeContainer.innerHTML = '';
const validRecipes = array.filter(recipe =>
recipe.title &&
recipe.image &&
recipe.extendedIngredients &&
Array.isArray(recipe.extendedIngredients) &&
recipe.analyzedInstructions &&
Array.isArray(recipe.analyzedInstructions)
);
if (validRecipes.length === 0) {
const fallbackForCuisine = fallbackRecipes.filter(recipe =>
recipe.cuisines.some(cuisine =>
activeFilters.includes(cuisine.toLowerCase())
)
);
if (fallbackForCuisine.length > 0) {
recipeContainer.innerHTML = `<p>No recipes found from the API. Showing fallback results instead.</p>`;
loadRecipes(fallbackForCuisine);
} else {
recipeContainer.innerHTML = '<p>No recipes found. Try again or choose another filter.</p>';
}
return;
}
validRecipes.forEach(recipe => {
const imageUrl = recipe.image?.startsWith('http') ? recipe.image : 'https://via.placeholder.com/300x200?text=No+Image';
recipeContainer.innerHTML += `
<article class="recipe-card" style="width: 300px">
<img src="${imageUrl}" alt="${recipe.title}" onerror="this.src='https://via.placeholder.com/300x200?text=No+Image'">
<h3>${recipe.title}</h3>
<div class="border-top-bottom">
<p><strong>Ready in:</strong> ${recipe.readyInMinutes || "?"} minutes</p>
</div>
<p><strong>Ingredients:</strong> ${renderIngredients(recipe.extendedIngredients)}</p>
<button class="show-instructions-btn">Show Instructions</button>
<div class="instructions" style="display: none;"><strong>Instructions:</strong>${renderInstructions(recipe)}</div>
</article>
`;
});
document.querySelectorAll('.show-instructions-btn').forEach((btn) => {
btn.addEventListener('click', () => {
const instructionsDiv = btn.nextElementSibling;
const isVisible = instructionsDiv.style.display === 'block';
instructionsDiv.style.display = isVisible ? 'none' : 'block';
btn.textContent = isVisible ? 'Show Instructions' : 'Hide Instructions';
});
});
};
// === FILTER, SORT, LAZY ===
const updateButtons = (id) => {
if (id === 'all' || id === 'lazy') {
filterButtons.forEach(btn => {
if (btn.id !== 'all') btn.checked = false;
});
} else {
const anyChecked = Array.from(filterButtons).some(btn => btn.checked && btn.id !== 'all');
allButton.checked = !anyChecked;
}
const anyCheckedNew = Array.from(filterButtons).some(btn => btn.checked);
if (!anyCheckedNew) allButton.checked = true;
};
const updateFilters = (button) => {
if (button.id === 'all') {
activeFilters = [];
workingArray = [...allRecipes];
} else {
const filterId = button.id.toLowerCase();
if (button.checked && !activeFilters.includes(filterId)) {
activeFilters.push(filterId);
} else {
activeFilters = activeFilters.filter(f => f !== filterId);
}
workingArray = activeFilters.length > 0
? allRecipes.filter(recipe =>
recipe.cuisines?.some(cuisine =>
activeFilters.includes(cuisine.toLowerCase())
)
)
: [...allRecipes];
}
applySortAndRender();
};
const sortAscending = (array) => {
array.sort((a, b) => (a.readyInMinutes || 0) - (b.readyInMinutes || 0));
};
const sortDescending = (array) => {
array.sort((a, b) => (b.readyInMinutes || 0) - (a.readyInMinutes || 0));
};
const applySortAndRender = () => {
if (selectedSort === 'ascending') {
sortAscending(workingArray);
} else {
sortDescending(workingArray);
}
loadRecipes(workingArray);
};
const randomRecipe = () => {
const randomIndex = Math.floor(Math.random() * workingArray.length);
const chosen = [workingArray[randomIndex]];
loadRecipes(chosen);
};
// === INIT ===
fetchData();
filterButtons.forEach(button => {
button.addEventListener('click', () => {
updateButtons(button.id);
updateFilters(button);
});
});
sortButtons.forEach(button => {
button.addEventListener('click', () => {
selectedSort = button.id;
applySortAndRender();
});
});
lazyButton.addEventListener('click', () => {
updateButtons(lazyButton.id);
randomRecipe();
});