forked from Technigo/js-project-recipe-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
569 lines (493 loc) · 20 KB
/
script.js
File metadata and controls
569 lines (493 loc) · 20 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/* ===========================================================
Recipe Finder App
Code is split up into small, named functions that describe what they do(purpose).
Each section is clearly marked with a header.
Sections:
-1 API config & fetch
-2 Cache handling
-3 DOM helpers
-4 String helpers
-5 Normalization helpers
-6 Cache functions
-7 Fetch with quota handling
-8 Filtering & sorting
-9 Rendering
-10 Event handling & init + random recipe
=========================================================== */
// Import backup data
import { backupData } from './backupData.js';
/* ===========================================================
1) API CONFIG
=========================================================== */
const API_KEY = '42a3e506a5a6493080872a8509f9c7d5';
const CUISINES = ['Italian', 'American', 'Chinese', 'Asian', 'Mediterranean', 'Middle Eastern'];
const API_URL = (n = 24) =>
`https://api.spoonacular.com/recipes/complexSearch?apiKey=${API_KEY}&number=${n}&cuisine=${encodeURIComponent(
CUISINES.join(',')
)}&addRecipeInformation=true&instructionsRequired=true&fillIngredients=true&sort=random`;
/* ===========================================================
2) CACHE + UI CONSTANTS
PURPOSE:These constants + the global array are shared settings/memory:
- CACHE_KEY → name used to save/load recipes in localStorage
- CACHE_TTL_MS → how long cached data is valid (6h). After that we try to fetch fresh data.
- MAX_INGREDIENTS → keep recipe cards short by limiting the number of shown ingredients
- RECIPES → global array holding all normalized recipes (so filter/sort/render share the same data)
We keep a separate cache for /recipes/{id}/information responses.
- DETAIL_TTL_MS can be longer than list cache (24h is fine).
- Data structure in localStorage: { ts, data: { [id]: fullRecipeObj } }
===========================================================*/
const CACHE_KEY = 'spoon_recipes_cache_v1';
const CACHE_TTL_MS = 1000 * 60 * 60 * 6; // 6 hours
const MAX_INGREDIENTS = 4;
let RECIPES = [];
const DETAIL_CACHE_KEY = 'spoon_recipe_detail_cache_v1';
const DETAIL_TTL_MS = 1000 * 60 * 60 * 24; // 24 hours
/* ===========================================================
3) DOM HELPERS
=========================================================== */
// $() → shortcut for document.getElementById()
// grid → reference to <section id="grid"> where recipe cards go
const $ = (id) => document.getElementById(id);
const grid = $('grid');
const setBusy = (on) => grid.toggleAttribute('aria-busy', !!on);
// overlay refs for the view recipe popup
const overlayEl = document.getElementById('popup-recipe');
const overlayBodyEl = document.getElementById('popup-recipe-content');
/* ===========================================================
4) STRING HELPERS
Purpose: Avoid duplicating string transforms in multiple places.
- toKebabCase: "Middle Eastern" → "middle-eastern" (stable codes for filters)
- toTitleCase: "middle-eastern" → "Middle Eastern" (friendly UI labels)
===========================================================*/
function toKebabCase(str = '') {
return String(str).trim().toLowerCase().replace(/\s+/g, '-');
}
function toTitleCase(str = '') {
return String(str).replace(/-/g, ' ').replace(/\b\w/g, (c) => c.toUpperCase());
}
// Helpers to transform numbers/codes to user-friendly labels.
function minutesToLabel(mins) {
if (mins < 15) return 'Under 15 min';
if (mins <= 30) return '15–30 min';
if (mins <= 60) return '30–60 min';
return 'Over 60 min';
}
function starsFromPopularity(p) {
const n = Math.max(0, Math.min(5, Math.round(p / 20)));
return '★'.repeat(n) + '☆'.repeat(5 - n);
}
// Converts text to safe HTML (used when injecting API text)
function escapeHTML(str) {
return String(str)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
/* ---------- Instructions HTML builder ----------
Reason: Spoonacular sometimes returns:
• structured analyzedInstructions[].steps
• AND/OR a big free-text “instructions” string that already contains numbers.
We normalize to a clean <ol><li>…</li></ol> with our own numbering,
and we strip any leading “1. ” / “2) ” etc. to avoid double numbers.
--------------------------------------------------*/
function buildInstructionsHTML(recipe) {
const steps = recipe?.analyzedInstructions?.[0]?.steps;
if (Array.isArray(steps) && steps.length) {
const items = steps
.map(s => `<li>${escapeHTML(s.step || '').trim()}</li>`)
.join('');
return `<ol>${items}</ol>`;
}
const raw = (recipe?.instructions || '').trim();
if (!raw) return '<p>No instructions provided.</p>';
const text = raw.replace(/<[^>]+>/g, ' '); // strip any embedded HTML
const chunks = text
.split(/\n+|(?<=\.)\s+(?=[A-Z0-9])/)
.map(s => s.trim())
.filter(Boolean);
const cleaned = chunks.map(s => s.replace(/^\s*\d+[\.\)]\s*/, '')); // remove "1. " / "2) " etc.
const items = cleaned.map(s => `<li>${escapeHTML(s)}</li>`).join('');
return `<ol>${items}</ol>`;
}
/* ===========================================================
5) NORMALIZATION HELPERS
PURPOSE: The API returns big objects with many fields and inconsistent shapes.
normalizeRecipe() cleans and reformats data into a consistent structure
that our UI can easily use.
===========================================================*/
function normalizeRecipe(recipe) {
if (!recipe) throw new Error('Recipe input is null or undefined');
// Read first cuisine safely (optional chaining) or default to 'unknown'.
// Then convert to kebab-case so filters are simple and consistent.
const cuisineCode = toKebabCase(recipe.cuisines?.[0] || 'unknown');
const dietCode = resolveDietCode(recipe);
const pop = resolvePopularity(recipe);
return mapToNormalizedRecipe(recipe, cuisineCode, dietCode, pop);
}
// Order of priority: vegan > vegetarian > gluten-free > dairy-free > none.
function resolveDietCode(recipe) {
if (recipe.vegan || recipe.diets?.includes?.('vegan')) return 'vegan';
if (recipe.vegetarian || recipe.diets?.includes?.('vegetarian')) return 'vegetarian';
if (recipe.glutenFree || recipe.diets?.includes?.('gluten free')) return 'gluten-free';
if (recipe.dairyFree || recipe.diets?.includes?.('dairy free')) return 'dairy-free';
return 'none';
}
// Prefer spoonacularScore (0–100), fallback to aggregateLikes capped at 100
function resolvePopularity(recipe) {
if (typeof recipe.spoonacularScore === 'number') return recipe.spoonacularScore;
return Math.min(100, recipe.aggregateLikes || 0);
}
function mapToNormalizedRecipe(recipe, cuisineCode, dietCode, popularity) {
return {
id: recipe.id,
title: recipe.title || 'Untitled recipe',
cuisine: cuisineCode,
cuisines: (recipe.cuisines || []).map(toKebabCase),
diet: dietCode,
timeMin: recipe.readyInMinutes || 0,
popularity,
imageUrl:
recipe.image ||
'https://images.unsplash.com/photo-1504674900247-0877df9cc836?q=80&w=1200&auto=format&fit=crop',
ingredients: (recipe.extendedIngredients || [])
.map((i) => i?.name)
.filter(Boolean)
.slice(0, 24),
};
}
/* ===========================================================
6) CACHE FUNCTIONS (localStorage)
PURPOSE: Reduce API calls (quota), speed up UI, work better with flaky networks.
We save normalized recipes + timestamp, then read them if still within TTL.
=========================================================== */
// One reader for both “fresh” (honor TTL) and “stale” (ignore TTL) reads
function readCache(ignoreTTL = false) {
try {
const raw = localStorage.getItem(CACHE_KEY);
if (!raw) return null;
const obj = JSON.parse(raw);
if (!obj || !Array.isArray(obj.data)) return null;
if (!ignoreTTL && (!obj.ts || Date.now() - obj.ts > CACHE_TTL_MS)) return null; // expired
return obj.data;
} catch {
return null;
}
}
function writeCache(recipes) {
try {
localStorage.setItem(CACHE_KEY, JSON.stringify({ ts: Date.now(), data: recipes }));
} catch {
// storage full/blocked – fail silently
}
}
/* detail cache helpers (for /recipes/{id}/information)
These prevent repeated API calls when the user opens the same recipe
multiple times or navigates back and forth. */
function readDetailCache() {
try {
const raw = localStorage.getItem(DETAIL_CACHE_KEY);
return raw ? JSON.parse(raw) : { ts: 0, data: {} };
} catch {
return { ts: 0, data: {} };
}
}
function writeDetailCache(store) {
try {
localStorage.setItem(DETAIL_CACHE_KEY, JSON.stringify(store));
} catch {
// ignore quota errors
}
}
// Unified accessor that returns cached detail (if fresh) or fetches/saves one
async function getRecipeInfo(id) {
const store = readDetailCache();
const fresh = Date.now() - (store.ts || 0) < DETAIL_TTL_MS;
if (fresh && store.data && store.data[id]) return store.data[id];
const res = await fetch(
`https://api.spoonacular.com/recipes/${id}/information?apiKey=${API_KEY}`
);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const json = await res.json();
const next = { ts: Date.now(), data: { ...(store.data || {}), [id]: json } };
writeDetailCache(next);
return json;
}
/* ===========================================================
7) FETCH (API + fallback) with QUOTA messaging
PURPOSE: Show loading → try cache → try network → normalize/save/render → handle errors.
Includes a stale-cache + backupData fallback to keep UI useful when offline/quota.
=========================================================== */
async function fetchRecipes(count = 24) {
// clear any previous UI note so status doesn't keep old messages
delete $('status').dataset.note;
grid.innerHTML = '<div class="loading">Loading recipes…</div>';
$('status').textContent = 'Loading recipes…';
setBusy(true);
// 1) Try fresh cache first
const fresh = readCache(false);
if (fresh) {
RECIPES = fresh;
return render(getVisibleRecipes(), 'cache');
}
// 2) Try API
try {
const res = await fetch(API_URL(count));
if (!res.ok) {
if (res.status === 402 || res.status === 429) throw new Error('QUOTA');
throw new Error(`HTTP ${res.status}`);
}
const data = await res.json();
const normalized = (data.results || data.recipes || []).map(normalizeRecipe);
if (!normalized.length) throw new Error('Empty');
RECIPES = normalized;
writeCache(RECIPES);
return render(getVisibleRecipes(), 'api');
} catch (err) {
const isQuota = err?.message === 'QUOTA';
$('status').textContent = isQuota
? 'Daily API quota reached — attempting cached recipes.'
: 'Network error — attempting cached recipes.';
// mark status element with a machine-readable note (used by updateStatus)
$('status').dataset.note = isQuota ? 'quota' : 'offline';
// 3a) Stale cache (ignore TTL)
const stale = readCache(true);
if (stale?.length) {
RECIPES = stale;
return render(getVisibleRecipes(), 'cache (stale)');
}
// 3b) BackupData fallback (bundled static data)
if (Array.isArray(backupData) && backupData.length) {
RECIPES = backupData.map(normalizeRecipe);
$('status').textContent = isQuota
? 'Quota reached — showing bundled backup data.'
: 'Offline — showing bundled backup data.';
return render(getVisibleRecipes(), 'backup');
}
// 3c) Empty state
RECIPES = [];
grid.innerHTML =
'<div class="empty">Couldn’t fetch recipes (quota/offline). Try again later.</div>';
setBusy(false);
}
}
/* ===========================================================
8) FILTERING + SORTING
PURPOSE: Take the full set of RECIPES and produce a new array
that matches the current dropdown values (cuisine/diet + sorting).
Non-mutating patterns are used (returns new arrays).
=========================================================== */
// Get current UI selections from the dropdowns. Empty string "" means “no filter/sort”
function getSelectedCuisine() {
return $('cuisine').value || '';
}
function getSelectedDiet() {
return $('diet').value || '';
}
function getSelectedSortTime() {
return $('sortTime').value || '';
}
function getSelectedSortPop() {
return $('sortPop').value || '';
}
function getQuery() {
return ($('q')?.value || '').trim().toLowerCase();
}
// FILTERING — applies current dropdown selections (cuisine, diet) + free-text search
function filterRecipes(list) {
const cuisine = getSelectedCuisine();
const diet = getSelectedDiet();
const q = getQuery();
return list.filter((r) => {
const passCuisine = cuisine ? r.cuisine === cuisine || r.cuisines?.includes(cuisine) : true;
const passDiet = diet ? r.diet === diet : true;
const passQuery = q
? r.title.toLowerCase().includes(q) ||
r.ingredients.some((i) => i.toLowerCase().includes(q))
: true;
return passCuisine && passDiet && passQuery;
});
}
// SORTING — popularity (most/least) + time (asc/desc)
function sortRecipes(list) {
const sTime = getSelectedSortTime();
const sPop = getSelectedSortPop();
const arr = [...list]; // copy first
arr.sort((a, b) => {
// Primary: popularity (if selected)
if (sPop === 'most' && a.popularity !== b.popularity) return b.popularity - a.popularity;
if (sPop === 'least' && a.popularity !== b.popularity) return a.popularity - b.popularity;
// Secondary (or primary if no popularity sort): time
if (sTime === 'asc') return a.timeMin - b.timeMin;
if (sTime === 'desc') return b.timeMin - a.timeMin;
return 0;
});
return arr;
}
// Compose the two steps: FILTER first, then SORT
const getVisibleRecipes = () => sortRecipes(filterRecipes(RECIPES));
/* ===========================================================
9) RENDER
PURPOSE: Turn the (filtered + sorted) array into real DOM cards.
We clone a <template> per recipe and fill its fields.
We also update a live status line for accessibility/debugging.
=========================================================== */
// build one card DOM node from a normalized recipe
function buildCard(r) {
const tpl = $('cardTpl');
const node = tpl.content.cloneNode(true);
// Fill img + title
node.querySelector('.card-img').src = r.imageUrl;
node.querySelector('.card-img').alt = r.title;
node.querySelector('.card-title').textContent = r.title;
// Fill meta info
node.querySelector('.meta-cuisine').textContent = toTitleCase(r.cuisine);
node.querySelector('.meta-diet').textContent = toTitleCase(r.diet);
node.querySelector('.meta-pop').textContent = starsFromPopularity(r.popularity);
node.querySelector('.meta-time').textContent = minutesToLabel(r.timeMin);
// Ingredients list
const ul = node.querySelector('.ing-list');
const list =
r.ingredients.length > MAX_INGREDIENTS
? [...r.ingredients.slice(0, MAX_INGREDIENTS), '…']
: r.ingredients;
list.forEach((i) => {
const li = document.createElement('li');
li.textContent = i;
ul.appendChild(li);
});
// compact “View recipe” button on each card
const btn = document.createElement('button');
btn.type = 'button';
btn.className = 'btn btn-view';
btn.textContent = 'View recipe';
btn.addEventListener('click', () => showRecipeDetails(r.id));
node.querySelector('.card').appendChild(btn);
return node;
}
// shows the given list of recipes in the grid and updates status line
function render(list, sourceLabel = 'filters') {
grid.innerHTML = '';
if (!list.length) {
grid.innerHTML =
'<div class="empty">No recipes match your filters, please try another option.</div>';
updateStatus(0, sourceLabel);
setBusy(false);
return;
}
const frag = document.createDocumentFragment();
list.forEach((r) => frag.appendChild(buildCard(r)));
grid.appendChild(frag);
updateStatus(list.length, sourceLabel);
setBusy(false);
}
// Simplified user-friendly status messages
// appends a clear user-facing note when quota/offline fallback is active
function updateStatus(count, source) {
if (count === 0) {
$('status').textContent = ''; // hide status when grid is empty
return;
}
const q = getQuery();
const base = q
? `Found ${count} recipe(s) matching "${q}".`
: `Showing ${count} recipe(s).`;
// Note set in fetchRecipes() catch: 'quota' | 'offline' | undefined
const noteType = $('status').dataset.note;
const note =
noteType === 'quota'
? ' (daily API quota reached — showing cached/backup data)'
: noteType === 'offline'
? ' (offline/unavailable — showing cached/backup data)'
: '';
$('status').textContent = base + note;
}
// View recipe popup (open/close + fetch details)
function openOverlay() {
overlayEl?.classList.add('is-open');
overlayEl?.setAttribute('aria-hidden', 'false');
}
function closeOverlay() {
overlayEl?.classList.remove('is-open');
overlayEl?.setAttribute('aria-hidden', 'true');
if (overlayBodyEl) overlayBodyEl.innerHTML = '';
}
// fetch + render recipe details into popup
async function showRecipeDetails(recipeId) {
if (!overlayEl || !overlayBodyEl) return;
openOverlay();
overlayBodyEl.innerHTML = '<p>Loading…</p>';
try {
// use cached detail accessor instead of raw fetch
const r = await getRecipeInfo(recipeId);
const ingredients = Array.isArray(r.extendedIngredients)
? r.extendedIngredients.map((i) => i.original).filter(Boolean)
: [];
// Build instructions HTML (normalized numbering)
const instructionsHTML = buildInstructionsHTML(r);
overlayBodyEl.innerHTML = `
<img src="${r.image}" alt="${escapeHTML(r.title)}">
<h2>${escapeHTML(r.title)}</h2>
<p><strong>Cuisine:</strong> ${escapeHTML(r.cuisines?.join(', ') || 'N/A')}</p>
<p><strong>Ready in:</strong> ${r.readyInMinutes || '—'} min</p>
<p><strong>Ingredients:</strong></p>
<ul class="ing">
${ingredients.map((i) => `<li>${escapeHTML(i)}</li>`).join('')}
</ul>
<h3>Instructions:</h3>
<div class="inst">
${instructionsHTML}
</div>
`;
} catch (_) {
overlayBodyEl.innerHTML = '<p>Couldn’t load recipe details.</p>';
}
}
// close popup on backdrop click / X button / ESC
overlayEl?.addEventListener('click', (e) => {
if (e.target === overlayEl || e.target.closest('[data-dismiss]')) closeOverlay();
});
document.addEventListener('keydown', (e) => { if (e.key === 'Escape') closeOverlay(); });
/* ===========================================================
10) EVENTS + INIT (+ Random)
PURPOSE: Wire up the dropdowns (change → re-render), and then kick off the first fetch.
Keep the visual "is-selected" pill style in sync with whether a value is chosen.
=========================================================== */
// Tiny event helper + debounce
const on = (el, evts, fn) => evts.split(' ').forEach((e) => el.addEventListener(e, fn));
const debounce = (fn, ms = 250) => {
let t;
return (...args) => {
clearTimeout(t);
t = setTimeout(() => fn(...args), ms);
};
};
// Wire selects (cuisine, diet, sorters)
['cuisine', 'diet', 'sortTime', 'sortPop'].forEach((id) => {
const sel = $(id);
if (!sel) return;
const rerender = () => render(getVisibleRecipes(), 'filters');
on(sel, 'change input', () => {
sel.classList.toggle('is-selected', !!sel.value);
rerender();
});
// Initialize selected state on load
sel.classList.toggle('is-selected', !!sel.value);
});
// Search (debounced to avoid re-render on every keystroke)
const qEl = $('q');
if (qEl) on(qEl, 'input', debounce(() => render(getVisibleRecipes(), 'filters'), 250));
// Random recipe button
$('btnRandom')?.addEventListener('click', (e) => {
e.preventDefault();
if (!RECIPES.length) {
$('status').textContent = 'No recipes loaded yet — try fetching first.';
return;
}
setBusy(true);
const pick = RECIPES[Math.floor(Math.random() * RECIPES.length)];
render([pick], 'random');
});
// ENTRY POINT – Start by fetching recipes (cache → API → stale/backup fallback)
fetchRecipes(24);