summaryrefslogtreecommitdiff
path: root/static/js/controllers/comments_controller.js
blob: 5abaec1ad6090d67448042cff97ffef1f7bb9e75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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) {
	event.preventDefault();
	var form = new FormData(event.target);
	form.append("domain", `${window.location.protocol}//${window.location.host}`);

	try {
	    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) {}
    }
 }