diff options
author | Bastien Wirtz <bastien.wirtz@gmail.com> | 2021-03-04 18:40:07 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-04 18:40:07 -0800 |
commit | cc7ff885527283d97f32347210b0370e8477c4ff (patch) | |
tree | 6c2eda44ed25cf2cd91cced7ab159ecd5e6735f7 /src | |
parent | 7bcfce6bdad32ec392279867af7f3f3c67a972b3 (diff) | |
parent | 9542de6eb25bfa20b376920b607db864c9e20a7a (diff) | |
download | homer-cc7ff885527283d97f32347210b0370e8477c4ff.tar.gz homer-cc7ff885527283d97f32347210b0370e8477c4ff.tar.zst homer-cc7ff885527283d97f32347210b0370e8477c4ff.zip |
Merge pull request #175 from luixal/message-remote-fields-mappingv21.03.1
Adds mapping remote field to Homer expected ones when loading message from URL
Diffstat (limited to 'src')
-rw-r--r-- | src/components/Message.vue | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/src/components/Message.vue b/src/components/Message.vue index 5a1e0ea..2f71f3f 100644 --- a/src/components/Message.vue +++ b/src/components/Message.vue | |||
@@ -29,19 +29,26 @@ export default { | |||
29 | created: async function () { | 29 | created: async function () { |
30 | // Look for a new message if an endpoint is provided. | 30 | // Look for a new message if an endpoint is provided. |
31 | this.message = Object.assign({}, this.item); | 31 | this.message = Object.assign({}, this.item); |
32 | if (this.item && this.item.url) { | 32 | await this.getMessage(); |
33 | const fetchedMessage = await this.getMessage(this.item.url); | ||
34 | // keep the original config value if no value is provided by the endpoint | ||
35 | for (const prop of ["title", "style", "content"]) { | ||
36 | if (prop in fetchedMessage && fetchedMessage[prop] !== null) { | ||
37 | this.message[prop] = fetchedMessage[prop]; | ||
38 | } | ||
39 | } | ||
40 | } | ||
41 | this.show = this.message.title || this.message.content; | 33 | this.show = this.message.title || this.message.content; |
42 | }, | 34 | }, |
35 | |||
43 | methods: { | 36 | methods: { |
44 | getMessage: function (url) { | 37 | getMessage: async function() { |
38 | if (this.item && this.item.url) { | ||
39 | let fetchedMessage = await this.downloadMessage(this.item.url); | ||
40 | if (this.item.mapping) fetchedMessage = this.mapRemoteMessage(fetchedMessage); | ||
41 | // keep the original config value if no value is provided by the endpoint | ||
42 | for (const prop of ["title", "style", "content"]) { | ||
43 | if (prop in fetchedMessage && fetchedMessage[prop] !== null) { | ||
44 | this.message[prop] = fetchedMessage[prop]; | ||
45 | } | ||
46 | } | ||
47 | } | ||
48 | if (this.item.refreshInterval) setTimeout(this.getMessage, this.item.refreshInterval); | ||
49 | }, | ||
50 | |||
51 | downloadMessage: function (url) { | ||
45 | return fetch(url).then(function (response) { | 52 | return fetch(url).then(function (response) { |
46 | if (response.status != 200) { | 53 | if (response.status != 200) { |
47 | return; | 54 | return; |
@@ -49,6 +56,13 @@ export default { | |||
49 | return response.json(); | 56 | return response.json(); |
50 | }); | 57 | }); |
51 | }, | 58 | }, |
59 | |||
60 | mapRemoteMessage: function (message) { | ||
61 | let mapped = {}; | ||
62 | // map property from message into mapped according to mapping config (only if field has a value): | ||
63 | for (const prop in this.item.mapping) if (message[this.item.mapping[prop]]) mapped[prop] = message[this.item.mapping[prop]]; | ||
64 | return mapped; | ||
65 | }, | ||
52 | }, | 66 | }, |
53 | }; | 67 | }; |
54 | </script> | 68 | </script> |