Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. import store from '@/store';
  2. import Vue from 'vue';
  3. import { LOAD_USER_PREFERENCES, SET_USER_PREFERENCE } from '@/store/mutations.type';
  4.  
  5. const state = {
  6. userPrefs: [],
  7. loaded: false
  8. };
  9.  
  10. const getters = {
  11. getUserPreference: (state) => (key) => {
  12. if(!loaded)
  13. store.commit(LOAD_USER_PREFERENCES);
  14. return state.userPrefs[key];
  15. }
  16. };
  17.  
  18. const actions = {
  19. };
  20.  
  21. /* eslint no-param-reassign: ["error", { "props": false }] */
  22. const mutations = {
  23. [LOAD_USER_PREFERENCES] (state) {
  24. let json = localStorage.getItem('userPrefs');
  25. if(json)
  26. state.userPrefs = JSON.parse(json);
  27. state.loaded = true;
  28. },
  29. [SET_USER_PREFERENCE] (state, payload) {
  30. if(!payload || !payload.key) {
  31. console.error(`${SET_USER_PREFERENCE} requires a payload with at least a key property to be set.`);
  32. return;
  33. }
  34.  
  35. if(!payload.value) {
  36. Vue.delete(state.userPrefs, payload.key);
  37. } else {
  38. Vue.set(state.userPrefs, payload.key, payload.value);
  39. }
  40.  
  41. localStorage.setItem('userPrefs', JSON.stringify(state.userPrefs));
  42. }
  43. };
  44.  
  45. export default {
  46. state,
  47. getters,
  48. actions,
  49. mutations
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement