-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecipe-detail.html
More file actions
212 lines (180 loc) · 7.91 KB
/
Copy pathrecipe-detail.html
File metadata and controls
212 lines (180 loc) · 7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DishDelight - Recipe Details</title>
<link rel="stylesheet" href="style.css">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<style>
.review-section {
margin-top: 3rem;
padding-top: 2rem;
border-top: 1px solid #eee;
}
.review-card {
background: #f9f9f9;
padding: 1rem;
border-radius: 8px;
margin-bottom: 1rem;
}
.star-rating {
display: flex;
flex-direction: row-reverse;
justify-content: flex-end;
gap: 0.5rem;
}
.star-rating input {
display: none;
}
.star-rating label {
cursor: pointer;
font-size: 1.5rem;
color: #ddd;
}
.star-rating input:checked~label,
.star-rating label:hover,
.star-rating label:hover~label {
color: #f1c40f;
}
</style>
</head>
<body>
<header></header>
<div class="container">
<div id="recipe-detail" class="recipe-detail-container">
<p class="text-center">Loading recipe details...</p>
</div>
<div class="review-section">
<h3>Reviews</h3>
<div id="review-form-container" class="mb-1">
<!-- Review form will be injected here if logged in -->
</div>
<div id="reviews-list">
<!-- Reviews will be loaded here -->
</div>
</div>
</div>
<script src="script.js"></script>
<script>
const urlParams = new URLSearchParams(window.location.search);
const recipeId = urlParams.get('id');
async function loadRecipeDetail() {
if (!recipeId) {
document.getElementById('recipe-detail').innerHTML = '<p class="text-center">Invalid recipe ID.</p>';
return;
}
const recipe = await apiFetch(`/api/recipes/${recipeId}`);
if (!recipe || recipe.message === 'Recipe not found') {
document.getElementById('recipe-detail').innerHTML = '<p class="text-center">Recipe not found.</p>';
return;
}
const container = document.getElementById('recipe-detail');
const user = JSON.parse(localStorage.getItem('user'));
let editButton = '';
if (user && (user.id === recipe.userId || user.role === 'admin')) {
editButton = `<a href="/add-recipe.html?id=${recipe.id}" class="btn btn-primary" style="margin-right: 1rem;">Edit Recipe</a>`;
}
container.innerHTML = `
<div class="recipe-header text-center">
<h1 class="mb-1">${recipe.title}</h1>
<p class="text-muted">Created by: ${recipe.userName || 'Unknown'}</p>
</div>
<div class="recipe-image-container mb-1">
<img src="${recipe.image || 'https://via.placeholder.com/800x400'}" alt="${recipe.title}" style="width: 100%; max-height: 400px; object-fit: cover; border-radius: 10px;">
</div>
<div class="recipe-content">
<div class="mb-1">
<h3>Description</h3>
<p>${recipe.description}</p>
</div>
<div class="mb-1">
<h3>Ingredients</h3>
<p style="white-space: pre-line;">${recipe.ingredients}</p>
</div>
<div class="mb-1">
<h3>Instructions</h3>
<p style="white-space: pre-line;">${recipe.instructions}</p>
</div>
</div>
<div class="text-center mt-1">
${editButton}
<a href="/recipe-list.html" class="btn btn-outline">Back to Recipes</a>
</div>
`;
loadReviews();
setupReviewForm();
}
async function loadReviews() {
const reviews = await apiFetch(`/api/recipes/${recipeId}/reviews`);
const container = document.getElementById('reviews-list');
if (!reviews || reviews.length === 0) {
container.innerHTML = '<p>No reviews yet. Be the first to review!</p>';
return;
}
container.innerHTML = reviews.map(review => `
<div class="review-card">
<div style="display: flex; justify-content: space-between; margin-bottom: 0.5rem;">
<strong>${review.userName}</strong>
<span style="color: #f1c40f;">${'★'.repeat(review.rating)}${'☆'.repeat(5 - review.rating)}</span>
</div>
<p>${review.comment}</p>
<small style="color: #999;">${new Date(review.createdAt).toLocaleDateString()}</small>
</div>
`).join('');
}
function setupReviewForm() {
const user = JSON.parse(localStorage.getItem('user'));
const container = document.getElementById('review-form-container');
if (!user) {
container.innerHTML = '<p><a href="/signin.html" style="color: var(--primary-color);">Sign in</a> to leave a review.</p>';
return;
}
container.innerHTML = `
<form id="review-form" style="background: white; padding: 1.5rem; border-radius: 8px; box-shadow: 0 2px 5px rgba(0,0,0,0.05);">
<h4>Leave a Review</h4>
<div class="form-group">
<label>Rating</label>
<div class="star-rating">
<input type="radio" id="star5" name="rating" value="5" required><label for="star5">★</label>
<input type="radio" id="star4" name="rating" value="4"><label for="star4">★</label>
<input type="radio" id="star3" name="rating" value="3"><label for="star3">★</label>
<input type="radio" id="star2" name="rating" value="2"><label for="star2">★</label>
<input type="radio" id="star1" name="rating" value="1"><label for="star1">★</label>
</div>
</div>
<div class="form-group">
<label for="comment">Comment</label>
<textarea id="comment" name="comment" rows="3" ></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit Review</button>
</form>
`;
document.getElementById('review-form').addEventListener('submit', async (e) => {
e.preventDefault();
const formData = new FormData(e.target);
const rating = formData.get('rating');
const comment = formData.get('comment');
const result = await apiFetch(`/api/recipes/${recipeId}/reviews`, {
method: 'POST',
body: JSON.stringify({
userId: user.id,
userName: user.name,
rating: parseInt(rating),
comment
})
});
if (result) {
alert('Review submitted successfully!');
loadReviews();
e.target.reset();
}
});
}
loadRecipeDetail();
</script>
<footer>
<p>© 2025 DishDelight. All rights reserved.</p>
</footer>
</body>
</html>