11/********************************************************************************************************************
2- * We make the controller for the likes in here. The event for liking an item happens inside the
3- * .recipe class.
4- * Therefore, we handle the event again at the .recipe class, where we only catch the event that pertains to the
5- * 'click' event occurring on the like button, which is the button with .recipe__love class under the
6- * .recipe class generated dynamically, and this is the reason we use event delegation.
2+ * Now we will handle the UI for the likes. Whenever the user clicks on the love button for a recipe,
3+ * the love button has to be shown as it is a clicked love button. For that, we add logic inside the recipeView
4+ * Module found at ./src/js/views/recipeView.js.
5+ *
6+ * Also, the love button should be persistent, it means that even if we reload the page, the recipe should be liked
7+ * and that love button should show that the recipe was liked. We also implement that functionality
8+ * by changing the recipeView module where we modify the renderRecipe() method. Note that the recipe won't be rendered
9+ * when we reload and that's because state.likes object is not available at the time of the call of renderRecipe()
10+ * inside the controlRecipe() controller. For that reason, we simply use a global state.likes object for testing
11+ * purposes. We can see that global state.likes object on top of the actual Likes Controller which is controlLikes()
12+ * controller method.
13+ *
14+ * Also, we don't want to show the heart icon at the top right of the webapp if there are no recipes that are liked
15+ * by the user yet. Therefore, we will implement the functionality to hide the heart icon at the top right in the
16+ * likesView Module.
17+ *
18+ * As the final step, we will render the likes into the heart icon's list when we hover over it.
19+ * Implementation of the method related to rendering the likes can be found in the likesView Module.
720 */
821
922// Import Data Models
@@ -16,6 +29,7 @@ import Likes from './models/Likes';
1629import * as searchView from './views/searchView' ;
1730import * as recipeView from './views/recipeView' ;
1831import * as listView from './views/listView' ;
32+ import * as likesView from './views/likesView' ;
1933
2034// Import Common Code Base
2135import { elements , renderLoader , clearLoader , elementStrings } from './views/base' ;
@@ -130,7 +144,10 @@ const controlRecipe = async () => {
130144
131145 // Render Recipe -- console.log(state.recipe);
132146 clearLoader ( ) ;
133- recipeView . renderRecipe ( state . recipe ) ;
147+ recipeView . renderRecipe (
148+ state . recipe ,
149+ state . likes . isLikedItem ( id )
150+ ) ;
134151
135152 } catch ( error ) {
136153 clearLoader ( ) ;
@@ -193,9 +210,13 @@ elements.shopping.addEventListener('click', event => {
193210} ) ;
194211
195212
196- // LIKE CONTROLLER
213+
214+ // LIKES CONTROLLER
215+ state . likes = new Likes ( ) ; // TESTING
216+ likesView . toggleLikeMenu ( state . likes . getNumberOfLikedItems ( ) ) ; // TESTING
217+
197218const controlLike = ( ) => {
198- if ( ! state . likes ) state . likes = new Likes ( ) ;
219+ // if (!state.likes) state.likes = new Likes();
199220 const currentID = state . recipe . id ;
200221
201222 // If a recipe is liked, then we have to include it in state.likes map, otherwise we don't.
@@ -207,23 +228,27 @@ const controlLike = () => {
207228 const newLike = state . likes . addLikedItem ( currentID , state . recipe . title , state . recipe . author , state . recipe . img ) ;
208229
209230 // Toggle the love button
210-
231+ likesView . toggleLikeBtn ( true ) ;
211232
212233 // Add like to the UI, i.e., add the liked recipe to the liked list
213-
214- console . log ( state . likes ) ; // TESTING
234+ likesView . renderLike ( newLike ) ;
235+ // console.log(state.likes); // TESTING
215236
216237 } else { // when the current recipe is liked
217238 // Remove like from the state
218239 state . likes . deleteLikedItem ( currentID ) ;
219240
220241 // Toggle the love button
242+ likesView . toggleLikeBtn ( false ) ;
221243
222244 // Remove like from the UI, i.e. from the liked list
223-
224-
225- console . log ( state . likes ) ; // TESTING
245+ likesView . deleteLike ( currentID ) ;
246+ // console.log(state.likes); // TESTING
226247 }
248+
249+ likesView . toggleLikeMenu (
250+ state . likes . getNumberOfLikedItems ( )
251+ ) ;
227252} ;
228253
229254
0 commit comments