From 99eb3e5f712a0596217d5e89f2ee0bea973433c1 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Mon, 11 Sep 2023 18:40:40 +0100 Subject: [PATCH] Comments: Fixed JS error when lacking commenting permissions The page comments component would throw an error due to references to form elements/content, when form elements may not exist due to permisisons. For #4531 --- resources/js/components/page-comments.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/resources/js/components/page-comments.js b/resources/js/components/page-comments.js index a46a5c3b3..e2911afc6 100644 --- a/resources/js/components/page-comments.js +++ b/resources/js/components/page-comments.js @@ -27,16 +27,12 @@ export class PageComments extends Component { // Internal State this.parentId = null; - this.formReplyText = this.formReplyLink.textContent; + this.formReplyText = this.formReplyLink?.textContent || ''; this.setupListeners(); } setupListeners() { - this.removeReplyToButton.addEventListener('click', this.removeReplyTo.bind(this)); - this.hideFormButton.addEventListener('click', this.hideForm.bind(this)); - this.addCommentButton.addEventListener('click', this.showForm.bind(this)); - this.elem.addEventListener('page-comment-delete', () => { this.updateCount(); this.hideForm(); @@ -47,6 +43,9 @@ export class PageComments extends Component { }); if (this.form) { + this.removeReplyToButton.addEventListener('click', this.removeReplyTo.bind(this)); + this.hideFormButton.addEventListener('click', this.hideForm.bind(this)); + this.addCommentButton.addEventListener('click', this.showForm.bind(this)); this.form.addEventListener('submit', this.saveComment.bind(this)); } } @@ -123,9 +122,8 @@ export class PageComments extends Component { this.showForm(); this.parentId = commentLocalId; this.replyToRow.toggleAttribute('hidden', false); - const replyLink = this.replyToRow.querySelector('a'); - replyLink.textContent = this.formReplyText.replace('1234', this.parentId); - replyLink.href = `#comment${this.parentId}`; + this.formReplyLink.textContent = this.formReplyText.replace('1234', this.parentId); + this.formReplyLink.href = `#comment${this.parentId}`; } removeReplyTo() {