Skip to content

Commit

Permalink
feat: update login scenario, online connection check
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivanov N committed Dec 15, 2024
1 parent fab55b4 commit 10da015
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
12 changes: 9 additions & 3 deletions src/components/LoginForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

<script>
import { validateMnemonic } from 'bip39'
import { computed, ref, defineComponent, watch } from 'vue'
import { computed, ref, defineComponent } from 'vue'
import { useI18n } from 'vue-i18n'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
Expand Down Expand Up @@ -89,7 +89,10 @@ export default defineComponent({
})
const isOnline = computed(() => store.getters['isOnline'])
const admNodes = computed(() => store.getters['nodes/adm'])
const activeOnlineAdmNode = admNodes.value.some(node => node.active && node.online)
const submit = () => {
if (!validateMnemonic(passphrase.value)) {
return emit('error', t('login.invalid_passphrase'))
Expand All @@ -109,6 +112,8 @@ export default defineComponent({
router.push({ name: 'Nodes' })
} else if (isAxiosError(err)) {
emit('error', t('login.invalid_passphrase'))
} else if (activeOnlineAdmNode) {
router.push({ name: 'Home' })
} else if (isAllNodesOfflineError(err)) {
emit('error', t('errors.all_nodes_offline', { crypto: err.nodeLabel.toUpperCase() }))
} else if (isAllNodesDisabledError(err)) {
Expand Down Expand Up @@ -140,7 +145,8 @@ export default defineComponent({
submit,
freeze,
antiFreeze,
login
login,
admNodes
}
}
})
Expand Down
52 changes: 42 additions & 10 deletions src/store/plugins/navigatorOnline.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
import { i18n } from '@/i18n'
import axios from 'axios'

const checkUrl = 'http://ipv4.icanhazip.com'

export default (store) => {
window.addEventListener('online', handleEvent)
window.addEventListener('offline', handleEvent)

function handleEvent(event) {
store.commit('setIsOnline', event.type === 'online')
store.dispatch('snackbar/show', {
message: i18n.global.t(`connection.${event.type}`),
timeout: 3000
})
// window.addEventListener('online', handleEvent)
// window.addEventListener('offline', handleEvent)

// async function handleEvent(event) {
// if (event.type === 'online') {
// store.commit('setIsOnline', !!res)
// } else {
// store.commit('setIsOnline', false)
// }

setInterval(async () => {
const res = Boolean(await inetConnectionCheck())
const storeVal = store.getters['isOnline']
if (storeVal !== res) {
store.commit('setIsOnline', res)
const snackMsg = res ? 'online' : 'offline'
store.dispatch('snackbar/show', {
message: i18n.global.t(`connection.${snackMsg}`),
timeout: 3000
})
}
}, 1000)
}

const inetConnectionCheck = async () => {
try {
const res = await axios.get(`${checkUrl}`,
{
headers: {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'Expires': '0',
},
}
)
return res.status >=200 && res.status < 300
} catch(err) {
if (err.code === 'ERR_NETWORK') return false
}
}
}

0 comments on commit 10da015

Please sign in to comment.