save provider to localStorage

This commit is contained in:
Danil Kovtonyuk 2020-11-03 22:27:23 +10:00
parent 34f5cbe728
commit e5a40d65a6
5 changed files with 81 additions and 4 deletions

View File

@ -18,6 +18,7 @@ import Footer from '@/components/Footer'
import Loading from '@/components/Loading'
import Notices from '@/components/Notices'
import { mapActions } from 'vuex'
import { localStorage } from '@/utillites'
export default {
components: {
@ -28,6 +29,18 @@ export default {
},
mounted() {
this.fetchDeploymentStatus()
const result = localStorage.getItem('provider')
if (result && result.name) {
this.$store.dispatch(
'provider/initProvider',
{
name: result.name,
network: result.network,
},
{ root: true }
)
}
},
methods: {
...mapActions('steps', ['fetchDeploymentStatus']),

View File

@ -1,4 +1,5 @@
import networkConfig from '@/networkConfig'
import { localStorage } from '@/utillites'
import {
SET_ACCOUNT,
SET_NETWORK,
@ -14,6 +15,8 @@ export default {
commit(SET_PROVIDER_NAME, name)
commit(SET_NETWORK_NAME, network)
localStorage.setItem('provider', { name, network })
const account = await this.$provider.initProvider(getters.getProvider)
await dispatch('checkNetworkVersion')
@ -57,8 +60,7 @@ export default {
},
clearState({ commit }) {
try {
window.localStorage.removeItem('provider')
localStorage.removeItem('provider')
commit(CLEAR_STATE)
} catch (err) {
throw new Error(err.message)

View File

@ -17,8 +17,6 @@ export const actions = {
{ root: true }
)
console.info('runTxWatcher result', JSON.stringify(result))
if (!result || !result.status) {
return false
}

1
utillites/index.js Normal file
View File

@ -0,0 +1 @@
export { localStorage } from './localStorage'

63
utillites/localStorage.js Normal file
View File

@ -0,0 +1,63 @@
let isLocalStorageEnabled = null
try {
window.localStorage.setItem('test', 'test')
window.localStorage.removeItem('test')
isLocalStorageEnabled = true
} catch (e) {
isLocalStorageEnabled = false
}
const setItem = (key, value) => {
if (isLocalStorageEnabled) {
window.localStorage.setItem(key, JSON.stringify(value))
}
}
const getItem = (key) => {
if (isLocalStorageEnabled) {
const value = window.localStorage.getItem(key)
try {
return JSON.parse(String(value))
} catch (err) {
return value
}
}
return undefined
}
const removeItem = (key) => {
if (isLocalStorageEnabled) {
return window.localStorage.removeItem(key)
}
}
const clear = () => {
if (isLocalStorageEnabled) {
window.localStorage.clear()
}
}
const subscribe = (key, originalListener) => {
const listener = (event) => {
if (event.storageArea === window.localStorage && event.key === key) {
originalListener(event.newValue, event.oldValue)
}
}
window.addEventListener('storage', listener, false)
return listener
}
const unsubscribe = (listener) => {
window.removeEventListener('storage', listener, false)
}
export const localStorage = {
setItem,
getItem,
removeItem,
clear,
subscribe,
unsubscribe,
}