Improve subscription setup UI

This commit is contained in:
silverpill 2022-06-30 13:11:19 +00:00
parent 5c1cbe8405
commit 8835669772
2 changed files with 70 additions and 21 deletions

View file

@ -379,10 +379,6 @@ async function onCancelSubscription() {
color: $error-color; color: $error-color;
} }
.loader {
margin: 0 auto;
}
.info { .info {
background-color: $block-background-color; background-color: $block-background-color;
border-radius: $block-border-radius; border-radius: $block-border-radius;
@ -451,4 +447,8 @@ async function onCancelSubscription() {
gap: $block-inner-padding; gap: $block-inner-padding;
} }
} }
.loader {
margin: 0 auto;
}
</style> </style>

View file

@ -9,10 +9,11 @@
</div> </div>
<div class="info" v-if="subscriptionConfigured !== null"> <div class="info" v-if="subscriptionConfigured !== null">
<template v-if="subscription"> <template v-if="subscription">
<div>Recipient address: {{ subscription.recipientAddress }}</div> <span>Subscription is enabled</span>
<div>Token address: {{ subscription.tokenAddress }}</div> <div class="price">
<div>Token symbol: {{ subscription.tokenSymbol }}</div> {{ subscription.pricePerMonth }} {{ subscription.tokenSymbol }}
<div>Price of one month: {{ subscription.pricePerMonth }}</div> <span class="price-subtext">per month</span>
</div>
</template> </template>
<template v-else> <template v-else>
Subscription is not configured Subscription is not configured
@ -32,13 +33,23 @@
Set up subscription Set up subscription
</button> </button>
</form> </form>
<div class="withdraw" v-if="subscription !== null"> <form class="withdraw" v-if="subscription !== null">
<input v-model="subscriberAddress" placeholder="Subscriber address"> <input v-model="subscriberAddress" placeholder="Subscriber address">
<button class="btn" @click="onCheckSubsciptionState()">Check</button> <button
<button class="btn" v-if="subscriptionState !== null" @click="onWithdrawReceived()"> class="btn"
@click.prevent="onCheckSubsciptionState()"
>
Check
</button>
<button
class="btn"
v-if="subscriptionState !== null"
@click.prevent="onWithdrawReceived()"
>
Withdraw {{ subscription.formatAmount(subscriptionState.recipientBalance) }} {{ subscription.tokenSymbol }} Withdraw {{ subscription.formatAmount(subscriptionState.recipientBalance) }} {{ subscription.tokenSymbol }}
</button> </button>
</div> </form>
<loader v-if="isLoading"></loader>
</div> </div>
</template> </template>
@ -59,6 +70,7 @@ import {
SubscriptionState, SubscriptionState,
SubscriptionToken, SubscriptionToken,
} from "@/api/subscriptions" } from "@/api/subscriptions"
import Loader from "@/components/Loader.vue"
import { useWallet } from "@/composables/wallet" import { useWallet } from "@/composables/wallet"
import { useInstanceInfo } from "@/store/instance" import { useInstanceInfo } from "@/store/instance"
import { useCurrentUser } from "@/store/user" import { useCurrentUser } from "@/store/user"
@ -75,6 +87,7 @@ const { connectWallet: connectEthereumWallet, getSigner } = useWallet()
const profileEthereumAddress = getVerifiedEthereumAddress(props.profile) const profileEthereumAddress = getVerifiedEthereumAddress(props.profile)
const subscriptionPrice = $ref<number>(1) const subscriptionPrice = $ref<number>(1)
let { walletAddress, walletError } = $(useWallet()) let { walletAddress, walletError } = $(useWallet())
let isLoading = $ref(false)
let subscriptionToken = $ref<SubscriptionToken | null>(null) let subscriptionToken = $ref<SubscriptionToken | null>(null)
let subscriptionConfigured = $ref<boolean | null>(null) let subscriptionConfigured = $ref<boolean | null>(null)
let subscription = $ref<Subscription | null>(null) let subscription = $ref<Subscription | null>(null)
@ -128,6 +141,7 @@ async function checkSubscription() {
walletError = "Incorrect wallet address" walletError = "Incorrect wallet address"
return return
} }
isLoading = true
const signer = getSigner() const signer = getSigner()
subscription = await getSubscriptionInfo( subscription = await getSubscriptionInfo(
instance.blockchain_contract_address, instance.blockchain_contract_address,
@ -143,6 +157,7 @@ async function checkSubscription() {
signer, signer,
) )
} }
isLoading = false
} }
function canConfigureSubscription(): boolean { function canConfigureSubscription(): boolean {
@ -162,6 +177,7 @@ async function onConfigureSubscription() {
) { ) {
return return
} }
isLoading = true
const signer = getSigner() const signer = getSigner()
const authToken = ensureAuthToken() const authToken = ensureAuthToken()
const pricePerSec = getPricePerSec( const pricePerSec = getPricePerSec(
@ -169,13 +185,20 @@ async function onConfigureSubscription() {
subscriptionToken.decimals, subscriptionToken.decimals,
) )
const signature = await getSubscriptionAuthorization(authToken, pricePerSec) const signature = await getSubscriptionAuthorization(authToken, pricePerSec)
const transaction = await configureSubscription( let transaction
try {
transaction = await configureSubscription(
instance.blockchain_contract_address, instance.blockchain_contract_address,
signer, signer,
profileEthereumAddress, profileEthereumAddress,
pricePerSec, pricePerSec,
signature, signature,
) )
} catch (error) {
console.error(error)
isLoading = false
return
}
await transaction.wait() await transaction.wait()
subscriptionConfigured = true subscriptionConfigured = true
subscription = await getSubscriptionInfo( subscription = await getSubscriptionInfo(
@ -183,6 +206,7 @@ async function onConfigureSubscription() {
signer, signer,
profileEthereumAddress, profileEthereumAddress,
) )
isLoading = false
} }
async function onCheckSubsciptionState() { async function onCheckSubsciptionState() {
@ -193,6 +217,7 @@ async function onCheckSubsciptionState() {
) { ) {
return return
} }
isLoading = true
const signer = getSigner() const signer = getSigner()
subscriptionState = await getSubscriptionState( subscriptionState = await getSubscriptionState(
instance.blockchain_contract_address, instance.blockchain_contract_address,
@ -200,6 +225,7 @@ async function onCheckSubsciptionState() {
subscriberAddress, subscriberAddress,
profileEthereumAddress, profileEthereumAddress,
) )
isLoading = false
} }
async function onWithdrawReceived() { async function onWithdrawReceived() {
@ -209,6 +235,7 @@ async function onWithdrawReceived() {
) { ) {
return return
} }
isLoading = true
const signer = getSigner() const signer = getSigner()
await withdrawReceived( await withdrawReceived(
instance.blockchain_contract_address, instance.blockchain_contract_address,
@ -216,6 +243,7 @@ async function onWithdrawReceived() {
subscriberAddress, subscriberAddress,
) )
subscriptionState = null subscriptionState = null
isLoading = false
} }
</script> </script>
@ -231,10 +259,22 @@ async function onWithdrawReceived() {
text-align: center; text-align: center;
} }
.wallet-error {
color: $error-color;
}
.info { .info {
background-color: $block-background-color; background-color: $block-background-color;
border-radius: $block-border-radius; border-radius: $block-border-radius;
display: flex;
flex-direction: column;
gap: $block-inner-padding / 2;
padding: $block-inner-padding; padding: $block-inner-padding;
.price {
font-size: 16px;
font-weight: bold;
}
} }
.setup { .setup {
@ -261,8 +301,17 @@ async function onWithdrawReceived() {
} }
.withdraw { .withdraw {
align-items: center;
display: flex; display: flex;
flex-wrap: wrap; flex-direction: column;
gap: $block-inner-padding / 2; gap: $block-inner-padding;
input {
width: 400px;
}
}
.loader {
margin: 0 auto;
} }
</style> </style>