diff options
Diffstat (limited to 'static/js/controllers/comments_controller.js')
-rw-r--r-- | static/js/controllers/comments_controller.js | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/static/js/controllers/comments_controller.js b/static/js/controllers/comments_controller.js index 5abaec1..5e725ce 100644 --- a/static/js/controllers/comments_controller.js +++ b/static/js/controllers/comments_controller.js @@ -8,21 +8,31 @@ export class CommentsController extends BaseController { this.renderComments(); } - async renderForm() { - const form = (await this.get("/api/comments/new"))?.getElementById("comment-form"); - - if(form != null) { - this.comments_node.appendChild(form); - this.form_element = document.getElementById("comment-form"); - this.form_element.addEventListener("submit", this.submit.bind(this)); - } + renderForm() { + this.get("/api/comments/new").then((value) => { + const form = value?.getElementById("comment-form"); + + if(form != null) { + this.comments_node.appendChild(form); + this.form_element = document.getElementById("comment-form"); + this.form_element.addEventListener("submit", this.submit.bind(this)); + } + }); } - async renderComments() { - const comments = (await this.get(`/api/comments?path=${this.post}`))?.getElementById("comment-section"); + renderComments() { + this.get(`/api/comments?path=${this.post}`).then((value) => { + const comments = value?.getElementById("comment-section"); - if(comments != null) - this.comments_node.appendChild(comments_document.getElementById("comment-section")); + if(comments != null) + this.comments_node.appendChild(comments); + }); + } + + renderSubmitResponse(response_document) { + const new_comments = this.htmlFromResponse(response_document).getElementById("comment-section"); + const comments = document.getElementById("comment-section"); + comments.replaceWith(new_comments); } async submit(event) { @@ -31,10 +41,12 @@ export class CommentsController extends BaseController { form.append("domain", `${window.location.protocol}//${window.location.host}`); try { - const response = await fetch(`${this.server}/api/comments?path=${this.post}`, + const response = await fetch(`${this.comments_server_host}/api/comments?path=${this.post}`, { method: "POST", body: form }); - return response.text().then(this.htmlFromResponse.bind(this)); + response.text().then((response_document) => { + this.renderSubmitResponse(response_document); + }); } catch(error) {} } } |