Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring bridge unused & duplicated code #427

Merged
merged 4 commits into from
Aug 17, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactoring bridge unused & duplicated code
  • Loading branch information
Nikita-Polyakov committed Aug 12, 2021
commit f0654edc481bc7492713099fd95ebf4872c8213b
33 changes: 33 additions & 0 deletions src/components/mixins/PaginationSearchMixin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Component, Vue } from 'vue-property-decorator'

@Component
export default class PaginationSearchMixin extends Vue {
currentPage = 1
pageAmount = 10
query = ''

get startIndex (): number {
return (this.currentPage - 1) * this.pageAmount
}

get lastIndex (): number {
return this.currentPage * this.pageAmount
}

handlePrevClick (current: number): void {
this.currentPage = current
}

handleNextClick (current: number): void {
this.currentPage = current
}

handleResetSearch (): void {
this.query = ''
this.currentPage = 1
}

getPageItems (items: Array<any>): Array<any> {
return items.slice(this.startIndex, this.lastIndex)
}
}
14 changes: 0 additions & 14 deletions src/store/bridge.ts
Original file line number Diff line number Diff line change
@@ -39,8 +39,6 @@ const types = flow(
'SET_ASSET_ADDRESS',
'SET_ASSET_BALANCE',
'SET_AMOUNT',
'SET_SORA_TOTAL',
'SET_EVM_TOTAL',
'SET_TRANSACTION_CONFIRM',
'SET_SORA_TRANSACTION_HASH',
'SET_SORA_TRANSACTION_DATE',
@@ -295,12 +293,6 @@ const mutations = {
[types.GET_EVM_NETWORK_FEE_FAILURE] (state) {
state.evmNetworkFee = ZeroStringValue
},
[types.SET_SORA_TOTAL] (state, soraTotal: string | number) {
state.soraTotal = soraTotal
},
[types.SET_EVM_TOTAL] (state, evmTotal: string | number) {
state.evmTotal = evmTotal
},
[types.SET_TRANSACTION_CONFIRM] (state, isTransactionConfirmed: boolean) {
state.isTransactionConfirmed = isTransactionConfirmed
},
@@ -392,12 +384,6 @@ const actions = {
setEvmNetworkFee ({ commit }, evmNetworkFee: CodecString) {
commit(types.GET_EVM_NETWORK_FEE_SUCCESS, evmNetworkFee)
},
getSoraTotal ({ commit }, soraTotal: string | number) {
commit(types.SET_SORA_TOTAL, soraTotal)
},
getevmTotal ({ commit }, evmTotal: string | number) {
commit(types.SET_EVM_TOTAL, evmTotal)
},
setTransactionConfirm ({ commit }, isTransactionConfirmed: boolean) {
commit(types.SET_TRANSACTION_CONFIRM, isTransactionConfirmed)
},
1 change: 0 additions & 1 deletion src/views/Bridge.vue
Original file line number Diff line number Diff line change
@@ -249,7 +249,6 @@ export default class Bridge extends Mixins(
@Getter('evmBalance', { namespace: 'web3' }) evmBalance!: CodecString
@Getter('evmNetwork', { namespace: 'web3' }) evmNetwork!: BridgeNetworks
@Getter('subNetworks', { namespace: 'web3' }) subNetworks!: Array<SubNetwork>
@Getter('defaultNetworkType', { namespace: 'web3' }) defaultNetworkType!: string
@Getter('isTransactionConfirmed', { namespace }) isTransactionConfirmed!: boolean
@Getter('isValidNetworkType', { namespace: 'web3' }) isValidNetworkType!: boolean
@Getter('isSoraToEvm', { namespace }) isSoraToEvm!: boolean
12 changes: 4 additions & 8 deletions src/views/BridgeTransaction.vue
Original file line number Diff line number Diff line change
@@ -233,7 +233,6 @@ export default class BridgeTransaction extends Mixins(
@Action('setCurrentTransactionState', { namespace }) setCurrentTransactionState
@Action('setInitialTransactionState', { namespace }) setInitialTransactionState
@Action('setTransactionStep', { namespace }) setTransactionStep
@Action('setTransactionConfirm', { namespace }) setTransactionConfirm
@Action('setHistoryItem', { namespace }) setHistoryItem

@Action('signSoraTransactionSoraToEvm', { namespace }) signSoraTransactionSoraToEvm
@@ -267,7 +266,6 @@ export default class BridgeTransaction extends Mixins(
}

activeTransactionStep: any = [this.transactionSteps.from, this.transactionSteps.to]
currentTransactionStep = 1
showConfirmTransactionDialog = false

get formattedAmount (): string {
@@ -312,11 +310,11 @@ export default class BridgeTransaction extends Mixins(
}

get isTransactionStep1 (): boolean {
return this.currentTransactionStep === 1
return this.transactionStep === 1
}

get isTransactionStep2 (): boolean {
return this.currentTransactionStep === 2
return this.transactionStep === 2
}

get isTransactionFromPending (): boolean {
@@ -372,13 +370,13 @@ export default class BridgeTransaction extends Mixins(
if (this.isTransactionFromPending || this.isTransactionToPending) {
return this.t(
'bridgeTransaction.status.pending',
{ step: this.t('bridgeTransaction.steps.step', { step: this.t(`bridgeTransaction.steps.step${this.currentTransactionStep}`) }) }
{ step: this.t('bridgeTransaction.steps.step', { step: this.t(`bridgeTransaction.steps.step${this.transactionStep}`) }) }
)
}
if (this.isTransactionFromFailed || this.isTransactionToFailed) {
return this.t(
'bridgeTransaction.status.failed',
{ step: this.t('bridgeTransaction.steps.step', { step: this.t(`bridgeTransaction.steps.step${this.currentTransactionStep}`) }) }
{ step: this.t('bridgeTransaction.steps.step', { step: this.t(`bridgeTransaction.steps.step${this.transactionStep}`) }) }
)
}
if (this.isTransferCompleted) {
@@ -572,7 +570,6 @@ export default class BridgeTransaction extends Mixins(
}
this.initializeTransactionStateMachine()
this.isInitRequestCompleted = true
this.currentTransactionStep = this.transactionStep
const withAutoRetry = this.prevRoute !== PageNames.BridgeTransactionsHistory
await this.handleSendTransactionFrom(withAutoRetry)
}
@@ -673,7 +670,6 @@ export default class BridgeTransaction extends Mixins(
}

setFromTransactionCompleted () {
this.currentTransactionStep = 2
this.setTransactionStep(2)
this.activeTransactionStep = this.transactionSteps.to
}
41 changes: 16 additions & 25 deletions src/views/BridgeTransactionsHistory.vue
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@
<template v-if="hasHistory">
<div
class="history-item"
v-for="item in filteredHistory.slice((currentPage - 1) * pageAmount, currentPage * pageAmount)"
v-for="item in filteredHistoryItems"
:key="`history-${item.id}`"
@click="showHistory(item.id)"
>
@@ -76,12 +76,13 @@
<script lang="ts">
import { Component, Mixins } from 'vue-property-decorator'
import { Getter, Action } from 'vuex-class'
import { RegisteredAccountAsset, Operation, isBridgeOperation, BridgeHistory, CodecString, FPNumber } from '@sora-substrate/util'
import { RegisteredAccountAsset, Operation, BridgeHistory, CodecString, FPNumber } from '@sora-substrate/util'

import TranslationMixin from '@/components/mixins/TranslationMixin'
import LoadingMixin from '@/components/mixins/LoadingMixin'

import NetworkFormatterMixin from '@/components/mixins/NetworkFormatterMixin'
import PaginationSearchMixin from '@/components/mixins/PaginationSearchMixin'
import router, { lazyComponent } from '@/router'
import { Components, PageNames } from '@/consts'
import { formatAssetSymbol, formatDateItem } from '@/utils'
@@ -96,7 +97,7 @@ const namespace = 'bridge'
InfoLine: lazyComponent(Components.InfoLine)
}
})
export default class BridgeTransactionsHistory extends Mixins(TranslationMixin, LoadingMixin, NetworkFormatterMixin) {
export default class BridgeTransactionsHistory extends Mixins(TranslationMixin, LoadingMixin, NetworkFormatterMixin, PaginationSearchMixin) {
@Getter('registeredAssets', { namespace: 'assets' }) registeredAssets!: Array<RegisteredAccountAsset>
@Getter('history', { namespace }) history!: Nullable<Array<BridgeHistory>>
@Getter('restored', { namespace }) restored!: boolean
@@ -129,20 +130,26 @@ export default class BridgeTransactionsHistory extends Mixins(TranslationMixin,
PageNames = PageNames
formatAssetSymbol = formatAssetSymbol
formatDateItem = formatDateItem
query = ''
currentPage = 1
pageAmount = 8
pageAmount = 8 // override PaginationSearchMixin

get filteredHistory (): Array<BridgeHistory> {
if (!this.history?.length) return []
const historyCopy = this.history.sort((a: BridgeHistory, b: BridgeHistory) => a.startTime && b.startTime ? b.startTime - a.startTime : 0)
return this.getFilteredHistory(historyCopy.filter(item => (isBridgeOperation(item.type) && item.transactionStep)))

const historyCopy = this.history
.filter(item => !!item.transactionStep)
.sort((a: BridgeHistory, b: BridgeHistory) => a.startTime && b.startTime ? b.startTime - a.startTime : 0)

return this.getFilteredHistory(historyCopy)
}

get hasHistory (): boolean {
return this.filteredHistory && this.filteredHistory.length > 0
}

get filteredHistoryItems (): Array<BridgeHistory> {
return this.getPageItems(this.filteredHistory)
}

async created (): Promise<void> {
this.withApi(async () => {
await this.updateRegisteredAssets()
@@ -190,10 +197,7 @@ export default class BridgeTransactionsHistory extends Mixins(TranslationMixin,
}

isOutgoingType (type: string | undefined): boolean {
if (type) {
return type !== Operation.EthBridgeIncoming
}
return true
return type !== Operation.EthBridgeIncoming
}

async showHistory (id: string): Promise<void> {
@@ -233,23 +237,10 @@ export default class BridgeTransactionsHistory extends Mixins(TranslationMixin,
})
}

handlePrevClick (current: number): void {
this.currentPage = current
}

handleNextClick (current: number): void {
this.currentPage = current
}

async handleClearHistory (): Promise<void> {
await this.clearHistory()
}

handleResetSearch (): void {
this.query = ''
this.currentPage = 1
}

async handleRestoreHistory (): Promise<void> {
await this.withLoading(async () => {
await this.getRestoredHistory()
30 changes: 4 additions & 26 deletions src/views/Tokens.vue
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
size="big"
>
<template #suffix v-if="query">
<s-button type="link" class="s-button--clear" icon="clear-X-16" @click="handleClearSearch" />
<s-button type="link" class="s-button--clear" icon="clear-X-16" @click="handleResetSearch" />
</template>
</s-input>
<s-table
@@ -87,6 +87,7 @@ import { lazyComponent } from '@/router'
import LoadingMixin from '@/components/mixins/LoadingMixin'
import TranslationMixin from '@/components/mixins/TranslationMixin'
import AssetsSearchMixin from '@/components/mixins/AssetsSearchMixin'
import PaginationSearchMixin from '@/components/mixins/PaginationSearchMixin'
import SortButton from '@/components/SortButton.vue'

@Component({
@@ -97,12 +98,9 @@ import SortButton from '@/components/SortButton.vue'
SortButton
}
})
export default class Tokens extends Mixins(LoadingMixin, TranslationMixin, AssetsSearchMixin) {
export default class Tokens extends Mixins(LoadingMixin, TranslationMixin, AssetsSearchMixin, PaginationSearchMixin) {
@Getter('whitelistAssets', { namespace: 'assets' }) items!: Array<Asset>

currentPage = 1
pageAmount = 10
query = ''
order = ''
property = ''

@@ -114,14 +112,6 @@ export default class Tokens extends Mixins(LoadingMixin, TranslationMixin, Asset
return !this.order || !this.property
}

get startIndex (): number {
return (this.currentPage - 1) * this.pageAmount
}

get lastIndex (): number {
return this.currentPage * this.pageAmount
}

get filteredItems (): Array<Asset> {
return this.filterAssetsByQuery(this.items)(this.query) as Array<Asset>
}
@@ -142,19 +132,7 @@ export default class Tokens extends Mixins(LoadingMixin, TranslationMixin, Asset
}

get tableItems (): Array<Asset> {
return this.sortedItems.slice(this.startIndex, this.lastIndex)
}

handlePrevClick (current: number): void {
this.currentPage = current
}

handleNextClick (current: number): void {
this.currentPage = current
}

handleClearSearch (): void {
this.query = ''
return this.getPageItems(this.sortedItems)
}

changeSort ({ order = '', property = '' } = {}): void {