|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 6 | + <title>Topic - Topic Name</title> |
| 7 | +</head> |
| 8 | +<body> |
| 9 | + <h1 id="topic-title">Topic Title</h1> |
| 10 | + |
| 11 | + <div id="comments-container"> |
| 12 | + <!-- Comments will be dynamically injected here --> |
| 13 | + </div> |
| 14 | + |
| 15 | + <h2>Add a Comment</h2> |
| 16 | + <form id="comment-form"> |
| 17 | + <label for="comment-content">Comment:</label> |
| 18 | + <textarea id="comment-content" required></textarea> |
| 19 | + <button type="submit">Post Comment</button> |
| 20 | + </form> |
| 21 | + |
| 22 | + <script> |
| 23 | + const topicName = window.location.pathname.split('/').pop().replace('.html', ''); // Get topic name from URL |
| 24 | + |
| 25 | + async function loadTopic() { |
| 26 | + const response = await fetch(`http://localhost:3000/forum/${topicName}`); |
| 27 | + const topic = await response.json(); |
| 28 | + |
| 29 | + document.getElementById('topic-title').textContent = topic.title; |
| 30 | + |
| 31 | + const commentsContainer = document.getElementById('comments-container'); |
| 32 | + commentsContainer.innerHTML = ''; |
| 33 | + topic.posts.forEach((post, index) => { |
| 34 | + const commentElement = document.createElement('div'); |
| 35 | + commentElement.innerHTML = ` |
| 36 | + <p>${post.content}</p> |
| 37 | + <small>Posted on: ${new Date(post.createdAt).toLocaleString()}</small> |
| 38 | + <hr> |
| 39 | + <button onclick="deleteComment(${index})">Delete Comment</button> |
| 40 | + `; |
| 41 | + commentsContainer.appendChild(commentElement); |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + async function deleteComment(replyIndex) { |
| 46 | + const response = await fetch(`http://localhost:3000/forum/${topicName}/reply/${replyIndex}`, { |
| 47 | + method: 'DELETE', |
| 48 | + headers: { |
| 49 | + 'Authorization': `Bearer admin` // Only admin can delete comments |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + if (response.ok) { |
| 54 | + alert('Comment deleted!'); |
| 55 | + loadTopic(); // Reload the topic to reflect the change |
| 56 | + } else { |
| 57 | + alert('Failed to delete comment'); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + loadTopic(); // Load the topic initially |
| 62 | + </script> |
| 63 | +</body> |
| 64 | +</html> |
0 commit comments