added persistence plugin to store

This commit is contained in:
Steffen 2020-05-02 21:46:51 +02:00
parent 23f754a955
commit 7e48036aa3
No known key found for this signature in database
GPG Key ID: 764D74E98267DFC6
3 changed files with 39 additions and 24 deletions

View File

@ -70,15 +70,6 @@ export default {
} }
}, },
mounted() { mounted() {
if (localStorage.background !== undefined) {
this.$store.commit("setBackground", JSON.parse(localStorage.background));
}
if (localStorage.isPublic !== undefined) {
this.$store.commit("showGrimoire", JSON.parse(localStorage.isPublic));
}
if (localStorage.edition) {
this.$store.commit("setEdition", localStorage.edition);
}
if (localStorage.players) { if (localStorage.players) {
this.players = JSON.parse(localStorage.players).map(player => ({ this.players = JSON.parse(localStorage.players).map(player => ({
...player, ...player,
@ -119,19 +110,6 @@ export default {
); );
}, },
deep: true deep: true
},
edition(newEdition) {
localStorage.edition = newEdition;
},
isPublic(newIsPublic) {
localStorage.isPublic = JSON.stringify(newIsPublic);
},
background(newBackground) {
if (newBackground) {
localStorage.background = JSON.stringify(newBackground);
} else {
localStorage.removeItem("background");
}
} }
} }
}; };

View File

@ -1,12 +1,14 @@
import Vue from "vue"; import Vue from "vue";
import Vuex from "vuex"; import Vuex from "vuex";
import persistence from "./persistence";
import editionJSON from "../editions.json"; import editionJSON from "../editions.json";
import rolesJSON from "../roles.json"; import rolesJSON from "../roles.json";
Vue.use(Vuex); Vue.use(Vuex);
const getRolesByEdition = (edition = "tb") => { const getRolesByEdition = (edition = "tb") => {
const selectedEdition = editionJSON.find(({ id }) => id === edition); const selectedEdition =
editionJSON.find(({ id }) => id === edition) || editionJSON[0];
return new Map( return new Map(
rolesJSON rolesJSON
.filter( .filter(
@ -73,5 +75,6 @@ export default new Vuex.Store({
state.modals.edition = false; state.modals.edition = false;
state.roles = getRolesByEdition(edition); state.roles = getRolesByEdition(edition);
} }
} },
plugins: [persistence]
}); });

34
src/store/persistence.js Normal file
View File

@ -0,0 +1,34 @@
module.exports = store => {
if (localStorage.background !== undefined) {
store.commit("setBackground", localStorage.background);
}
if (localStorage.isPublic !== undefined) {
store.commit("showGrimoire", JSON.parse(localStorage.isPublic));
}
if (localStorage.edition !== undefined) {
store.commit("setEdition", localStorage.edition);
}
store.subscribe(({ type, payload }, state) => {
switch (type) {
case "toggleGrimoire":
case "showGrimoire":
localStorage.setItem(
"isPublic",
JSON.stringify(state.grimoire.isPublic)
);
break;
case "setBackground":
if (payload) {
localStorage.setItem("background", payload);
} else {
localStorage.removeItem("background");
}
break;
case "setEdition":
localStorage.setItem("edition", payload);
break;
}
console.log(type, payload);
});
};