Skip to content

Commit

Permalink
Move tags retrieval into Vuex action
Browse files Browse the repository at this point in the history
This should make it much easier to trigger a reload from other parts of
the UI once this is needed in the future.

Signed-off-by: Florian Hotze <[email protected]>
  • Loading branch information
florian-h05 committed Jun 23, 2023
1 parent 0bca358 commit df102c3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
14 changes: 3 additions & 11 deletions bundles/org.openhab.ui/web/src/components/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -507,15 +507,8 @@ export default {
if (data[2]) dayjs.locale(data[2].key)
// load the Semantic tags, only if the `tags` endpoint exists (or empty arrays otherwise)
return Promise.all([
...this.$store.getters.apiEndpoint('tags')
? [this.$oh.api.get('/rest/tags')]
: [Promise.resolve([])]
]).then((data) => {
// store the Semantic tags
this.$store.commit('setSemanticClasses', { tags: data[0] })
// load the Semantic tags
this.$store.dispatch('loadSemantics').then(() => {
this.ready = true
return Promise.resolve()
})
Expand Down Expand Up @@ -552,8 +545,7 @@ export default {
localStorage.setItem('openhab.ui:serverUrl', this.serverUrl)
localStorage.setItem('openhab.ui:username', this.username)
localStorage.setItem('openhab.ui:password', this.password)
this.loadData().then((data) => {
// this.sitemaps = data
this.loadData().then(() => {
this.loginScreenOpened = false
this.loggedIn = true
}).catch((err) => {
Expand Down
38 changes: 26 additions & 12 deletions bundles/org.openhab.ui/web/src/js/store/modules/semantics.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import i18n from '@/js/i18n'
import api from '@/js/openhab/api'

const state = {
Locations: [],
Expand All @@ -14,23 +15,36 @@ const getters = {
}
}

const mutations = {
setSemanticClasses (state, { tags }) {
state.Locations = tags.filter(t => t.uid.startsWith('Location_')).map(t => t.name)
state.Equipment = tags.filter(t => t.uid.startsWith('Equipment_')).map(t => t.name)
state.Points = tags.filter(t => t.uid.startsWith('Point_')).map(t => t.name)
state.Properties = tags.filter(t => t.uid.startsWith('Property_')).map(t => t.name)
// Store i18n labels
Object.values(tags).forEach(t => {
if (t.label) state.Labels[t.name] = t.label
})
// Save as i18n messages
i18n.mergeLocaleMessage(this.getters.locale, state.Labels)
const actions = {
loadSemantics () {
if (this.getters.apiEndpoint('tags')) {
api.get('/rest/tags')
.then((tags) => {
state.Locations = tags.filter(t => t.uid.startsWith('Location_')).map(t => t.name)
state.Equipment = tags.filter(t => t.uid.startsWith('Equipment_')).map(t => t.name)
state.Points = tags.filter(t => t.uid.startsWith('Point_')).map(t => t.name)
state.Properties = tags.filter(t => t.uid.startsWith('Property_')).map(t => t.name)
// Store i18n labels
Object.values(tags).forEach(t => {
if (t.label) state.Labels[t.name] = t.label
})
// Save as i18n messages
i18n.mergeLocaleMessage(this.getters.locale, state.Labels)

return Promise.resolve()
})
.catch((e) => Promise.reject(e))
} else {
return Promise.resolve()
}
}
}

const mutations = {}

export default {
state,
getters,
actions,
mutations
}

0 comments on commit df102c3

Please sign in to comment.