File tree 1 file changed +38
-0
lines changed
projects/project_01_browser/js
1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments