import { BaseController } from "./base_controller.js"; export class CommentsController extends BaseController { constructor(replies_controller) { super(); this.replies_controller = replies_controller; this.comments_node = document.getElementById("comments-thread"); this.renderForm(); this.renderComments(); } 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)); } }); } renderComments() { this.get(`/api/comments?path=${this.post}`).then((value) => { const comments = value?.getElementById("comment-section"); if(comments != null) { this.comments_node.appendChild(comments); this.replies_controller.init(); } }); } 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) { event.preventDefault(); var form = new FormData(event.target); form.append("domain", `${window.location.protocol}//${window.location.host}`); try { const response = await fetch(`${this.comments_server_host}/api/comments?path=${this.post}`, { method: "POST", body: form }); response.text().then((response_document) => { this.renderSubmitResponse(response_document); }); } catch(error) {} } }