Skip to content

Commit 18abceb

Browse files
committed
feat: script to display the recipe
1 parent c0689f9 commit 18abceb

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
let recipes = {};
2+
3+
const toggleRecipe = (e) => {
4+
const drinkName = e.target.textContent.trim();
5+
const recipeDiv = e.currentTarget.querySelector('.recipe');
6+
7+
if (!recipeDiv) {
8+
const newRecipeDiv = document.createElement('div');
9+
newRecipeDiv.className = 'recipe';
10+
newRecipeDiv.textContent = recipes[drinkName] || 'Recipe not available..';
11+
e.currentTarget.appendChild(newRecipeDiv);
12+
} else {
13+
recipeDiv.style.display = recipeDiv.style.display === 'none' ? 'block' : 'none';
14+
}
15+
};
16+
17+
const initializeApp = () => {
18+
const li = document.querySelectorAll('.drink-list ul li');
19+
20+
li.forEach((item) => {
21+
item.addEventListener('click', toggleRecipe);
22+
});
23+
};
24+
25+
const loadRecipes = async () => {
26+
try {
27+
const response = await fetch('./json/recipes.json');
28+
recipes = await response.json();
29+
30+
initializeApp();
31+
} catch (error) {
32+
console.error('Failed to load recipes', error);
33+
}
34+
};
35+
36+
document.addEventListener('DOMContentLoaded', async () => {
37+
await loadRecipes();
38+
});

0 commit comments

Comments
 (0)