Skip to content

Commit

Permalink
Merge pull request #677 from Adamant-im/feat/update-login-scenario
Browse files Browse the repository at this point in the history
feat: update login scenario
  • Loading branch information
bludnic authored Dec 23, 2024
2 parents 4ea7c6f + 2dfa540 commit fb47c2f
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 52 deletions.
14 changes: 7 additions & 7 deletions src/components/LoginForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,26 @@ export default defineComponent({
}
})
const isOnline = computed(() => store.getters['isOnline'])
const submit = () => {
if (!validateMnemonic(passphrase.value)) {
return emit('error', t('login.invalid_passphrase'))
}
freeze()
login()
}
const login = () => {
const promise = store.dispatch('login', passphrase.value)
promise
.then(() => {
emit('login')
})
.catch((err) => {
if (isAxiosError(err)) {
if (!isOnline.value) {
emit('error', t('connection.offline'))
router.push({ name: 'Nodes' })
} else if (isAxiosError(err)) {
emit('error', t('login.invalid_passphrase'))
} else if (isAllNodesOfflineError(err)) {
emit('error', t('errors.all_nodes_offline', { crypto: err.nodeLabel.toUpperCase() }))
Expand Down Expand Up @@ -140,10 +143,7 @@ export default defineComponent({
showPassphrase,
classes,
togglePassphraseVisibility,
submit,
freeze,
antiFreeze,
login
submit
}
}
})
Expand Down
30 changes: 27 additions & 3 deletions src/components/LoginPasswordForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@
</template>

<script>
import { clearDb } from '@/lib/idb'
import { isAxiosError } from 'axios'
import { computed, defineComponent, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { useI18n } from 'vue-i18n'
import { clearDb } from '@/lib/idb'
import { isAllNodesDisabledError, isAllNodesOfflineError } from '@/lib/nodes/utils/errors'
export default defineComponent({
props: {
Expand All @@ -55,7 +60,9 @@ export default defineComponent({
},
emits: ['login', 'error', 'update:modelValue'],
setup(props, { emit }) {
const router = useRouter()
const store = useStore()
const { t } = useI18n()
const passwordField = ref(null)
const showSpinner = ref(false)
Expand All @@ -68,6 +75,8 @@ export default defineComponent({
}
})
const isOnline = computed(() => store.getters['isOnline'])
const submit = () => {
showSpinner.value = true
Expand All @@ -76,8 +85,23 @@ export default defineComponent({
.then(() => {
emit('login')
})
.catch(() => {
emit('error', 'login_via_password.incorrect_password')
.catch((err) => {
if (!isOnline.value) {
emit('error', t('connection.offline'))
router.push({ name: 'Nodes' })
} else if (err?.message === 'Invalid password') {
emit('error', t('login_via_password.incorrect_password'))
} else if (isAxiosError(err)) {
emit('error', t('login.invalid_passphrase'))
} else if (isAllNodesOfflineError(err)) {
emit('error', t('errors.all_nodes_offline', { crypto: err.nodeLabel.toUpperCase() }))
} else if (isAllNodesDisabledError(err)) {
emit('error', t('errors.all_nodes_disabled', { crypto: err.nodeLabel.toUpperCase() }))
router.push({ name: 'Nodes' })
} else {
emit('error', t('errors.something_went_wrong'))
}
console.log(err)
})
.finally(() => {
showSpinner.value = false
Expand Down
2 changes: 1 addition & 1 deletion src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"error": "Error",
"errors": {
"something_went_wrong": "Something went wrong. Check the console for details",
"all_nodes_offline": "All {crypto} nodes are offline. Try again later",
"all_nodes_offline": "No online {crypto} nodes. Review the node list",
"all_nodes_disabled": "No active {crypto} nodes. Enable at least one"
},
"home": {
Expand Down
4 changes: 2 additions & 2 deletions src/locales/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@
"error": "Ошибка",
"errors": {
"something_went_wrong": "Что-то пошло не так. Детали ошибки в консоли",
"all_nodes_offline": "Все {crypto} ноды недоступны. Попробуйте позже",
"all_nodes_disabled": "Нет активных узлов {crypto}. Включите хотя бы один"
"all_nodes_offline": "Все {crypto} ноды недоступны. Посмотрите список узлов",
"all_nodes_disabled": "Нет активных {crypto} нод. Включите хотя бы одну"
},
"home": {
"balance": "Баланс",
Expand Down
5 changes: 2 additions & 3 deletions src/store/modules/btc-base/btc-base-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ function createActions(options) {
commit('setBalanceStatus', FetchStatus.Success)
} catch (err) {
commit('setBalanceStatus', FetchStatus.Error)

throw err
console.warn(err)
}
}
},
Expand All @@ -112,7 +111,7 @@ function createActions(options) {
})
.catch((err) => {
context.commit('setBalanceStatus', FetchStatus.Error)
throw err
console.warn(err)
})
},

Expand Down
18 changes: 14 additions & 4 deletions src/store/modules/btc/btc-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ const customActions = (getApi) => ({
})
.catch((err) => {
context.commit('setBalanceStatus', FetchStatus.Error)
throw err
console.warn(err)
})

// The unspent transactions are needed to estimate the fee
btcIndexer.getUnspents(context.state.address).then((utxo) => context.commit('utxo', utxo))
btcIndexer
.getUnspents(context.state.address)
.then((utxo) => context.commit('utxo', utxo))
.catch((err) => console.warn(err))

// The estimated fee rate is also needed
btcIndexer.getFeeRate().then((rate) => context.commit('feeRate', rate['2']))
btcIndexer
.getFeeRate()
.then((rate) => context.commit('feeRate', rate['2']))
.catch((err) => console.warn(err))

// Last block height
context.dispatch('updateHeight')
Expand All @@ -34,7 +40,11 @@ const customActions = (getApi) => ({
updateHeight({ commit }) {
const api = getApi()
if (!api) return
btcIndexer.getHeight().then((height) => commit('height', height))

btcIndexer
.getHeight()
.then((height) => commit('height', height))
.catch((err) => console.warn(err))
}
})

Expand Down
58 changes: 31 additions & 27 deletions src/store/modules/erc20/erc20-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const createSpecificActions = (api) => ({
commit('setBalanceStatus', FetchStatus.Success)
} catch (err) {
commit('setBalanceStatus', FetchStatus.Error)
console.log(err)
console.warn(err)
}
}
},
Expand All @@ -74,33 +74,37 @@ const createSpecificActions = (api) => ({
return
}

const contract = new EthContract(Erc20, context.state.contractAddress)
contract.setProvider(api.getClient().provider)

contract.methods
.balanceOf(context.state.address)
.call()
.then(
(balance) => {
context.commit(
'balance',
Number(ethUtils.toFraction(balance.toString(10), context.state.decimals))
)
context.commit('setBalanceStatus', FetchStatus.Success)
},
() => {
context.commit('setBalanceStatus', FetchStatus.Error)
}
)
.then(() => {
const delay = Math.max(0, STATUS_INTERVAL - Date.now() + lastStatusUpdate)
setTimeout(() => {
if (context.state.address) {
lastStatusUpdate = Date.now()
context.dispatch('updateStatus')
try {
const contract = new EthContract(Erc20, context.state.contractAddress)
contract.setProvider(api.getClient().provider)

contract.methods
.balanceOf(context.state.address)
.call()
.then(
(balance) => {
context.commit(
'balance',
Number(ethUtils.toFraction(balance.toString(10), context.state.decimals))
)
context.commit('setBalanceStatus', FetchStatus.Success)
},
() => {
context.commit('setBalanceStatus', FetchStatus.Error)
}
}, delay)
})
)
.then(() => {
const delay = Math.max(0, STATUS_INTERVAL - Date.now() + lastStatusUpdate)
setTimeout(() => {
if (context.state.address) {
lastStatusUpdate = Date.now()
context.dispatch('updateStatus')
}
}, delay)
})
} catch (err) {
console.warn(err)
}
}
})

Expand Down
5 changes: 4 additions & 1 deletion src/store/modules/eth/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const createSpecificActions = (api) => ({
commit('setBalanceStatus', FetchStatus.Success)
} catch (err) {
commit('setBalanceStatus', FetchStatus.Error)
console.log(err)
console.warn(err)
}
}
},
Expand All @@ -86,6 +86,7 @@ const createSpecificActions = (api) => ({
context.commit('balance', Number(utils.toEther(balance.toString())))
context.commit('setBalanceStatus', FetchStatus.Success)
})
.catch((err) => console.warn(err))

// Current gas price
void api
Expand All @@ -96,13 +97,15 @@ const createSpecificActions = (api) => ({
fee: +(+utils.calculateFee(DEFAULT_ETH_TRANSFER_GAS_LIMIT, price)).toFixed(8)
})
})
.catch((err) => console.warn(err))

// Current block number
void api
.useClient((client) => client.getBlockNumber())
.then((number) => {
context.commit('blockNumber', Number(number))
})
.catch((err) => console.warn(err))

const delay = Math.max(0, STATUS_INTERVAL - Date.now() + lastStatusUpdate)
setTimeout(() => {
Expand Down
12 changes: 8 additions & 4 deletions src/store/modules/kly/kly-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const customActions = (getAccount) => ({
commit('setBalanceStatus', FetchStatus.Success)
} catch (err) {
commit('setBalanceStatus', FetchStatus.Error)
console.log(err)
console.warn(err)
}
}
},
Expand All @@ -45,17 +45,21 @@ const customActions = (getAccount) => ({
} catch (err) {
commit('setBalanceStatus', FetchStatus.Error)

throw err
console.warn(err)
}

// Last block height
dispatch('updateHeight')
},

async updateHeight({ commit }) {
const height = await kly.getHeight()
try {
const height = await kly.getHeight()

commit('height', height)
commit('height', height)
} catch (err) {
console.warn(err)
}
},

/**
Expand Down

0 comments on commit fb47c2f

Please sign in to comment.