@@ -140,14 +140,32 @@ function setRating(rating, starContainer, recipe) {
140
140
stars [ i ] . style . color = 'black' ;
141
141
}
142
142
}
143
- if ( recipe && recipe . id ) {
144
- giveRating ( rating , recipe . id ) ;
143
+ if ( recipe && recipe . recipe . id ) {
144
+ giveRating ( rating , recipe . recipe . id ) ;
145
145
} else {
146
146
console . error ( 'Recipe ID not found for the given recipe:' , recipe ) ;
147
147
// Handle the case where the recipe ID is not available
148
148
}
149
149
}
150
150
151
+ function setLike ( isClicked , heartContainer , recipe ) {
152
+ if ( isClicked ) {
153
+ heartContainer . textContent = '♥' ;
154
+ heartContainer . style . color = 'red' ;
155
+ } else {
156
+ heartContainer . textContent = '♥' ;
157
+ heartContainer . style . color = 'black' ;
158
+ }
159
+
160
+ if ( recipe && recipe . recipe . id ) {
161
+ giveLike ( recipe . recipe . id ) ;
162
+ } else {
163
+ console . error ( 'Recipe ID not found for the given recipe:' , recipe ) ;
164
+ // Handle the case where the recipe ID is not available
165
+ }
166
+ }
167
+
168
+
151
169
function giveRating ( newRating , recipeId ) {
152
170
const url = `https://recipiebeckend.azurewebsites.net/user/give-rate?recipeId=${ recipeId } &rate=${ newRating } ` ;
153
171
const JWTAccessToken = sessionStorage . getItem ( 'accessToken' ) ;
@@ -174,6 +192,32 @@ function setRating(rating, starContainer, recipe) {
174
192
. catch ( error => console . error ( 'Error updating rating:' , error ) ) ;
175
193
}
176
194
195
+ function giveLike ( recipeId ) {
196
+ const url = `https://recipiebeckend.azurewebsites.net/user/give-like?recipeId=${ recipeId } ` ;
197
+ const JWTAccessToken = sessionStorage . getItem ( 'accessToken' ) ;
198
+
199
+ const headers = {
200
+ 'Content-Type' : 'application/json' ,
201
+ 'Authorization' : JWTAccessToken ,
202
+ } ;
203
+
204
+ fetch ( url , {
205
+ method : 'POST' ,
206
+ headers : headers ,
207
+ } )
208
+ . then ( response => {
209
+ if ( ! response . ok ) {
210
+ throw new Error ( `Network response was not ok (status: ${ response . status } )` ) ;
211
+ }
212
+ return response . text ( ) ; // or response.blob(), response.json(), depending on the expected response type
213
+ } )
214
+ . then ( data => {
215
+ console . log ( 'Response Data:' , data ) ;
216
+ // Continue handling the response as needed
217
+ } )
218
+ . catch ( error => console . error ( 'Error updating rating:' , error ) ) ;
219
+ }
220
+
177
221
const days = [ 'monday' , 'tuesday' , 'wednesday' , 'thursday' , 'friday' , 'saturday' , 'sunday' ] ;
178
222
let selectedDay = null ;
179
223
@@ -310,20 +354,9 @@ const createRecipeElement = async (recipe) => {
310
354
311
355
const getCustomData = await getSelectedCustomDataOfDashboard ( recipeIndex ) ;
312
356
const rate = getCustomData . rate ;
313
- // const recipeId = getStarAndHeartInfo.recipe.id;
314
- // const getRateInfo = getStarAndHeartInfo[0].rateResponseList[0].rate;
315
- // const getRatedRecipeId = getStarAndHeartInfo[0].rateResponseList[1].recipeId;
316
357
317
358
console . log ( "Star Info: " , rate ) ;
318
- // const isRatedByUser = recipe.id === recipeId;
319
-
320
- // if (isRatedByUser) {
321
- // console.log("Recipe is rated by the user");
322
- // } else {
323
- // console.log("Recipe is not rated by the user");
324
- // }
325
- // const userRating = getUserRating(recipe.id);
326
- // Create stars
359
+
327
360
for ( let i = 1 ; i <= 5 ; i ++ ) {
328
361
const star = document . createElement ( 'span' ) ;
329
362
star . classList . add ( 'star' ) ;
@@ -359,13 +392,20 @@ const createRecipeElement = async (recipe) => {
359
392
recipeDiv . appendChild ( starContainer ) ;
360
393
361
394
// Create heart
362
- const heart = document . createElement ( 'span' ) ;
363
- heart . classList . add ( 'favorite-heart' ) ;
364
- heart . src = 'Gifs/heart.gif' ; // Replace with the actual path to your animated GIF
365
- heart . alt = 'Animated Heart' ;
366
- heart . onclick = ( ) => toggleFavorite ( heart ) ;
367
- heart . textContent = '♥' ;
368
- heart . onclick = ( ) => toggleFavorite ( heart ) ;
395
+ const heartContainer = document . createElement ( 'span' ) ;
396
+ // heart.src = 'Gifs/heart.gif';
397
+ // heart.alt = 'Animated Heart';
398
+ // heart.onclick = () => toggleFavorite(heart);
399
+ heartContainer . textContent = '♥' ;
400
+ heartContainer . classList . add ( 'favorite-heart' ) ;
401
+ if ( getCustomData . isLiked ) {
402
+ heartContainer . style . color = 'red' ;
403
+ }
404
+ // heart.onclick = () => toggleFavorite(heart);
405
+
406
+ const clickHandler = ( ) => setLike ( true , heartContainer , recipe ) ;
407
+ heartContainer . onclick = clickHandler ;
408
+
369
409
370
410
// Title
371
411
const titleDiv = document . createElement ( 'div' ) ;
@@ -376,7 +416,7 @@ const createRecipeElement = async (recipe) => {
376
416
recipeDiv . appendChild ( imgDiv ) ;
377
417
recipeDiv . appendChild ( titleDiv ) ;
378
418
recipeDiv . appendChild ( starContainer ) ;
379
- recipeDiv . appendChild ( heart ) ;
419
+ recipeDiv . appendChild ( heartContainer ) ;
380
420
381
421
return recipeDiv ;
382
422
} ;
0 commit comments