From e5a40d65a608117263b9397fd001c5c5304eb0b3 Mon Sep 17 00:00:00 2001 From: Danil Kovtonyuk Date: Tue, 3 Nov 2020 22:27:23 +1000 Subject: [PATCH] save provider to localStorage --- layouts/default.vue | 13 ++++++++ store/provider/actions.js | 6 ++-- store/txStorage.js | 2 -- utillites/index.js | 1 + utillites/localStorage.js | 63 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 utillites/index.js create mode 100644 utillites/localStorage.js diff --git a/layouts/default.vue b/layouts/default.vue index 31d03a2..580297a 100644 --- a/layouts/default.vue +++ b/layouts/default.vue @@ -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']), diff --git a/store/provider/actions.js b/store/provider/actions.js index 9db12e0..42d68ed 100644 --- a/store/provider/actions.js +++ b/store/provider/actions.js @@ -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) diff --git a/store/txStorage.js b/store/txStorage.js index 6aa1bfb..930278f 100644 --- a/store/txStorage.js +++ b/store/txStorage.js @@ -17,8 +17,6 @@ export const actions = { { root: true } ) - console.info('runTxWatcher result', JSON.stringify(result)) - if (!result || !result.status) { return false } diff --git a/utillites/index.js b/utillites/index.js new file mode 100644 index 0000000..0bb0252 --- /dev/null +++ b/utillites/index.js @@ -0,0 +1 @@ +export { localStorage } from './localStorage' diff --git a/utillites/localStorage.js b/utillites/localStorage.js new file mode 100644 index 0000000..53e8619 --- /dev/null +++ b/utillites/localStorage.js @@ -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, +}