| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <style>
- .comment-form {
- background-color: white;
- padding: 30px;
- border-radius: 10px;
- box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
- margin-bottom: 40px;
- }
- .form-group {
- margin-bottom: 20px;
- }
- .form-group label {
- display: block;
- margin-bottom: 8px;
- font-weight: 600;
- }
- .form-group input,
- .form-group textarea {
- width: 100%;
- padding: 12px;
- border: 1px solid #ddd;
- border-radius: 6px;
- font-size: 1rem;
- transition: border-color 0.3s ease;
- }
- .form-group input:focus,
- .form-group textarea:focus {
- outline: none;
- border-color: var(--primary-color);
- }
- .submit-btn {
- background-color: var(--primary-color);
- color: white;
- padding: 12px 30px;
- border: none;
- border-radius: 30px;
- cursor: pointer;
- font-weight: 600;
- transition: all 0.3s ease;
- }
- .submit-btn:hover {
- background-color: var(--secondary-color);
- }
- .comments-container {
- background-color: white;
- padding: 30px;
- border-radius: 10px;
- box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
- }
- .loading-comments {
- text-align: center;
- color: var(--light-text);
- padding: 20px 0;
- }
- .comment-item {
- padding: 20px 0;
- border-bottom: 1px solid #eee;
- }
- .comment-item:last-child {
- border-bottom: none;
- }
- .comment-header {
- display: flex;
- justify-content: space-between;
- margin-bottom: 10px;
- }
- .comment-author {
- font-weight: 700;
- color: var(--primary-color);
- }
- .comment-date {
- font-size: 0.9rem;
- color: var(--light-text);
- }
- .comment-content {
- line-height: 1.5;
- }
- </style>
- <section>
- <h2>Leave a Comment</h2>
- <% if (/^[0-9a-fA-F]{24}$/.test(pageId)) { %>
- <p>We'd love to hear your thoughts! Share your experience with this coloring page.</p>
- <% } else { %>
- <p>We'd love to hear your thoughts! Share your experience with our coloring pages.</p>
- <% } %>
- <form id="comment-form" class="comment-form">
- <div class="form-group">
- <label for="name">Name *</label>
- <input type="text" id="name" name="name" required>
- </div>
- <div class="form-group">
- <label for="email">Email *</label>
- <input type="email" id="email" name="email" required>
- </div>
- <div class="form-group">
- <label for="comment">Your Comment *</label>
- <textarea id="comment" name="comment" rows="5" required></textarea>
- </div>
- <div class="form-group">
- <button type="submit" class="submit-btn">Post Comment</button>
- </div>
- </form>
- <%- include('notification') %>
- <div id="comments-container" class="comments-container">
- <!-- <div class="loading-comments">Loading comments...</div> -->
- <% comments.forEach(item=> { %>
- <div class="comment-header">
- <div class="comment-author"><%= item.name %></div>
- <div class="comment-date"><%= dateFormat(item.createdAt) %></div>
- </div>
- <div class="comment-content"><%= item.comment %></div>
- <% }); %>
-
- <% if (!comments || comments.length <= 0) {%>
- <p class="no-comments">No comments yet. Be the first to leave a comment!</p>
- <% } %>
- </div>
- </section>
- <script>
- // 评论表单提交
- const commentForm = document.getElementById('comment-form');
- const commentsContainer = document.getElementById('comments-container');
- commentForm.addEventListener('submit', async (e) => {
- e.preventDefault();
- const formData = {
- name: document.getElementById('name').value,
- email: document.getElementById('email').value,
- comment: document.getElementById('comment').value,
- page: '<%= pageId%>'
- };
- try {
- // 显示加载状态
- const submitBtn = commentForm.querySelector('.submit-btn');
- submitBtn.disabled = true;
- submitBtn.textContent = 'Posting...';
- // 发送评论数据到服务器
- const response = await fetch('/api/comment', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify(formData)
- });
- if (response.ok) {
- // 清空表单
- commentForm.reset();
- // 重新加载评论
- // loadComments();
- showNotification('Thank you for your comment! It will appear after moderation.');
- } else {
- const errorData = await response.json();
- showNotification(`Error: ${errorData.message}`);
- }
- } catch (error) {
- console.error('Error submitting comment:', error);
- showNotification('An error occurred while submitting your comment. Please try again later.');
- } finally {
- // 恢复按钮状态
- const submitBtn = commentForm.querySelector('.submit-btn');
- submitBtn.disabled = false;
- submitBtn.textContent = 'Post Comment';
- }
- });
- // 加载评论
- async function loadComments() {
- try {
- const response = await fetch('/api/comment?page=<%=pageId %>');
- if (response.ok) {
- const comments = await response.json();
- // 清空加载状态
- commentsContainer.innerHTML = '';
- if (comments.length === 0) {
- commentsContainer.innerHTML = '<p class="no-comments">No comments yet. Be the first to leave a comment!</p>';
- return;
- }
- // 渲染评论
- comments.forEach(comment => {
- const commentElement = document.createElement('div');
- commentElement.className = 'comment-item';
- commentElement.innerHTML = `
- <div class="comment-header">
- <div class="comment-author">${comment.name}</div>
- <div class="comment-date">${new Date(comment.createdAt).toLocaleDateString()}</div>
- </div>
- <div class="comment-content">${comment.comment}</div>
- `;
- commentsContainer.appendChild(commentElement);
- });
- } else {
- commentsContainer.innerHTML = '<p class="error-loading">Error loading comments. Please try again later.</p>';
- }
- } catch (error) {
- console.error('Error loading comments:', error);
- commentsContainer.innerHTML = '<p class="error-loading">Error loading comments. Please try again later.</p>';
- }
- }
- // 页面加载时加载评论
- // document.addEventListener('DOMContentLoaded', loadComments);
- </script>
|