diff --git a/js/comments.js b/js/comments.js
index 92663c3..2d3258d 100644
--- a/js/comments.js
+++ b/js/comments.js
@@ -16,15 +16,24 @@ document.addEventListener("DOMContentLoaded", function () {
}
/**
- * Registriert alle Antwort-Buttons.
+ * Aktiviert einen einzelnen Antworten-Button.
+ *
+ * @param {HTMLButtonElement} button Antworten-Button
*/
- document.querySelectorAll(".reply-button").forEach(function (button) {
+ function registerReplyButton(button) {
button.addEventListener("click", function () {
parentCommentInput.value = button.dataset.commentId;
replyInfo.textContent = "Antwort auf " + button.dataset.author;
replyInfo.style.display = "block";
commentContent.focus();
});
+ }
+
+ /**
+ * Registriert alle bereits vorhandenen Antwort-Buttons.
+ */
+ document.querySelectorAll(".reply-button").forEach(function (button) {
+ registerReplyButton(button);
});
/**
@@ -58,17 +67,15 @@ document.addEventListener("DOMContentLoaded", function () {
if (parentCommentId) {
commentElement.classList.add("comment-reply");
- }
- commentElement.innerHTML = `
-
- ${escapeHtml(data.author)}
- ${escapeHtml(data.created)}
-
- ${escapeHtml(data.content).replace(/\n/g, "
")}
- `;
+ commentElement.innerHTML = `
+
+ ${escapeHtml(data.author)}
+ ${escapeHtml(data.created)}
+
+ ${escapeHtml(data.content).replace(/\n/g, "
")}
+ `;
- if (parentCommentId) {
const parentReplies = document.querySelector(
`.comment-item[data-comment-id="${parentCommentId}"] .comment-replies`
);
@@ -76,8 +83,34 @@ document.addEventListener("DOMContentLoaded", function () {
if (parentReplies) {
parentReplies.appendChild(commentElement);
}
+
} else {
+ commentElement.dataset.commentId = data.commentId;
+
+ commentElement.innerHTML = `
+
+ ${escapeHtml(data.author)}
+ ${escapeHtml(data.created)}
+
+ ${escapeHtml(data.content).replace(/\n/g, "
")}
+
+
+
+
+ `;
+
commentsList.prepend(commentElement);
+
+ const newReplyButton = commentElement.querySelector(".reply-button");
+
+ if (newReplyButton) {
+ registerReplyButton(newReplyButton);
+ }
}
commentContent.value = "";
diff --git a/php/ajax/add-comment.php b/php/ajax/add-comment.php
index 0b9ef29..95a59e7 100644
--- a/php/ajax/add-comment.php
+++ b/php/ajax/add-comment.php
@@ -32,7 +32,7 @@ if (empty($articleId) || empty($content)) {
try {
$commentManager = CommentManager::getInstance();
- $commentManager->addComment(
+ $commentId = $commentManager->addComment(
$articleId,
$_SESSION["user_email"],
$content,
@@ -41,6 +41,7 @@ try {
echo json_encode([
"success" => true,
+ "commentId" => $commentId,
"author" => $_SESSION["user_email"],
"content" => $content,
"created" => date("Y-m-d H:i:s"),
diff --git a/php/model/CommentManagerDAO.php b/php/model/CommentManagerDAO.php
index 27e20c7..63aaf83 100644
--- a/php/model/CommentManagerDAO.php
+++ b/php/model/CommentManagerDAO.php
@@ -23,7 +23,7 @@ interface CommentManagerDAO
* @param string $content Inhalt des Kommentars
* @param int|null $parentCommentId ID des Eltern-Kommentars oder null
*
- * @return void
+ * @return int ID des neu gespeicherten Kommentars
*/
public function addComment(
$articleId,
diff --git a/php/model/DatabaseCommentManager.php b/php/model/DatabaseCommentManager.php
index df4124c..5f61e13 100644
--- a/php/model/DatabaseCommentManager.php
+++ b/php/model/DatabaseCommentManager.php
@@ -102,7 +102,7 @@ class DatabaseCommentManager implements CommentManagerDAO
* @param string $content Inhalt des Kommentars
* @param int|null $parentCommentId ID des Eltern-Kommentars oder null
*
- * @return void
+ * @return int ID des neu gespeicherten Kommentars
*/
public function addComment(
$articleId,
@@ -141,6 +141,8 @@ class DatabaseCommentManager implements CommentManagerDAO
":content" => $content
]);
+ return intval($db->lastInsertId());
+
} catch (PDOException $e) {
throw new RuntimeException(
"Kommentar konnte nicht gespeichert werden."
@@ -162,22 +164,22 @@ class DatabaseCommentManager implements CommentManagerDAO
$db = $this->getConnection();
$sql = "
- SELECT
- id,
- article_id,
- CASE
- WHEN parent_comment_id IS NULL THEN NULL
- WHEN parent_comment_id = '' THEN NULL
- WHEN parent_comment_id = 0 THEN NULL
- ELSE parent_comment_id
- END AS parent_comment_id,
- author,
- content,
- created
- FROM comments
- WHERE article_id = :articleId
- ORDER BY created ASC
- ";
+ SELECT
+ id,
+ article_id,
+ CASE
+ WHEN parent_comment_id IS NULL THEN NULL
+ WHEN parent_comment_id = '' THEN NULL
+ WHEN parent_comment_id = 0 THEN NULL
+ ELSE parent_comment_id
+ END AS parent_comment_id,
+ author,
+ content,
+ created
+ FROM comments
+ WHERE article_id = :articleId
+ ORDER BY created ASC
+ ";
$command = $db->prepare($sql);