From 6d29bc27e78bc549479cbbc203c683137ace9d48 Mon Sep 17 00:00:00 2001 From: luixal 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') 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; + }, }, }; -- cgit v1.2.3 From 1ddf394176ffe7e1c4118ead46b9fa76c6f3c614 Mon Sep 17 00:00:00 2001 From: luixal Date: Thu, 7 Jan 2021 21:23:13 +0100 Subject: Refactors created function (splits logic for getting message) and adds timeout according to refreshInterval field --- src/components/Message.vue | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/components/Message.vue b/src/components/Message.vue index df203ae..72106c2 100644 --- a/src/components/Message.vue +++ b/src/components/Message.vue @@ -29,20 +29,27 @@ export default { created: async function () { // Look for a new message if an endpoint is provided. this.message = Object.assign({}, this.item); - if (this.item && 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) { - this.message[prop] = fetchedMessage[prop]; - } - } - } + await this.getMessage(); this.show = this.message.title || this.message.content; }, + methods: { - getMessage: function (url) { + getMessage: async function() { + if (this.item && this.item.url) { + let fetchedMessage = await this.downloadMessage(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) { + this.message[prop] = fetchedMessage[prop]; + } + } + } + console.log(this.item.refreshInterval); + if (this.item.refreshInterval) setTimeout(this.getMessage, this.item.refreshInterval); + }, + + downloadMessage: function (url) { return fetch(url).then(function (response) { if (response.status != 200) { return; -- cgit v1.2.3 From b6782c92b5ee91c4d6ac19ee01768363d4f0dc8b Mon Sep 17 00:00:00 2001 From: luixal Date: Thu, 7 Jan 2021 21:26:22 +0100 Subject: Removes forgotten console.log --- src/components/Message.vue | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/components/Message.vue b/src/components/Message.vue index 72106c2..2f71f3f 100644 --- a/src/components/Message.vue +++ b/src/components/Message.vue @@ -45,7 +45,6 @@ export default { } } } - console.log(this.item.refreshInterval); if (this.item.refreshInterval) setTimeout(this.getMessage, this.item.refreshInterval); }, -- cgit v1.2.3