comment.ejs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <style>
  2. .comment-form {
  3. background-color: white;
  4. padding: 30px;
  5. border-radius: 10px;
  6. box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
  7. margin-bottom: 40px;
  8. }
  9. .form-group {
  10. margin-bottom: 20px;
  11. }
  12. .form-group label {
  13. display: block;
  14. margin-bottom: 8px;
  15. font-weight: 600;
  16. }
  17. .form-group input,
  18. .form-group textarea {
  19. width: 100%;
  20. padding: 12px;
  21. border: 1px solid #ddd;
  22. border-radius: 6px;
  23. font-size: 1rem;
  24. transition: border-color 0.3s ease;
  25. }
  26. .form-group input:focus,
  27. .form-group textarea:focus {
  28. outline: none;
  29. border-color: var(--primary-color);
  30. }
  31. .submit-btn {
  32. background-color: var(--primary-color);
  33. color: white;
  34. padding: 12px 30px;
  35. border: none;
  36. border-radius: 30px;
  37. cursor: pointer;
  38. font-weight: 600;
  39. transition: all 0.3s ease;
  40. }
  41. .submit-btn:hover {
  42. background-color: var(--secondary-color);
  43. }
  44. .comments-container {
  45. background-color: white;
  46. padding: 30px;
  47. border-radius: 10px;
  48. box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
  49. }
  50. .loading-comments {
  51. text-align: center;
  52. color: var(--light-text);
  53. padding: 20px 0;
  54. }
  55. .comment-item {
  56. padding: 20px 0;
  57. border-bottom: 1px solid #eee;
  58. }
  59. .comment-item:last-child {
  60. border-bottom: none;
  61. }
  62. .comment-header {
  63. display: flex;
  64. justify-content: space-between;
  65. margin-bottom: 10px;
  66. }
  67. .comment-author {
  68. font-weight: 700;
  69. color: var(--primary-color);
  70. }
  71. .comment-date {
  72. font-size: 0.9rem;
  73. color: var(--light-text);
  74. }
  75. .comment-content {
  76. line-height: 1.5;
  77. }
  78. </style>
  79. <section>
  80. <h2>Leave a Comment</h2>
  81. <% if (/^[0-9a-fA-F]{24}$/.test(pageId)) { %>
  82. <p>We'd love to hear your thoughts! Share your experience with this coloring page.</p>
  83. <% } else { %>
  84. <p>We'd love to hear your thoughts! Share your experience with our coloring pages.</p>
  85. <% } %>
  86. <form id="comment-form" class="comment-form">
  87. <div class="form-group">
  88. <label for="name">Name *</label>
  89. <input type="text" id="name" name="name" required>
  90. </div>
  91. <div class="form-group">
  92. <label for="email">Email *</label>
  93. <input type="email" id="email" name="email" required>
  94. </div>
  95. <div class="form-group">
  96. <label for="comment">Your Comment *</label>
  97. <textarea id="comment" name="comment" rows="5" required></textarea>
  98. </div>
  99. <div class="form-group">
  100. <button type="submit" class="submit-btn">Post Comment</button>
  101. </div>
  102. </form>
  103. <%- include('notification') %>
  104. <div id="comments-container" class="comments-container">
  105. <!-- <div class="loading-comments">Loading comments...</div> -->
  106. <% comments.forEach(item=> { %>
  107. <div class="comment-header">
  108. <div class="comment-author"><%= item.name %></div>
  109. <div class="comment-date"><%= dateFormat(item.createdAt) %></div>
  110. </div>
  111. <div class="comment-content"><%= item.comment %></div>
  112. <% }); %>
  113. <% if (!comments || comments.length <= 0) {%>
  114. <p class="no-comments">No comments yet. Be the first to leave a comment!</p>
  115. <% } %>
  116. </div>
  117. </section>
  118. <script>
  119. // 评论表单提交
  120. const commentForm = document.getElementById('comment-form');
  121. const commentsContainer = document.getElementById('comments-container');
  122. commentForm.addEventListener('submit', async (e) => {
  123. e.preventDefault();
  124. const formData = {
  125. name: document.getElementById('name').value,
  126. email: document.getElementById('email').value,
  127. comment: document.getElementById('comment').value,
  128. page: '<%= pageId%>'
  129. };
  130. try {
  131. // 显示加载状态
  132. const submitBtn = commentForm.querySelector('.submit-btn');
  133. submitBtn.disabled = true;
  134. submitBtn.textContent = 'Posting...';
  135. // 发送评论数据到服务器
  136. const response = await fetch('/api/comment', {
  137. method: 'POST',
  138. headers: {
  139. 'Content-Type': 'application/json'
  140. },
  141. body: JSON.stringify(formData)
  142. });
  143. if (response.ok) {
  144. // 清空表单
  145. commentForm.reset();
  146. // 重新加载评论
  147. // loadComments();
  148. showNotification('Thank you for your comment! It will appear after moderation.');
  149. } else {
  150. const errorData = await response.json();
  151. showNotification(`Error: ${errorData.message}`);
  152. }
  153. } catch (error) {
  154. console.error('Error submitting comment:', error);
  155. showNotification('An error occurred while submitting your comment. Please try again later.');
  156. } finally {
  157. // 恢复按钮状态
  158. const submitBtn = commentForm.querySelector('.submit-btn');
  159. submitBtn.disabled = false;
  160. submitBtn.textContent = 'Post Comment';
  161. }
  162. });
  163. // 加载评论
  164. async function loadComments() {
  165. try {
  166. const response = await fetch('/api/comment?page=<%=pageId %>');
  167. if (response.ok) {
  168. const comments = await response.json();
  169. // 清空加载状态
  170. commentsContainer.innerHTML = '';
  171. if (comments.length === 0) {
  172. commentsContainer.innerHTML = '<p class="no-comments">No comments yet. Be the first to leave a comment!</p>';
  173. return;
  174. }
  175. // 渲染评论
  176. comments.forEach(comment => {
  177. const commentElement = document.createElement('div');
  178. commentElement.className = 'comment-item';
  179. commentElement.innerHTML = `
  180. <div class="comment-header">
  181. <div class="comment-author">${comment.name}</div>
  182. <div class="comment-date">${new Date(comment.createdAt).toLocaleDateString()}</div>
  183. </div>
  184. <div class="comment-content">${comment.comment}</div>
  185. `;
  186. commentsContainer.appendChild(commentElement);
  187. });
  188. } else {
  189. commentsContainer.innerHTML = '<p class="error-loading">Error loading comments. Please try again later.</p>';
  190. }
  191. } catch (error) {
  192. console.error('Error loading comments:', error);
  193. commentsContainer.innerHTML = '<p class="error-loading">Error loading comments. Please try again later.</p>';
  194. }
  195. }
  196. // 页面加载时加载评论
  197. // document.addEventListener('DOMContentLoaded', loadComments);
  198. </script>