aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/components/Message.vue
blob: fcb0fbba9bb79adf4770dfa8c094a53152a9a3d4 (plain) (blame)
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
<template>
  <article v-if="item" class="message" :class="item.style">
    <div v-if="item.title" class="message-header">
      <p>{{ item.title }}</p>
    </div>
    <div v-if="item.content" class="message-body" v-html="item.content"></div>
  </article>
</template>

<script>
export default {
  name: "Message",
  props: {
    item: Object,
  },
  created: function () {
    // Look for a new message if an endpoint is provided.
    let that = this;
    if (this.item && this.item.url) {
      this.getMessage(this.item.url).then(function (message) {
        // keep the original config value if no value is provided by the endpoint
        for (const prop of ["title", "style", "content"]) {
          if (prop in message && message[prop] !== null) {
            that.item[prop] = message[prop];
          }
        }
      });
    }
  },
  methods: {
    getMessage: function (url) {
      return fetch(url).then(function (response) {
        if (response.status != 200) {
          return;
        }
        return response.json();
      });
    },
  },
};
</script>