Add prompt reviews and rating sorting

This commit is contained in:
yogarathuvar committed 2026-06-01 22:10:38 +02:00
1 parent c4a802b458
commit 37d92fd6a7
7 files changed
+296 -14

No files matched your search

+168 -2
View File
@@ -232,6 +232,27 @@
<div class="rating-section" id="rating-section">
<div class="rating-stars" id="rating-display"></div>
</div>
<!-- Reviews -->
<div class="reviews-section" id="reviews-section">
<h2>REVIEWS</h2>
<div class="review-form" id="review-form">
<h3>Your review</h3>
<div class="review-star-input" id="review-star-input" aria-label="Select rating">
<button type="button" data-rating="1">☆</button>
<button type="button" data-rating="2">☆</button>
<button type="button" data-rating="3">☆</button>
<button type="button" data-rating="4">☆</button>
<button type="button" data-rating="5">☆</button>
</div>
<textarea id="review-comment" maxlength="200" rows="3" placeholder="Write a short comment..."></textarea>
<button type="button" id="submit-review-btn">Submit Review</button>
<p id="review-message"></p>
</div>
<div class="reviews-list" id="reviews-list">
<p style="color:#94a3b8;">Loading reviews...</p>
</div>
</div>
</div>
</div>
</main>
@@ -256,6 +277,7 @@
const params = new URLSearchParams(location.search);
const id = params.get("id");
let selectedReviewRating = 0;
if (!id) {
showError("No prompt ID provided", "Add ?id=... to the URL.");
@@ -313,6 +335,8 @@
<span style="margin-left:12px;"><i class="bi ${p.isSaved ? 'bi-bookmark-fill' : 'bi-bookmark'}" style="color:#f59e0b;"></i> ${p.saveCount || 0} saves</span>`;
renderExamples(p);
renderOwnerActions(p);
setupReviewSection(p);
loadReviews(p.id);
// Tier badge
const badge = document.getElementById("tier-badge");
@@ -330,11 +354,11 @@
document.getElementById("rating-display").innerHTML =
`<span style="color:#f59e0b;font-size:1.1rem;">${"★".repeat(stars)}${"☆".repeat(5 - stars)}</span>
<span style="margin-left:8px;font-weight:600;">${p.averageRating.toFixed(1)}</span>
<span style="color:#94a3b8;font-size:0.85rem;margin-left:4px;">/ 5.0</span>`;
<span style="color:#94a3b8;font-size:0.85rem;margin-left:4px;">/ 5.0 (${p.reviewCount || 0} ${(p.reviewCount || 0) === 1 ? "review" : "reviews"})</span>`;
document.getElementById("prompt-rating-stat").innerHTML =
`<i class="bi ${p.isLiked ? 'bi-heart-fill' : 'bi-heart'}" style="color:#ef4444;"></i> ${p.likeCount || 0} likes
<span style="margin-left:12px;"><i class="bi ${p.isSaved ? 'bi-bookmark-fill' : 'bi-bookmark'}" style="color:#f59e0b;"></i> ${p.saveCount || 0} saves</span>
<span style="margin-left:12px;"><i class="bi bi-star-fill" style="color:#f59e0b;"></i> ${p.averageRating.toFixed(1)} rating</span>`;
<span style="margin-left:12px;"><i class="bi bi-star-fill" style="color:#f59e0b;"></i> ${p.averageRating.toFixed(1)} (${p.reviewCount || 0})</span>`;
} else {
document.getElementById("rating-display").innerHTML =
'<span style="color:#94a3b8;font-size:0.9rem;">No ratings yet</span>';
@@ -357,6 +381,148 @@
document.getElementById("detail-loading").style.display = "none";
document.getElementById("detail-body").style.display = "block";
scrollToHashSection();
}
function scrollToHashSection() {
if (!location.hash) return;
const target = document.querySelector(location.hash);
if (!target) return;
requestAnimationFrame(() => {
target.scrollIntoView({ behavior: "smooth", block: "start" });
});
}
function setReviewRating(rating) {
selectedReviewRating = rating;
document.querySelectorAll("#review-star-input button").forEach((button) => {
const value = Number(button.dataset.rating);
button.textContent = value <= rating ? "★" : "☆";
});
}
function escapeHtml(value) {
return String(value ?? "")
.replaceAll("&", "&amp;")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;")
.replaceAll('"', "&quot;")
.replaceAll("'", "&#039;");
}
async function setupReviewSection(prompt) {
const form = document.getElementById("review-form");
const message = document.getElementById("review-message");
const submitBtn = document.getElementById("submit-review-btn");
selectedReviewRating = 0;
setReviewRating(0);
document.getElementById("review-star-input").style.display = "flex";
document.getElementById("review-comment").style.display = "block";
submitBtn.style.display = "inline-block";
document.getElementById("review-comment").value = "";
message.textContent = "";
form.style.display = "block";
if (!prompt.content) {
document.getElementById("review-star-input").style.display = "none";
document.getElementById("review-comment").style.display = "none";
submitBtn.style.display = "none";
message.textContent = "Unlock this prompt before writing a review.";
return;
}
try {
const response = await fetch("/api/v1/auth/me", {
credentials: "same-origin",
});
if (!response.ok) return;
const user = await response.json();
if (user.id === prompt.creatorId) {
document.getElementById("review-star-input").style.display = "none";
document.getElementById("review-comment").style.display = "none";
submitBtn.style.display = "none";
message.textContent = "You cannot review your own prompt.";
return;
}
} catch {
// Keep the review form visible; the API will reject unauthenticated users.
}
document.querySelectorAll("#review-star-input button").forEach((button) => {
button.onclick = () => setReviewRating(Number(button.dataset.rating));
});
submitBtn.onclick = async () => {
if (!selectedReviewRating) {
message.textContent = "Please select a star rating.";
return;
}
submitBtn.disabled = true;
message.textContent = "Saving review...";
const response = await fetch(`/api/v1/prompts/${prompt.id}/reviews`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
credentials: "same-origin",
body: JSON.stringify({
rating: selectedReviewRating,
comment: document.getElementById("review-comment").value.trim() || null,
}),
});
if (response.status === 401) {
location.href = "/login";
return;
}
submitBtn.disabled = false;
if (!response.ok) {
message.textContent = await response.text();
return;
}
message.textContent = "Review saved.";
await loadPrompt(prompt.id);
};
}
async function loadReviews(promptId) {
const list = document.getElementById("reviews-list");
try {
const response = await fetch(`/api/v1/prompts/${promptId}/reviews`, {
credentials: "same-origin",
});
if (response.status === 401) {
location.href = "/login";
return;
}
if (!response.ok) throw new Error(`Server error ${response.status}`);
const reviews = await response.json();
if (reviews.length === 0) {
list.innerHTML = '<p style="color:#94a3b8;">No reviews yet.</p>';
return;
}
list.innerHTML = reviews.map((review) => {
const stars = "★".repeat(review.rating) + "☆".repeat(5 - review.rating);
return `
<article class="review-card">
<div class="review-card-header">
<span class="review-card-user">@${escapeHtml(review.creatorName)}</span>
<span class="review-card-stars">${stars}</span>
</div>
<p class="review-card-comment">${escapeHtml(review.comment || "No comment.")}</p>
</article>`;
}).join("");
} catch (error) {
list.innerHTML = `<p style="color:#ef4444;">${error.message}</p>`;
}
}
async function renderOwnerActions(p) {