Skip to content

Commit

Permalink
feat: use vue-query for DASH and DOGE
Browse files Browse the repository at this point in the history
  • Loading branch information
bludnic committed Jul 12, 2024
1 parent d64aa8a commit 36ad5b1
Show file tree
Hide file tree
Showing 6 changed files with 455 additions and 32 deletions.
81 changes: 51 additions & 30 deletions src/components/transactions/BtcTransaction.vue
Original file line number Diff line number Diff line change
@@ -1,55 +1,78 @@
<template>
<div>
<transaction-template
:id="transaction.hash || ''"
:amount="currency(transaction.amount, crypto)"
:timestamp="transaction.timestamp || NaN"
:fee="fee"
:confirmations="confirmations || NaN"
:sender="sender || ''"
:recipient="recipient || ''"
:sender-formatted="senderFormatted || ''"
:recipient-formatted="recipientFormatted || ''"
:explorer-link="explorerLink"
:partner="partnerAdmAddress || ''"
:status="status"
:adm-tx="admTx"
:crypto="crypto"
/>
</div>
<transaction-template
v-if="transaction"
:id="transaction.hash || ''"
:amount="currency(transaction.amount, crypto)"
:timestamp="transaction.timestamp || NaN"
:fee="fee"
:confirmations="confirmations || NaN"
:sender="sender || ''"
:recipient="recipient || ''"
:sender-formatted="senderFormatted || ''"
:recipient-formatted="recipientFormatted || ''"
:explorer-link="explorerLink"
:partner="partnerAdmAddress || ''"
:status="{ status, virtualStatus: status }"
:adm-tx="admTx"
:crypto="crypto"
/>

<transaction-template-loading v-else :explorer-link="explorerLink" :crypto="crypto" />
</template>

<script>
import { computed, defineComponent } from 'vue'
<script lang="ts">
import { computed, defineComponent, PropType } from 'vue'
import { useI18n } from 'vue-i18n'
import { useStore } from 'vuex'
import { useChatName } from '@/components/AChat/hooks/useChatName'
import { useFindAdmAddress } from '@/hooks/address/useFindAdmAddress'
import { usePartnerCryptoAddress } from '@/hooks/address/usePartnerCryptoAddress'
import { useTransactionStatus } from '@/hooks/useTransactionStatus'
import { formatCryptoAddress, formatMultipleBTCAddresses } from '@/utils/address'
import TransactionTemplate from './TransactionTemplate.vue'
import TransactionTemplateLoading from './TransactionTemplateLoading.vue'
import { getExplorerTxUrl } from '@/config/utils'
import { CryptosInfo } from '@/lib/constants'
import { CryptosInfo, CryptoSymbol, TransactionStatus } from '@/lib/constants'
import { useFindAdmTransaction } from '@/hooks/address'
import { useBtcTransferQuery } from '@/hooks/queries/useBtcTransferQuery'
import currency from '@/filters/currencyAmountWithSymbol'
export default defineComponent({
components: {
TransactionTemplate
TransactionTemplate,
TransactionTemplateLoading
},
props: {
crypto: {
required: true,
type: String as PropType<CryptoSymbol>
},
id: {
required: true,
type: String
}
},
props: ['id', 'crypto'],
setup(props) {
const store = useStore()
const { t } = useI18n()
const cryptoKey = computed(() => props.crypto.toLowerCase())
const transaction = computed(
() => store.getters[`${cryptoKey.value}/transaction`](props.id) || {}
)
const cryptoAddress = computed(() => store.state[cryptoKey.value].address)
const {
isFetching,
isError,
isSuccess,
data: transaction
} = useBtcTransferQuery(props.id, props.crypto)
const status = computed(() => {
if (isFetching.value) return TransactionStatus.PENDING
if (isError.value) return TransactionStatus.REJECTED
if (isSuccess.value) return TransactionStatus.CONFIRMED
return TransactionStatus.PENDING
})
const cryptoAddress = computed(() => store.state.btc.address)
const partnerCryptoAddress = usePartnerCryptoAddress(
cryptoAddress,
transaction.value.senderId,
Expand Down Expand Up @@ -153,8 +176,6 @@ export default defineComponent({
const admTx = useFindAdmTransaction(props.id)
const { status } = useTransactionStatus(admTx, transaction)
return {
transaction,
fee,
Expand Down
181 changes: 181 additions & 0 deletions src/components/transactions/DashTransaction.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<template>
<transaction-template
v-if="transaction"
:id="transaction.hash || ''"
:amount="currency(transaction.amount, crypto)"
:timestamp="transaction.timestamp || NaN"
:fee="fee"
:confirmations="confirmations || NaN"
:sender="sender || ''"
:recipient="recipient || ''"
:sender-formatted="senderFormatted || ''"
:recipient-formatted="recipientFormatted || ''"
:explorer-link="explorerLink"
:partner="partnerAdmAddress || ''"
:status="{ status, virtualStatus: status }"
:adm-tx="admTx"
:crypto="crypto"
/>

<transaction-template-loading v-else :explorer-link="explorerLink" :crypto="crypto" />
</template>

<script lang="ts">
import { computed, defineComponent, PropType } from 'vue'
import { useI18n } from 'vue-i18n'
import { useStore } from 'vuex'
import { useChatName } from '@/components/AChat/hooks/useChatName'
import { useFindAdmAddress } from '@/hooks/address/useFindAdmAddress'
import { usePartnerCryptoAddress } from '@/hooks/address/usePartnerCryptoAddress'
import { formatCryptoAddress, formatMultipleBTCAddresses } from '@/utils/address'
import TransactionTemplate from './TransactionTemplate.vue'
import TransactionTemplateLoading from './TransactionTemplateLoading.vue'
import { getExplorerTxUrl } from '@/config/utils'
import { CryptosInfo, CryptoSymbol, TransactionStatus } from '@/lib/constants'
import { useFindAdmTransaction } from '@/hooks/address'
import { useDashTransferQuery } from '@/hooks/queries/useDashTransferQuery'
import currency from '@/filters/currencyAmountWithSymbol'
export default defineComponent({
components: {
TransactionTemplate,
TransactionTemplateLoading
},
props: {
crypto: {
required: true,
type: String as PropType<CryptoSymbol>
},
id: {
required: true,
type: String
}
},
setup(props) {
const store = useStore()
const { t } = useI18n()
const cryptoKey = computed(() => props.crypto.toLowerCase())
const {
isFetching,
isError,
isSuccess,
data: transaction
} = useDashTransferQuery(props.id, props.crypto)
const status = computed(() => {
if (isFetching.value) return TransactionStatus.PENDING
if (isError.value) return TransactionStatus.REJECTED
if (isSuccess.value) return TransactionStatus.CONFIRMED
return TransactionStatus.PENDING
})
const cryptoAddress = computed(() => store.state.dash.address)
const partnerCryptoAddress = usePartnerCryptoAddress(
cryptoAddress,
transaction.value.senderId,
transaction.value.recipientId
)
const partnerAdmAddress = useFindAdmAddress(cryptoKey, partnerCryptoAddress, props.id)
const senderAdmAddress = useFindAdmAddress(cryptoKey, transaction.value.senderId, props.id)
const recipientAdmAddress = useFindAdmAddress(
cryptoKey,
transaction.value.recipientId,
props.id
)
const senderName = useChatName(senderAdmAddress.value)
const recipientName = useChatName(recipientAdmAddress.value)
const fee = computed(() => {
const fee = transaction.value.fee
if (!fee) return ''
return `${+fee.toFixed(CryptosInfo[props.crypto].decimals)} ${props.crypto.toUpperCase()}`
})
const sender = computed(() => {
const { senders, senderId } = transaction.value
const onlySender = senderId && (!senders || senders.length === 1)
if (onlySender) {
return senderId
} else if (senders) {
return senders.join(', ')
} else {
return undefined
}
})
const recipient = computed(() => {
const { recipientId, recipients } = transaction.value
const onlyRecipient = recipientId && (!recipients || recipients.length === 1)
if (onlyRecipient) {
return recipientId
} else if (recipients) {
return recipients.join(', ')
} else {
return undefined
}
})
const senderFormatted = computed(() => {
const { senders, senderId } = transaction.value
const onlySender = senderId && (!senders || senders.length === 1)
if (onlySender) {
return formatCryptoAddress(
senderId,
cryptoAddress.value,
t,
senderAdmAddress.value,
senderName.value
)
} else if (senders) {
return formatMultipleBTCAddresses(senders, cryptoAddress.value, t)
} else {
return undefined
}
})
const recipientFormatted = computed(() => {
const { recipientId, recipients } = transaction.value
const onlyRecipient = recipientId && (!recipients || recipients.length === 1)
if (onlyRecipient) {
return formatCryptoAddress(
recipientId,
cryptoAddress.value,
t,
recipientAdmAddress.value,
recipientName.value
)
} else if (recipients) {
return formatMultipleBTCAddresses(recipients, cryptoAddress.value, t)
} else {
return undefined
}
})
const explorerLink = computed(() => getExplorerTxUrl(props.crypto, props.id))
const confirmations = computed(() => transaction.value.confirmations)
const admTx = useFindAdmTransaction(props.id)
return {
transaction,
fee,
sender,
recipient,
senderFormatted,
recipientFormatted,
partnerAdmAddress,
explorerLink,
confirmations,
admTx,
status,
currency
}
}
})
</script>
Loading

0 comments on commit 36ad5b1

Please sign in to comment.