Skip to content

Commit 55ec4ca

Browse files
committed
fav tab html navigation
1 parent 33f74d3 commit 55ec4ca

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

scripts/userFavTab.js

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,48 +31,49 @@ async function displayFavRecipes() {
3131
const cardDiv = document.createElement('div');
3232
cardDiv.classList.add('card');
3333
const truncatedDescription = truncateDescription(item.description);
34-
const cardContent = `
35-
<img src="${item.photoPath ? item.photoPath : (item.userRecipePhoto ? item.userRecipePhoto : 'default/path/to/image.jpg')}" alt="${item.title}">
3634

35+
let isUserRecipe;
36+
if (item.userRecipePhoto) {
37+
isUserRecipe = true;
38+
} else {
39+
isUserRecipe = false;
40+
}
41+
42+
const cardContent = `
43+
<img src="${isUserRecipe ? item.userRecipePhoto : (item.photoPath || 'default/path/to/image.jpg')}" alt="${item.title}">
3744
<div class="info">
3845
<h1>${item.title}</h1>
3946
<p>${truncatedDescription}</p>
40-
<button class="read-more-btn" ">Read More</button>
47+
<button class="read-more-btn" data-id="${item.id}" data-is-user-recipe="${isUserRecipe}">Read More</button>
4148
</div>
4249
`;
4350
cardDiv.innerHTML = cardContent;
4451
cardsContainer.appendChild(cardDiv);
4552
});
46-
47-
const openRecipeDetailPage = (id) => {
48-
const recipeDetailURL = `recipeDetail.html?id=${id}`;
49-
// Perform any additional actions before navigating, if needed
50-
// For example, you might want to validate the id or perform some asynchronous tasks
51-
window.location.href = recipeDetailURL;
52-
};
53-
54-
// Adding event listeners to the "Read More" buttons
55-
document.querySelectorAll('.read-more-btn').forEach(button => {
56-
button.addEventListener('click', () => {
57-
const recipeId = button.getAttribute('data-id');
58-
openRecipeDetailPage(recipeId);
53+
54+
55+
document.querySelectorAll('.read-more-btn').forEach(button => {
56+
button.addEventListener('click', (event) => {
57+
const id = event.target.getAttribute('data-id');
58+
const isUserRecipe = event.target.getAttribute('data-is-user-recipe') === 'true';
59+
goRecipeDetailPage(id, isUserRecipe);
5960
});
60-
});
61-
62-
// Assuming you have an element with class "link" for the event listener
63-
const link = document.querySelector('.link');
64-
// Adding event listener to the link
65-
link.addEventListener('click', () => {
66-
// Here you should get the ID from your data, in this example let's assume you have it as "recipeId"
67-
const recipeId = recipe.recipe.id; // Adjust this according to your data structure
68-
openRecipeDetailPage(recipeId);
69-
});
70-
61+
});
7162
} catch (error) {
7263
console.error('Error displaying favorite recipes:', error);
7364
}
7465
}
7566

67+
function goRecipeDetailPage(id, isUserRecipe) {
68+
const recipeDetailURL = isUserRecipe
69+
? `userRecipeDetail.html?id=${id}`
70+
: `recipeDetail.html?id=${id}`;
71+
window.location.href = recipeDetailURL;
72+
}
73+
74+
75+
76+
7677
function truncateDescription(description) {
7778
const maxLength = 100; // Maksimum karakter sayısı
7879
if (description.length > maxLength) {
@@ -96,4 +97,5 @@ function setupBackButton() {
9697
// Call the function to generate user recipe boxes when the page loads
9798
window.onload = function () {
9899
setupBackButton(); // Call setupBackButton after generateUserRecipeBoxes
99-
};
100+
};
101+

0 commit comments

Comments
 (0)