quickstarts/monolith-to-microservices/modular/templates/book_details.html (98 lines of code) (raw):
<!--
Copyright 2025 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ book.title }} - Details</title>
<link rel="stylesheet" href="{{ url_for('static', filename='styles_for_details.css') }}">
</head>
<body>
<a href="/" class="button">Back to Home</a>
<div class="book-details">
<div class="book-image-container">
<div class="image-placeholder"></div>
<img src="/images/{{ book.image_url }}" alt="{{ book.title }}" class="book-image" style="display: none;">
</div>
<div class="book-info">
<h1>{{ book.title }}</h1>
<p><strong>Author:</strong> {{ book.author }}</p>
<p><strong>Year:</strong> {{ book.year }}</p>
<p><strong>Description:</strong> {{ book.description }}</p>
<button id="toggleReviews" class="button" onclick="toggleReviews()">Show Reviews</button>
</div>
</div>
<div class="content-wrapper">
<div class="reviews-section">
<div id="reviews"></div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', (event) => {
const img = document.querySelector('.book-image');
const placeholder = document.querySelector('.image-placeholder');
function loadImage() {
img.style.display = 'none';
placeholder.style.display = 'block';
img.onload = function() {
img.style.display = 'block';
placeholder.style.display = 'none';
}
img.onerror = function() {
console.error('Failed to load image:', this.src);
placeholder.textContent = 'Image not found';
}
img.src = img.src + '?' + new Date().getTime();
}
loadImage();
setTimeout(() => {
if (img.style.display === 'none') {
loadImage();
}
}, 500);
});
function toggleReviews() {
const reviewsDiv = document.getElementById('reviews');
const toggleButton = document.getElementById('toggleReviews');
if (reviewsDiv.style.display === 'none' || reviewsDiv.style.display === '') {
fetch(`/book/{{ book.id }}/reviews`)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(reviews => {
if (reviews.length === 0) {
throw new Error('No reviews found');
}
const totalReviews = reviews.length;
const averageRating = (reviews.reduce((sum, review) => sum + review.rating, 0) / totalReviews).toFixed(1);
const reviewsHtml = reviews.map((review, index) => `
<div class="review">
<h3>Review #${index + 1}</h3>
<p><strong>Rating:</strong> ${review.rating}/5</p>
<p>${review.content}</p>
</div>
`).join('');
reviewsDiv.innerHTML = `
<h2>Reviews</h2>
<p><strong>Total Reviews:</strong> ${totalReviews}</p>
<p><strong>Average Rating:</strong> ${averageRating}/5</p>
${reviewsHtml}
`;
reviewsDiv.style.display = 'block';
toggleButton.textContent = 'Hide Reviews';
})
.catch(error => {
console.error('Error fetching reviews:', error);
reviewsDiv.innerHTML = 'Error loading reviews.';
reviewsDiv.style.display = 'block';
});
} else {
reviewsDiv.style.display = 'none';
toggleButton.textContent = 'Show Reviews';
}
}
</script>
</body>
</html>