diff options
Diffstat (limited to 'static/js/controllers/comments_controller.js')
-rw-r--r-- | static/js/controllers/comments_controller.js | 54 |
1 files changed, 32 insertions, 22 deletions
diff --git a/static/js/controllers/comments_controller.js b/static/js/controllers/comments_controller.js index 611b2ee..5abaec1 100644 --- a/static/js/controllers/comments_controller.js +++ b/static/js/controllers/comments_controller.js @@ -1,31 +1,41 @@ -export class CommentsController { - constructor(domain, post) { - this.domain = domain; - this.post = post; - this.parser = new DOMParser(); - this.form_element = document.getElementById("comment-form"); - this.form_element.addEventListener("submit", this.submit.bind(this)); +import { BaseController } from "./base_controller.js"; + +export class CommentsController extends BaseController { + constructor(domain) { + super(domain); + this.comments_node = document.getElementById("comments-thread"); + this.renderForm(); + 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)); + } + } + + async renderComments() { + const comments = (await this.get(`/api/comments?path=${this.post}`))?.getElementById("comment-section"); + + if(comments != null) + this.comments_node.appendChild(comments_document.getElementById("comment-section")); } async submit(event) { - const form = new FormData(event.target); - form.append("domain", window.location.hostname); + event.preventDefault(); + var form = new FormData(event.target); + form.append("domain", `${window.location.protocol}//${window.location.host}`); try { - const response = await fetch(`https://${this.domain}/${this.post}/comments`, - { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: form - }); - const response_page = this.htmlFromResponse(response.body); + const response = await fetch(`${this.server}/api/comments?path=${this.post}`, + { method: "POST", body: form }); + + return response.text().then(this.htmlFromResponse.bind(this)); } catch(error) {} } - - htmlFromResponse(body) { - return this.parser.parseFromString(body, "text/html"); - } } |