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
110 lines (99 loc) · 3.88 KB
/
script.js
File metadata and controls
110 lines (99 loc) · 3.88 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
document.addEventListener('DOMContentLoaded', () => {
// Sample recipe data
const recipes = [
{
name: "Baked Chicken",
cuisineType: ["American"],
ingredients: [
"6 bone-in chicken breast halves, or 6 chicken thighs and wings, skin-on",
"1/2 teaspoon coarse salt",
"1/2 teaspoon Mrs. Dash seasoning",
"1/4 teaspoon freshly ground black pepper",
],
source: "Martha Stewart",
totalTime: 90,
url: "http://www.marthastewart.com/318981/baked-chicken",
},
{
name: "Spaghetti Carbonara",
cuisineType: ["Italian"],
ingredients: [
"1 pound spaghetti",
"2 large eggs",
"1/2 cup grated Parmesan cheese",
"4 slices bacon, diced",
"4 cloves garlic, minced",
"Salt and black pepper to taste",
],
source: "Allrecipes",
totalTime: 30,
url: "https://www.allrecipes.com/recipe/11973/spaghetti-carbonara/",
},
{
name: "Chicken Stir-Fry",
cuisineType: ["Chinese"],
ingredients: [
"1 pound boneless, skinless chicken breasts, cut into bite-sized pieces",
"1 tablespoon soy sauce",
"1 tablespoon cornstarch",
"1 tablespoon vegetable oil",
"1 cup broccoli florets",
"1 cup sliced carrots",
"1/2 cup sliced onion",
],
source: "The Woks of Life",
totalTime: 25,
url: "https://thewoksoflife.com/chicken-stir-fry/",
}
];
const searchForm = document.querySelector('form[role="search"]');
const searchInput = document.getElementById('searchInput');
const recipeContainer = document.getElementById('recipeDescription');
const sortOrder = document.getElementById('sortOrder');
let filteredRecipes = [...recipes];
const renderRecipes = (recipesToRender) => {
recipeContainer.innerHTML = '';
if (recipesToRender.length === 0) {
recipeContainer.innerHTML = '<p>No recipes found.</p>';
return;
}
const recipeList = document.createElement('ul');
recipeList.setAttribute('aria-label', 'Recipe search results');
recipesToRender.forEach(recipe => {
const listItem = document.createElement('li');
listItem.innerHTML = `
<h3><a href="${recipe.url}" target="_blank" rel="noopener noreferrer">${recipe.name} <span class="visually-hidden">(opens in a new tab)</span></a></h3>
<p>Cuisine: ${recipe.cuisineType.join(', ')}</p>
<p>Time: ${recipe.totalTime} minutes</p>
<p>Source: ${recipe.source}</p>
`;
recipeList.appendChild(listItem);
});
recipeContainer.appendChild(recipeList);
};
const sortAndRender = () => {
const sortBy = sortOrder.value;
const sorted = [...filteredRecipes].sort((a, b) => {
if (sortBy === 'asc') {
return a.totalTime - b.totalTime;
} else {
return b.totalTime - a.totalTime;
}
});
renderRecipes(sorted);
};
const handleSearch = (event) => {
event.preventDefault();
const searchTerm = searchInput.value.toLowerCase();
if (searchTerm) {
filteredRecipes = recipes.filter(recipe => recipe.name.toLowerCase().includes(searchTerm));
} else {
filteredRecipes = [...recipes];
}
sortAndRender();
};
searchForm.addEventListener('submit', handleSearch);
sortOrder.addEventListener('change', sortAndRender);
// Initial render on page load
sortAndRender();
});