2017-02-21 04:25:19 +11:00
|
|
|
import merge from 'lodash.merge'
|
|
|
|
import objectPath from 'object-path'
|
2017-03-07 18:43:48 +11:00
|
|
|
import localforage from 'localforage'
|
2018-09-08 01:17:17 +10:00
|
|
|
import { each } from 'lodash'
|
2017-02-21 04:25:19 +11:00
|
|
|
|
2017-04-10 00:30:21 +10:00
|
|
|
let loaded = false
|
|
|
|
|
2017-02-21 04:25:19 +11:00
|
|
|
const defaultReducer = (state, paths) => (
|
|
|
|
paths.length === 0 ? state : paths.reduce((substate, path) => {
|
|
|
|
objectPath.set(substate, path, objectPath.get(state, path))
|
|
|
|
return substate
|
|
|
|
}, {})
|
|
|
|
)
|
|
|
|
|
2018-09-08 01:17:17 +10:00
|
|
|
const saveImmedeatelyActions = [
|
|
|
|
'markNotificationsAsSeen',
|
|
|
|
'clearCurrentUser',
|
|
|
|
'setCurrentUser',
|
|
|
|
'setOption'
|
|
|
|
]
|
|
|
|
|
2017-02-21 04:25:19 +11:00
|
|
|
const defaultStorage = (() => {
|
2017-03-07 18:43:48 +11:00
|
|
|
return localforage
|
2017-02-21 04:25:19 +11:00
|
|
|
})()
|
|
|
|
|
|
|
|
export default function createPersistedState ({
|
2017-02-24 05:11:22 +11:00
|
|
|
key = 'vuex-lz',
|
2017-02-21 04:25:19 +11:00
|
|
|
paths = [],
|
|
|
|
getState = (key, storage) => {
|
2017-02-21 04:54:09 +11:00
|
|
|
let value = storage.getItem(key)
|
2017-03-07 18:43:48 +11:00
|
|
|
return value
|
2017-02-21 04:25:19 +11:00
|
|
|
},
|
2018-09-08 01:17:17 +10:00
|
|
|
setState = (key, state, storage) => {
|
|
|
|
if (!loaded) {
|
|
|
|
console.log('waiting for old state to be loaded...')
|
|
|
|
return Promise.resolve()
|
|
|
|
} else {
|
|
|
|
return storage.setItem(key, state)
|
|
|
|
}
|
|
|
|
},
|
2017-02-21 04:25:19 +11:00
|
|
|
reducer = defaultReducer,
|
|
|
|
storage = defaultStorage,
|
|
|
|
subscriber = store => handler => store.subscribe(handler)
|
|
|
|
} = {}) {
|
|
|
|
return store => {
|
2017-03-07 18:43:48 +11:00
|
|
|
getState(key, storage).then((savedState) => {
|
2017-04-10 06:15:35 +10:00
|
|
|
try {
|
|
|
|
if (typeof savedState === 'object') {
|
|
|
|
// build user cache
|
|
|
|
const usersState = savedState.users || {}
|
|
|
|
usersState.usersObject = {}
|
|
|
|
const users = usersState.users || []
|
|
|
|
each(users, (user) => { usersState.usersObject[user.id] = user })
|
|
|
|
savedState.users = usersState
|
2017-03-09 03:59:12 +11:00
|
|
|
|
2017-04-10 06:15:35 +10:00
|
|
|
store.replaceState(
|
|
|
|
merge({}, store.state, savedState)
|
|
|
|
)
|
|
|
|
}
|
2017-11-18 22:13:51 +11:00
|
|
|
if (store.state.config.customTheme) {
|
|
|
|
// This is a hack to deal with async loading of config.json and themes
|
|
|
|
// See: style_setter.js, setPreset()
|
|
|
|
window.themeLoaded = true
|
|
|
|
store.dispatch('setOption', {
|
|
|
|
name: 'customTheme',
|
|
|
|
value: store.state.config.customTheme
|
|
|
|
})
|
|
|
|
}
|
2017-07-02 21:07:35 +10:00
|
|
|
if (store.state.users.lastLoginName) {
|
|
|
|
store.dispatch('loginUser', {username: store.state.users.lastLoginName, password: 'xxx'})
|
|
|
|
}
|
2017-04-10 06:15:35 +10:00
|
|
|
loaded = true
|
|
|
|
} catch (e) {
|
|
|
|
console.log("Couldn't load state")
|
|
|
|
loaded = true
|
2017-03-07 18:43:48 +11:00
|
|
|
}
|
|
|
|
})
|
2017-02-21 04:25:19 +11:00
|
|
|
|
|
|
|
subscriber(store)((mutation, state) => {
|
|
|
|
try {
|
2018-09-08 01:17:17 +10:00
|
|
|
if (saveImmedeatelyActions.includes(mutation.type)) {
|
|
|
|
setState(key, reducer(state, paths), storage)
|
|
|
|
.then(success => {
|
|
|
|
if (typeof success !== 'undefined') {
|
|
|
|
if (mutation.type === 'setOption') {
|
|
|
|
store.dispatch('settingsSaved', { success })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, error => {
|
|
|
|
if (mutation.type === 'setOption') {
|
|
|
|
store.dispatch('settingsSaved', { error })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
console.warn(`Not saving to localStorage for: ${mutation.type}`)
|
|
|
|
}
|
2017-02-21 04:25:19 +11:00
|
|
|
} catch (e) {
|
|
|
|
console.log("Couldn't persist state:")
|
|
|
|
console.log(e)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|