summaryrefslogtreecommitdiff
path: root/app/javascript/controllers/chat_controller.js
blob: 95b61c5de5f694558cc904006fe07f4f4c7f7c66 (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
import { Controller } from "stimulus";

export default class extends Controller {
  static targets = [ "message", "output" ];

  connect() {
    this.load();
  }

  send() {
    let vm = this;
    // TODO: Temporary hack. Should just run this after default event.
    setTimeout(function() {
      vm.messageTarget.value = "";
    }, 100);
  }

  load() {
    this.scrollToBottom();
    if (this.outputTarget.innerHTML.trim() === "") {
      fetch("/chat_messages")
        .then(response => response.text())
        .then(html => {
          this.outputTarget.innerHTML = html;
          this.scrollToBottom();
        });
    }
  }

  scrollToBottom() {
    this.outputTarget.scrollTop = this.outputTarget.scrollHeight;
  }
}