diff options
author | luixal <luixal@gmail.com> | 2021-01-07 21:23:13 +0100 |
---|---|---|
committer | luixal <luixal@gmail.com> | 2021-01-07 21:23:13 +0100 |
commit | 1ddf394176ffe7e1c4118ead46b9fa76c6f3c614 (patch) | |
tree | 2d6b2dc6126a66b8a7d3f906e64c02ac2b940efe | |
parent | 6d29bc27e78bc549479cbbc203c683137ace9d48 (diff) | |
download | homer-1ddf394176ffe7e1c4118ead46b9fa76c6f3c614.tar.gz homer-1ddf394176ffe7e1c4118ead46b9fa76c6f3c614.tar.zst homer-1ddf394176ffe7e1c4118ead46b9fa76c6f3c614.zip |
Refactors created function (splits logic for getting message) and adds timeout according to refreshInterval field
-rw-r--r-- | docs/configuration.md | 2 | ||||
-rw-r--r-- | src/components/Message.vue | 29 |
2 files changed, 20 insertions, 11 deletions
diff --git a/docs/configuration.md b/docs/configuration.md index 93fa898..8bff54f 100644 --- a/docs/configuration.md +++ b/docs/configuration.md | |||
@@ -69,12 +69,14 @@ message: | |||
69 | # mapping: # allows to map fields from the remote format to the one expected by Homer | 69 | # mapping: # allows to map fields from the remote format to the one expected by Homer |
70 | # title: 'id' # use value from field 'id' as title | 70 | # title: 'id' # use value from field 'id' as title |
71 | # content: 'value' # value from field 'value' as content | 71 | # content: 'value' # value from field 'value' as content |
72 | # refreshInterval: 10000 # time interval to refresh message | ||
72 | # | 73 | # |
73 | # Real example using chucknorris.io for showing Chuck Norris facts as messages: | 74 | # Real example using chucknorris.io for showing Chuck Norris facts as messages: |
74 | # url: https://api.chucknorris.io/jokes/random | 75 | # url: https://api.chucknorris.io/jokes/random |
75 | # mapping: | 76 | # mapping: |
76 | # title: 'id' | 77 | # title: 'id' |
77 | # content: 'value' | 78 | # content: 'value' |
79 | # refreshInterval: 10000 | ||
78 | style: "is-warning" | 80 | style: "is-warning" |
79 | title: "Optional message!" | 81 | title: "Optional message!" |
80 | icon: "fa fa-exclamation-triangle" | 82 | icon: "fa fa-exclamation-triangle" |
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 { | |||
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 | let fetchedMessage = await this.getMessage(this.item.url); | ||
34 | if (this.item.mapping) fetchedMessage = this.mapRemoteMessage(fetchedMessage); | ||
35 | // keep the original config value if no value is provided by the endpoint | ||
36 | for (const prop of ["title", "style", "content"]) { | ||
37 | if (prop in fetchedMessage && fetchedMessage[prop] !== null) { | ||
38 | this.message[prop] = fetchedMessage[prop]; | ||
39 | } | ||
40 | } | ||
41 | } | ||
42 | this.show = this.message.title || this.message.content; | 33 | this.show = this.message.title || this.message.content; |
43 | }, | 34 | }, |
35 | |||
44 | methods: { | 36 | methods: { |
45 | 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 | console.log(this.item.refreshInterval); | ||
49 | if (this.item.refreshInterval) setTimeout(this.getMessage, this.item.refreshInterval); | ||
50 | }, | ||
51 | |||
52 | downloadMessage: function (url) { | ||
46 | return fetch(url).then(function (response) { | 53 | return fetch(url).then(function (response) { |
47 | if (response.status != 200) { | 54 | if (response.status != 200) { |
48 | return; | 55 | return; |