11/******************************************************************************************************************** 
2-  * Implementation of  the listView Module at ./src/js/views/listView.js . 
2+  * We will now implement  the controller for the shopping list in this controller module named as controlList() below . 
33 *  
4+  * controlList() is only called when the "Add to Shopping List" Button is clicked in the .recipe class. 
5+  *  
6+  * Therefore, we handle that using event delegation in .recipe class' element again. 
7+  *  
8+  * We also handle click event when the user wants to update/remove the item from the shopping list. 
49 */ 
510// Import Data Models 
611import  Search  from  './models/Search' ; 
@@ -10,6 +15,7 @@ import List from './models/List';
1015// Import Front-End Views 
1116import  *  as  searchView  from  './views/searchView' ; 
1217import  *  as  recipeView  from  './views/recipeView' ; 
18+ import  *  as  listView  from  './views/listView' ; 
1319
1420// Import Common Code Base 
1521import  {  elements ,  renderLoader ,  clearLoader ,  elementStrings  }  from  './views/base' ;  
@@ -22,6 +28,7 @@ import { elements, renderLoader, clearLoader, elementStrings } from './views/bas
2228 * - Liked Recipes -- Stored persistently. We'll know about JS local storage (i.e., persistent data) later. 
2329 */ 
2430const  state  =  { } ; 
31+ window . state  =  state ;  // TESTING 
2532
2633// SEARCH CONTROLLER 
2734const  controlSearch  =  async  ( )  =>  { 
@@ -144,6 +151,48 @@ const controlRecipe = async () => {
144151[ 'hashchange' ,  'load' ] . forEach ( event  =>  window . addEventListener ( event ,  controlRecipe ) ) ; 
145152
146153
154+ // SHOPPING LIST CONTROLLER 
155+ 156+ // TEST CODE 
157+ // window.l = new List(); 
158+ 159+ const  controlList  =  ( )  =>  { 
160+  // Create a new shopping list, IF there is none yet 
161+  if  ( ! state . list ) 
162+  state . list  =  new  List ( ) ; 
163+ 164+  // Add each ingredient to the list and the UI 
165+  state . recipe . ingredients . forEach ( el  =>  { 
166+  const  item  =  state . list . addItem ( el . count ,  el . unit ,  el . ingredient ) ; 
167+  listView . renderItem ( item ) ; 
168+  } ) ; 
169+ } ; 
170+ 171+ 172+ // Handling update/delete list item events 
173+ elements . shopping . addEventListener ( 'click' ,  event  =>  { 
174+  // get the ID of the item we want to delete/update 
175+  const  id  =  event . target . closest ( `.${ elementStrings . shoppingItem }  ` ) . dataset . itemid ; 
176+ 177+  // handle the delete event 
178+  if  ( event . target . matches ( `.${ elementStrings . shoppingDelete }  , .${ elementStrings . shoppingDelete }   *` ) )  { 
179+  // Delete from state 
180+  state . list . deleteItem ( id ) ; 
181+ 182+  // Remove the item from the UI 
183+  listView . deleteItem ( id ) ; 
184+  } 
185+ 186+  // handle the updation of the items in the shopping list 
187+  else  if  ( event . target . matches ( `.${ elementStrings . shoppingCount }  ` ) )  { 
188+  // Read the data from the UI and update it in our state 
189+  const  val  =  parseFloat ( event . target . value ,  10 ) ; 
190+  if  ( val  >=  0 ) 
191+  state . list . updateCount ( id ,  val ) ; 
192+  } 
193+ } ) ; 
194+ 195+ 147196// Handling recipe button clicks 
148197elements . recipe . addEventListener ( 'click' ,  event  =>  { 
149198 // This time we cannot use the closest() method because this time we have two buttons that can be clicked. 
@@ -154,19 +203,22 @@ elements.recipe.addEventListener('click', event => {
154203
155204 // Decrease Servings Button is clicked 
156205 if  ( event . target . matches ( `.${ elementStrings . servingsDecreaseButton }  , .${ elementStrings . servingsDecreaseButton }   *` ) 
157-  &&  state . recipe . servings  >  1 ) 
206+  &&  state . recipe . servings  >  1 ) { 
158207 state . recipe . updateServings ( 'dec' ) ; 
208+  recipeView . updateServingsAndIngredients ( state . recipe ) ; 
209+  } 
159210
160211 // Increase Servings Button is clicked 
161-  else  if  ( event . target . matches ( `.${ elementStrings . servingsIncreaseButton }  , .${ elementStrings . servingsIncreaseButton }   *` ) ) 
212+  else  if  ( event . target . matches ( `.${ elementStrings . servingsIncreaseButton }  , .${ elementStrings . servingsIncreaseButton }   *` ) ) { 
162213 state . recipe . updateServings ( 'inc' ) ; 
214+  recipeView . updateServingsAndIngredients ( state . recipe ) ; 
215+  } 
163216
164-  recipeView . updateServingsAndIngredients ( state . recipe ) ; 
217+  else  if  ( event . target . matches ( `.${ elementStrings . addToShoppingList }  , .${ elementStrings . addToShoppingList }   *` ) )  { 
218+  controlList ( ) ; 
219+  } 
220+ 165221 //console.log(state.recipe); // TESTING 
166222} ) ; 
167223
168224
169- // SHOPPING LIST CONTROLLER 
170- 171- // TEST CODE 
172- window . l  =  new  List ( ) ; 
0 commit comments