Merge branch 'localstorage-mixin-fix' into 'master'

Moved localstorage parser to mixin

Closes #7

See merge request openflexure/openflexure-microscope-jsclient!13
This commit is contained in:
Joel Collins 2019-05-10 08:34:20 +00:00
commit 363dfa87a3
2 changed files with 21 additions and 10 deletions

View file

@ -87,21 +87,15 @@ export default {
mounted() {
// Propagate localMode settings
this.setLocalMode(this.localMode)
// Try loading and parsing savedHosts from localStorage
if (localStorage.getItem('savedHosts')) {
try {
this.savedHosts = JSON.parse(localStorage.getItem('savedHosts'));
} catch(e) {
localStorage.removeItem('savedHosts');
}
}
// Try loading savedHosts from localStorage. If null, don't change.
this.savedHosts = this.getLocalStorageObj('savedHosts') || this.savedHosts
},
// When savedHosts changes, serialise to JSON and save to localStorage
watch: {
savedHosts(newSavedHosts) {
const parsed = JSON.stringify(this.savedHosts);
localStorage.setItem('savedHosts', parsed);
this.setLocalStorageObj('savedHosts', this.savedHosts)
}
},

View file

@ -67,6 +67,23 @@ Vue.mixin({
}
this.$store.dispatch('errorState', errormsg);
UIkit.notification({message: `<span uk-icon=\'icon: warning\'></span> ${errormsg}`, status: 'danger'})
},
getLocalStorageObj: function(keyName) {
if (localStorage.getItem(keyName)) {
try {
return JSON.parse(localStorage.getItem(keyName))
} catch(e) {
console.log("Malformed entry. Removing from localStorage")
localStorage.removeItem(keyName)
return null
}
}
},
setLocalStorageObj: function(keyName, object) {
const parsed = JSON.stringify(object)
localStorage.setItem(keyName, parsed);
}
}