Fehlerbehebung
This commit is contained in:
+37
-4
@@ -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 () {
|
button.addEventListener("click", function () {
|
||||||
parentCommentInput.value = button.dataset.commentId;
|
parentCommentInput.value = button.dataset.commentId;
|
||||||
replyInfo.textContent = "Antwort auf " + button.dataset.author;
|
replyInfo.textContent = "Antwort auf " + button.dataset.author;
|
||||||
replyInfo.style.display = "block";
|
replyInfo.style.display = "block";
|
||||||
commentContent.focus();
|
commentContent.focus();
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registriert alle bereits vorhandenen Antwort-Buttons.
|
||||||
|
*/
|
||||||
|
document.querySelectorAll(".reply-button").forEach(function (button) {
|
||||||
|
registerReplyButton(button);
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -58,7 +67,6 @@ document.addEventListener("DOMContentLoaded", function () {
|
|||||||
|
|
||||||
if (parentCommentId) {
|
if (parentCommentId) {
|
||||||
commentElement.classList.add("comment-reply");
|
commentElement.classList.add("comment-reply");
|
||||||
}
|
|
||||||
|
|
||||||
commentElement.innerHTML = `
|
commentElement.innerHTML = `
|
||||||
<p>
|
<p>
|
||||||
@@ -68,7 +76,6 @@ document.addEventListener("DOMContentLoaded", function () {
|
|||||||
<p>${escapeHtml(data.content).replace(/\n/g, "<br>")}</p>
|
<p>${escapeHtml(data.content).replace(/\n/g, "<br>")}</p>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
if (parentCommentId) {
|
|
||||||
const parentReplies = document.querySelector(
|
const parentReplies = document.querySelector(
|
||||||
`.comment-item[data-comment-id="${parentCommentId}"] .comment-replies`
|
`.comment-item[data-comment-id="${parentCommentId}"] .comment-replies`
|
||||||
);
|
);
|
||||||
@@ -76,8 +83,34 @@ document.addEventListener("DOMContentLoaded", function () {
|
|||||||
if (parentReplies) {
|
if (parentReplies) {
|
||||||
parentReplies.appendChild(commentElement);
|
parentReplies.appendChild(commentElement);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
commentElement.dataset.commentId = data.commentId;
|
||||||
|
|
||||||
|
commentElement.innerHTML = `
|
||||||
|
<p>
|
||||||
|
<strong>${escapeHtml(data.author)}</strong>
|
||||||
|
<span>${escapeHtml(data.created)}</span>
|
||||||
|
</p>
|
||||||
|
<p>${escapeHtml(data.content).replace(/\n/g, "<br>")}</p>
|
||||||
|
|
||||||
|
<button type="button"
|
||||||
|
class="reply-button"
|
||||||
|
data-comment-id="${escapeHtml(data.commentId)}"
|
||||||
|
data-author="${escapeHtml(data.author)}">
|
||||||
|
Antworten
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<div class="comment-replies"></div>
|
||||||
|
`;
|
||||||
|
|
||||||
commentsList.prepend(commentElement);
|
commentsList.prepend(commentElement);
|
||||||
|
|
||||||
|
const newReplyButton = commentElement.querySelector(".reply-button");
|
||||||
|
|
||||||
|
if (newReplyButton) {
|
||||||
|
registerReplyButton(newReplyButton);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
commentContent.value = "";
|
commentContent.value = "";
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ if (empty($articleId) || empty($content)) {
|
|||||||
try {
|
try {
|
||||||
$commentManager = CommentManager::getInstance();
|
$commentManager = CommentManager::getInstance();
|
||||||
|
|
||||||
$commentManager->addComment(
|
$commentId = $commentManager->addComment(
|
||||||
$articleId,
|
$articleId,
|
||||||
$_SESSION["user_email"],
|
$_SESSION["user_email"],
|
||||||
$content,
|
$content,
|
||||||
@@ -41,6 +41,7 @@ try {
|
|||||||
|
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
"success" => true,
|
"success" => true,
|
||||||
|
"commentId" => $commentId,
|
||||||
"author" => $_SESSION["user_email"],
|
"author" => $_SESSION["user_email"],
|
||||||
"content" => $content,
|
"content" => $content,
|
||||||
"created" => date("Y-m-d H:i:s"),
|
"created" => date("Y-m-d H:i:s"),
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ interface CommentManagerDAO
|
|||||||
* @param string $content Inhalt des Kommentars
|
* @param string $content Inhalt des Kommentars
|
||||||
* @param int|null $parentCommentId ID des Eltern-Kommentars oder null
|
* @param int|null $parentCommentId ID des Eltern-Kommentars oder null
|
||||||
*
|
*
|
||||||
* @return void
|
* @return int ID des neu gespeicherten Kommentars
|
||||||
*/
|
*/
|
||||||
public function addComment(
|
public function addComment(
|
||||||
$articleId,
|
$articleId,
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ class DatabaseCommentManager implements CommentManagerDAO
|
|||||||
* @param string $content Inhalt des Kommentars
|
* @param string $content Inhalt des Kommentars
|
||||||
* @param int|null $parentCommentId ID des Eltern-Kommentars oder null
|
* @param int|null $parentCommentId ID des Eltern-Kommentars oder null
|
||||||
*
|
*
|
||||||
* @return void
|
* @return int ID des neu gespeicherten Kommentars
|
||||||
*/
|
*/
|
||||||
public function addComment(
|
public function addComment(
|
||||||
$articleId,
|
$articleId,
|
||||||
@@ -141,6 +141,8 @@ class DatabaseCommentManager implements CommentManagerDAO
|
|||||||
":content" => $content
|
":content" => $content
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
return intval($db->lastInsertId());
|
||||||
|
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
throw new RuntimeException(
|
throw new RuntimeException(
|
||||||
"Kommentar konnte nicht gespeichert werden."
|
"Kommentar konnte nicht gespeichert werden."
|
||||||
|
|||||||
Reference in New Issue
Block a user