diff --git a/templates/dashboard.templ b/templates/dashboard.templ index 3b3f5af..5a7214e 100644 --- a/templates/dashboard.templ +++ b/templates/dashboard.templ @@ -129,21 +129,80 @@ templ Dashboard(user User) { }); } - function renderEbooks(ebooks) { + async function renderEbooks(ebooks) { const container = document.getElementById('ebooks-container'); container.innerHTML = ''; - ebooks.forEach(ebook => { + for (const ebook of ebooks) { const card = document.createElement('div'); card.className = 'card p-4 rounded-lg border hover:shadow-lg transition-shadow'; card.style.cssText = 'background-color: var(--bg-secondary); border-color: var(--border);'; const coverUrl = ebook.cover_image_path ? ebook.cover_image_path : '/static/placeholder-book.svg'; + + // Get user's rating for this ebook + const rating = await getEbookRating(ebook.id); + const ratingStars = renderRatingStars(ebook.id, rating); - card.innerHTML = '
Cover

' + ebook.title + '

' + (ebook.author ? '

by ' + ebook.author + '

' : '') + '
'; + card.innerHTML = '
Cover

' + ebook.title + '

' + (ebook.author ? '

by ' + ebook.author + '

' : '') + '
' + ratingStars + '
'; container.appendChild(card); - }); + } + } + + async function getEbookRating(ebookId) { + try { + const response = await fetch(`/api/ebooks/${ebookId}/rating`, { + headers: { + 'Authorization': 'Bearer ' + localStorage.getItem('token'), + 'Content-Type': 'application/json' + } + }); + + if (response.ok) { + const data = await response.json(); + return data.rating || 0; + } + return 0; + } catch (error) { + console.error('Error fetching rating:', error); + return 0; + } + } + + function renderRatingStars(ebookId, currentRating) { + let stars = ''; + for (let i = 1; i <= 5; i++) { + const filled = i <= currentRating; + const starClass = filled ? 'text-yellow-400' : 'text-gray-400'; + stars += `${filled ? '★' : '☆'}`; + } + return stars; + } + + async function setRating(ebookId, rating) { + try { + const response = await fetch(`/api/ebooks/${ebookId}/rating`, { + method: 'POST', + headers: { + 'Authorization': 'Bearer ' + localStorage.getItem('token'), + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ rating: rating }) + }); + + if (response.ok) { + const data = await response.json(); + const ratingContainer = document.getElementById(`rating-${ebookId}`); + if (ratingContainer) { + ratingContainer.innerHTML = renderRatingStars(ebookId, data.rating); + } + } else { + console.error('Error setting rating:', await response.text()); + } + } catch (error) { + console.error('Error setting rating:', error); + } } function logout() {