summaryrefslogtreecommitdiff
path: root/public/application.js
blob: ee905e03ece883989161311fe6fa62c5cbdbf41d (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
document.addEventListener('DOMContentLoaded', (event) => {
  const { createApp, ref } = Vue
  const regex = /\s*(?<sparklee>@\w+)\s+(?<reason>.+)/

  const app = createApp({
    created: function() {
      this.reload();
      this.intervalId = setInterval(() => this.reload(), 30000);
    },
    destroyed: function() {
      if (this.intervalId)
        clearInterval(this.intervalId);
      this.intervalId = null;
    },
    computed: {
      heading: function() {
        return this.sparkles.length == 0 ? "Nothing to see here" : "Recent";
      },
      recentSparkles: function() {
        return this.sparkles;
      },
      isDisabled: function() {
        return this.isSending || !this.isValid();
      }
    },
    data() {
      return {
        intervalId: null,
        errorMessage: "",
        isSending: false,
        sparkle: "",
        sparkles: [],
      }
    },
    methods: {
      reload: function() {
        fetch("/sparkles")
          .then((response) => response.json())
          .then((json) => this.sparkles = json)
          .catch((json) => console.dir(json));
      },
      isValid: function() {
        return this.sparkle.length > 0;
      },
      submitSparkle: function() {
        this.isSending = true;

        let matches = regex.exec(this.sparkle)
        let sparklee = matches.groups.sparklee
        let reason = matches.groups.reason

        fetch("/sparkles", {
          method: "POST",
          mode: "cors",
          cache: "no-cache",
          headers: { "Content-Type": "application/json" },
          redirect: "follow",
          body: JSON.stringify({ sparklee: sparklee, reason: reason })
        }).then((response) => {
          response.json().then((json) => {
            this.isSending = false;
            if (response.ok) {
              this.sparkles.push(json);
              this.sparkle = "";
            } else {
              this.errorMessage = json["error"];
            }
          })
        }).catch((error) => console.error(error));
      },
    }
  })

  app.config.compilerOptions.delimiters = ["${", "}"];
  app.mount('#app')
  htmx.logAll();
})