From 6d29bc27e78bc549479cbbc203c683137ace9d48 Mon Sep 17 00:00:00 2001
From: luixal <luixal@gmail.com>
Date: Mon, 4 Jan 2021 09:43:58 +0100
Subject: Adds mapping remote field to Homer expected ones when loading message
 from url

---
 src/components/Message.vue | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

(limited to 'src/components/Message.vue')

diff --git a/src/components/Message.vue b/src/components/Message.vue
index 5a1e0ea..df203ae 100644
--- a/src/components/Message.vue
+++ b/src/components/Message.vue
@@ -30,7 +30,8 @@ export default {
     // Look for a new message if an endpoint is provided.
     this.message = Object.assign({}, this.item);
     if (this.item && this.item.url) {
-      const fetchedMessage = await this.getMessage(this.item.url);
+      let fetchedMessage = await this.getMessage(this.item.url);
+      if (this.item.mapping) fetchedMessage = this.mapRemoteMessage(fetchedMessage);
       // keep the original config value if no value is provided by the endpoint
       for (const prop of ["title", "style", "content"]) {
         if (prop in fetchedMessage && fetchedMessage[prop] !== null) {
@@ -49,6 +50,13 @@ export default {
         return response.json();
       });
     },
+
+    mapRemoteMessage: function (message) {
+      let mapped = {};
+      // map property from message into mapped according to mapping config (only if field has a value):
+      for (const prop in this.item.mapping) if (message[this.item.mapping[prop]]) mapped[prop] = message[this.item.mapping[prop]];
+      return mapped;
+    },
   },
 };
 </script>
-- 
cgit v1.2.3

