Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import store from '@/store';
- import Vue from 'vue';
- import { LOAD_USER_PREFERENCES, SET_USER_PREFERENCE } from '@/store/mutations.type';
- const state = {
- userPrefs: [],
- loaded: false
- };
- const getters = {
- getUserPreference: (state) => (key) => {
- if(!loaded)
- store.commit(LOAD_USER_PREFERENCES);
- return state.userPrefs[key];
- }
- };
- const actions = {
- };
- /* eslint no-param-reassign: ["error", { "props": false }] */
- const mutations = {
- [LOAD_USER_PREFERENCES] (state) {
- let json = localStorage.getItem('userPrefs');
- if(json)
- state.userPrefs = JSON.parse(json);
- state.loaded = true;
- },
- [SET_USER_PREFERENCE] (state, payload) {
- if(!payload || !payload.key) {
- console.error(`${SET_USER_PREFERENCE} requires a payload with at least a key property to be set.`);
- return;
- }
- if(!payload.value) {
- Vue.delete(state.userPrefs, payload.key);
- } else {
- Vue.set(state.userPrefs, payload.key, payload.value);
- }
- localStorage.setItem('userPrefs', JSON.stringify(state.userPrefs));
- }
- };
- export default {
- state,
- getters,
- actions,
- mutations
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement