diff --git a/src/layouts/components/Navbar.vue b/src/layouts/components/Navbar.vue index 7dfbcf56..f7fd0ca7 100644 --- a/src/layouts/components/Navbar.vue +++ b/src/layouts/components/Navbar.vue @@ -54,7 +54,6 @@ {{ currentApi }} ({{ selected_chain.sdk_version || '-' }}) this.apiOptions.length ? this.apiOptions[0] : this.apiOptions[this.index] }, apiOptions() { - const conf = store.state.chains.selected + const conf = this.$store.state.chains.selected if (Array.isArray(conf.api)) { return conf.api } @@ -237,16 +233,19 @@ export default { methods: { change(v) { this.index = v - const conf = store.state.chains.selected + const conf = this.$store.state.chains.selected localStorage.setItem(`${conf.chain_name}-api-index`, v) }, block() { - store.commit('setHeight', 0) + const conf = this.$store.state.chains.selected + const s = localStorage.getItem(`${conf.chain_name}-api-index`) || 0 + this.index = Number(s) + this.$store.commit('setHeight', 0) this.$http.getLatestBlock().then(block => { - store.commit('setHeight', Number(block.block.header.height)) + this.$store.commit('setHeight', Number(block.block.header.height)) if (timeIn(block.block.header.time, 1, 'm')) { this.variant = 'danger' - this.tips = `Halted ${toDay(block.block.header.time, 'from')}, Height: ${store.state.chains.height} ` + this.tips = `Halted ${toDay(block.block.header.time, 'from')}, Height: ${this.$store.state.chains.height} ` } else { this.variant = 'success' this.tips = 'Synced' diff --git a/src/libs/fetch.js b/src/libs/fetch.js index da74a323..f94e6b22 100644 --- a/src/libs/fetch.js +++ b/src/libs/fetch.js @@ -251,33 +251,6 @@ export default class ChainFetch { }) } - async get(url, config = null) { - let host = '' - if (!config) { - this.getSelectedConfig() - } - host = (config ? config.api : this.config.api) - const finalurl = (Array.isArray(host) ? host[this.getApiIndex(config)] : host) + url - // finalurl = finalurl.replaceAll('v1beta1', this.getEndpointVersion()) - const ret = await fetch(finalurl).then(response => response.json()) - return ret - } - - getApiIndex(config = null) { - const conf = config || this.config - return localStorage.getItem(`${conf.chain_name}-api-index`) || 0 - } - - async getUrl(url) { - this.getSelectedConfig() - return fetch(url).then(res => res.json()) - } - - static fetch(host, url) { - const ret = fetch((Array.isArray(host) ? host[0] : host) + url).then(response => response.json()) - return ret - } - async getAuthAccount(address, config = null) { return this.get('/auth/accounts/'.concat(address), config).then(data => { const result = commonProcess(data) @@ -420,9 +393,9 @@ export default class ChainFetch { this.getSelectedConfig() } const conf = config || this.config - const index = localStorage.getItem(`${conf.chain_name}-api-index`) || 0 + const index = this.getApiIndex(config) // Default options are marked with * - const response = await fetch((Array.isArray(conf.api) ? conf.api[Number(index)] : conf.api) + url, { + const response = await fetch((Array.isArray(conf.api) ? conf.api[index] : conf.api) + url, { method: 'POST', // *GET, POST, PUT, DELETE, etc. // mode: 'cors', // no-cors, *cors, same-origin // credentials: 'same-origin', // redirect: 'follow', // manual, *follow, error @@ -437,6 +410,33 @@ export default class ChainFetch { // const response = axios.post((config ? config.api : this.config.api) + url, data) return response.json() // parses JSON response into native JavaScript objects } + + async get(url, config = null) { + if (!config) { + this.getSelectedConfig() + } + const conf = config || this.config + const finalurl = (Array.isArray(conf.api) ? conf.api[this.getApiIndex(config)] : conf.api) + url + // finalurl = finalurl.replaceAll('v1beta1', this.getEndpointVersion()) + const ret = await fetch(finalurl).then(response => response.json()) + return ret + } + + getApiIndex(config = null) { + const conf = config || this.config + const index = Number(localStorage.getItem(`${conf.chain_name}-api-index`) || 0) + return index < conf.api.length ? index : 0 + } + + async getUrl(url) { + this.getSelectedConfig() + return fetch(url).then(res => res.json()) + } + + static fetch(host, url) { + const ret = fetch((Array.isArray(host) ? host[0] : host) + url).then(response => response.json()) + return ret + } } // export default chainAPI