generated from CSA-trimester-2/nba-analysis-final
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomment.html
98 lines (85 loc) · 3.13 KB
/
comment.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Comment Section</title>
<style>
#comment-section {
margin: auto;
width: 50%;
padding: 10px;
}
#comments {
margin-bottom: 20px;
}
.comment {
background-color: #f2f2f2;
padding: 10px;
margin-bottom: 10px;
}
input, textarea {
width: 100%;
margin-bottom: 10px;
}
button {
width: 100%;
padding: 10px;
background-color: blue;
color: white;
cursor: pointer;
}
</style>
</head>
<body>
<div id="comment-section">
<h2>Comments</h2>
<input type="text" id="gameNameInput" placeholder="Enter game name to view comments">
<button onclick="fetchComments()">Load Comments</button>
<div id="comments"></div>
<h3>Add a comment</h3>
<input type="text" id="userName" placeholder="Your name">
<textarea id="text" placeholder="Your comment"></textarea>
<button onclick="postComment()">Post Comment</button>
</div>
<script>
function fetchComments() {
const gameName = document.getElementById('gameNameInput').value;
fetch(`http://localhost:8062/api/comments/${encodeURIComponent(gameName)}`)
.then(response => response.json())
.then(data => {
const commentsDiv = document.getElementById('comments');
commentsDiv.innerHTML = ''; // Clear existing comments
data.forEach(comment => {
const commentDiv = document.createElement('div');
commentDiv.classList.add('comment');
commentDiv.innerHTML = `<strong>${comment.userName}</strong>: ${comment.text}`;
commentsDiv.appendChild(commentDiv);
});
})
.catch(error => console.error('Error fetching comments:', error));
}
function postComment() {
const userName = document.getElementById('userName').value;
const gameName = document.getElementById('gameNameInput').value; // Use the same game name for posting
const text = document.getElementById('text').value;
const comment = { userName, gameName, text, timestamp: new Date().toISOString() };
fetch('http://localhost:8062/api/comments/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(comment),
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
fetchComments(); // Refresh comments after posting
})
.catch((error) => {
console.error('Error posting comment:', error);
});
}
</script>
</body>
</html>