Skip to content

Commit 319c4b2

Browse files
committed
Added giveLike and related fetch function. Now hearts that are liked before comes red if user has logged in.
1 parent a5dbe9f commit 319c4b2

File tree

2 files changed

+63
-30
lines changed

2 files changed

+63
-30
lines changed

scripts/dashboard.js

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,32 @@ function setRating(rating, starContainer, recipe) {
140140
stars[i].style.color = 'black';
141141
}
142142
}
143-
if (recipe && recipe.id) {
144-
giveRating(rating, recipe.id);
143+
if (recipe && recipe.recipe.id) {
144+
giveRating(rating, recipe.recipe.id);
145145
} else {
146146
console.error('Recipe ID not found for the given recipe:', recipe);
147147
// Handle the case where the recipe ID is not available
148148
}
149149
}
150150

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+
151169
function giveRating(newRating, recipeId) {
152170
const url = `https://recipiebeckend.azurewebsites.net/user/give-rate?recipeId=${recipeId}&rate=${newRating}`;
153171
const JWTAccessToken = sessionStorage.getItem('accessToken');
@@ -174,6 +192,32 @@ function setRating(rating, starContainer, recipe) {
174192
.catch(error => console.error('Error updating rating:', error));
175193
}
176194

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+
177221
const days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
178222
let selectedDay = null;
179223

@@ -310,20 +354,9 @@ const createRecipeElement = async (recipe) => {
310354

311355
const getCustomData = await getSelectedCustomDataOfDashboard(recipeIndex);
312356
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;
316357

317358
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+
327360
for (let i = 1; i <= 5; i++) {
328361
const star = document.createElement('span');
329362
star.classList.add('star');
@@ -359,13 +392,20 @@ const createRecipeElement = async (recipe) => {
359392
recipeDiv.appendChild(starContainer);
360393

361394
// 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+
369409

370410
// Title
371411
const titleDiv = document.createElement('div');
@@ -376,7 +416,7 @@ const createRecipeElement = async (recipe) => {
376416
recipeDiv.appendChild(imgDiv);
377417
recipeDiv.appendChild(titleDiv);
378418
recipeDiv.appendChild(starContainer);
379-
recipeDiv.appendChild(heart);
419+
recipeDiv.appendChild(heartContainer);
380420

381421
return recipeDiv;
382422
};

styles/dashboard.css

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -485,22 +485,15 @@ a {
485485
color: rgb(33, 31, 34);
486486
}
487487

488-
.favorite-heart {
489-
/* Kalp simgesini büyütmek için */
490-
/* İstenilen boyuta göre */
491-
margin-left: 20px;
492488

493-
}
494489
.favorite-heart {
490+
margin-left: 20px;
495491
font-size: 24px;
496492
color: black;
497493

498494
cursor: pointer;
499495
}
500496

501-
.favorite-heart.favorited {
502-
color: red;
503-
}
504497
.recipe p {
505498
margin-bottom: 0;
506499
line-height: 1;

0 commit comments

Comments
 (0)