@@ -31,48 +31,49 @@ async function displayFavRecipes() {
31
31
const cardDiv = document . createElement ( 'div' ) ;
32
32
cardDiv . classList . add ( 'card' ) ;
33
33
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 } ">
36
34
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 } ">
37
44
<div class="info">
38
45
<h1>${ item . title } </h1>
39
46
<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>
41
48
</div>
42
49
` ;
43
50
cardDiv . innerHTML = cardContent ;
44
51
cardsContainer . appendChild ( cardDiv ) ;
45
52
} ) ;
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 ) ;
59
60
} ) ;
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
+ } ) ;
71
62
} catch ( error ) {
72
63
console . error ( 'Error displaying favorite recipes:' , error ) ;
73
64
}
74
65
}
75
66
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
+
76
77
function truncateDescription ( description ) {
77
78
const maxLength = 100 ; // Maksimum karakter sayısı
78
79
if ( description . length > maxLength ) {
@@ -96,4 +97,5 @@ function setupBackButton() {
96
97
// Call the function to generate user recipe boxes when the page loads
97
98
window . onload = function ( ) {
98
99
setupBackButton ( ) ; // Call setupBackButton after generateUserRecipeBoxes
99
- } ;
100
+ } ;
101
+
0 commit comments