add working community switch to send coins

This commit is contained in:
einhornimmond 2023-08-29 11:12:43 +02:00
parent 4656884b71
commit 354af5864d
9 changed files with 98 additions and 14 deletions

View File

@ -1,5 +1,5 @@
import { Decimal } from 'decimal.js-light'
import { ArgsType, Field } from 'type-graphql'
import { ArgsType, Field, Int } from 'type-graphql'
@ArgsType()
export class TransactionSendArgs {
@ -11,4 +11,7 @@ export class TransactionSendArgs {
@Field(() => String)
memo: string
@Field(() => Int)
targetCommunity: number
}

View File

@ -26,7 +26,7 @@ export class CommunityResolver {
@Authorized([RIGHTS.COMMUNITIES])
@Query(() => [Community])
async getCommunitySelections(): Promise<Community[]> {
async communities(): Promise<Community[]> {
const dbCommunities: DbCommunity[] = await DbCommunity.find({
order: {
name: 'ASC',

View File

@ -317,7 +317,7 @@ export class TransactionResolver {
@Authorized([RIGHTS.SEND_COINS])
@Mutation(() => Boolean)
async sendCoins(
@Args() { identifier, amount, memo }: TransactionSendArgs,
@Args() { identifier, amount, memo, targetCommunity }: TransactionSendArgs,
@Ctx() context: Context,
): Promise<boolean> {
logger.info(`sendCoins(identifier=${identifier}, amount=${amount}, memo=${memo})`)

View File

@ -0,0 +1,58 @@
<template>
<div class="community-switch">
<b-dropdown no-flip :text="value.name">
<b-dropdown-item
v-for="community in communities"
@click.prevent="updateCommunity(community)"
:key="community.id"
:title="community.description"
:active="value.id === community.id"
>
{{ community.name }}
</b-dropdown-item>
</b-dropdown>
</div>
</template>
<script>
import { selectCommunities } from '@/graphql/queries'
import { COMMUNITY_NAME } from '@/config'
export default {
name: 'CommunitySwitch',
props: {
value: { type: Object, default: { id: 0, name: COMMUNITY_NAME } },
},
data() {
return {
communities: [],
}
},
methods: {
updateCommunity(community) {
this.value = community
this.$emit('input', this.value)
},
setDefaultCommunity() {
// set default community, the only one which isn't foreign
// we assume it is only one entry with foreign = false
if (!this.value.id && this.communities.length) {
const foundCommunity = this.communities.find((community) => !community.foreign)
if (foundCommunity) {
this.updateCommunity(foundCommunity)
}
}
},
},
apollo: {
communities: {
query: selectCommunities,
},
},
mounted() {
this.setDefaultCommunity()
},
updated() {
this.setDefaultCommunity()
},
}
</script>

View File

@ -6,7 +6,7 @@
<b-col cols="12">
<b-row class="mt-3">
<b-col class="h5">{{ $t('form.recipientCommunity') }}</b-col>
<b-col>{{ communityName }}</b-col>
<b-col>{{ targetCommunity.name }}</b-col>
</b-row>
<b-row>
<b-col class="h5">{{ $t('form.recipient') }}</b-col>
@ -76,11 +76,11 @@ export default {
amount: { type: Number, required: true },
memo: { type: String, required: true },
userName: { type: String, default: '' },
targetCommunity: { type: Object, default: { id: 0, name: COMMUNITY_NAME }},
},
data() {
return {
disabled: false,
communityName: COMMUNITY_NAME,
}
},
}

View File

@ -54,7 +54,12 @@
<b-col>{{ $t('form.recipientCommunity') }}</b-col>
</b-row>
<b-row>
<b-col class="font-weight-bold">{{ communityName }}</b-col>
<b-col class="font-weight-bold">
<community-switch
v-model="form.targetCommunity"
:disabled="isBalanceDisabled"
/>
</b-col>
</b-row>
</b-col>
<b-col cols="12" v-if="radioSelected === sendTypes.send">
@ -137,6 +142,7 @@ import { SEND_TYPES } from '@/pages/Send'
import InputIdentifier from '@/components/Inputs/InputIdentifier'
import InputAmount from '@/components/Inputs/InputAmount'
import InputTextarea from '@/components/Inputs/InputTextarea'
import CommunitySwitch from '@/components/CommunitySwitch.vue'
import { user as userQuery } from '@/graphql/queries'
import { isEmpty } from 'lodash'
import { COMMUNITY_NAME } from '@/config'
@ -147,6 +153,7 @@ export default {
InputIdentifier,
InputAmount,
InputTextarea,
CommunitySwitch,
},
props: {
balance: { type: Number, default: 0 },
@ -154,6 +161,7 @@ export default {
amount: { type: Number, default: 0 },
memo: { type: String, default: '' },
selected: { type: String, default: 'send' },
targetCommunity: { type: Object, default: { id: 0, name: COMMUNITY_NAME } },
},
data() {
return {
@ -161,10 +169,10 @@ export default {
identifier: this.identifier,
amount: this.amount ? String(this.amount) : '',
memo: this.memo,
targetCommunity: this.targetCommunity,
},
radioSelected: this.selected,
userName: '',
communityName: COMMUNITY_NAME,
userName: ''
}
},
methods: {
@ -179,6 +187,7 @@ export default {
amount: Number(this.form.amount.replace(',', '.')),
memo: this.form.memo,
userName: this.userName,
targetCommunity: this.form.targetCommunity,
})
},
onReset(event) {
@ -186,6 +195,7 @@ export default {
this.form.identifier = ''
this.form.amount = ''
this.form.memo = ''
this.form.targetCommunity = { id: 0, name: COMMUNITY_NAME }
this.$refs.formValidator.validate()
if (this.$route.query && !isEmpty(this.$route.query))
this.$router.replace({ query: undefined })

View File

@ -71,8 +71,18 @@ export const createUser = gql`
`
export const sendCoins = gql`
mutation($identifier: String!, $amount: Decimal!, $memo: String!) {
sendCoins(identifier: $identifier, amount: $amount, memo: $memo)
mutation(
$identifier: String!
$amount: Decimal!
$memo: String!
$targetCommunity: Int!
) {
sendCoins(
identifier: $identifier,
amount: $amount,
memo: $memo,
targetCommunity: $targetCommunity
)
}
`

View File

@ -72,14 +72,13 @@ export const listGDTEntriesQuery = gql`
}
`
export const communities = gql`
export const selectCommunities = gql`
query {
communities {
id
name
url
description
registerUrl
foreign
}
}
`

View File

@ -122,7 +122,11 @@ export default {
this.$apollo
.mutate({
mutation: sendCoins,
variables: this.transactionData,
variables: {
...this.transactionData,
// from target community we need only the id
targetCommunity: this.transactionData.targetCommunity.id,
},
})
.then(() => {
this.error = false