Adds mapping remote field to Homer expected ones when loading message from url

This commit is contained in:
luixal
2021-01-04 09:43:58 +01:00
parent 3786f80dae
commit 6d29bc27e7
3 changed files with 76 additions and 1 deletions

View File

@@ -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>