Skip to content

Commit e29a980

Browse files
committed
feat: add faucet fetch
1 parent e7d8475 commit e29a980

File tree

4 files changed

+33
-44
lines changed

4 files changed

+33
-44
lines changed

src/components/faucet/FaucetDetail.vue

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
ref="DOMpopup"
66
class="popup fixed flex flex-col items-center rounded w-[90vw] max-w-[36rem] bg-grey-300 top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 z-[1000] justify-center text-grey-50 before:absolute before:top-0 before:bottom-0 before:left-0 before:right-0 before:rounded before:bg-500 before:z-min after:absolute after:top-px after:left-px after:bottom-px after:right-px after:bg-grey-500 after:rounded after:z-min"
77
>
8-
<div ref="DOMFaucetRequest" class="p-12">
8+
<div ref="DOMFaucetRequest" class="py-12 px-20 w-full">
99
<FaucetContentForm :name="store.selectedFaucet.name ?? 'Faucet'" :options="store.faucetAmount" v-show="store.contentStep === 0" class="js-faucetform opacity-100" :error="error" @requestFaucet="requestFaucet" />
1010
<div>
1111
<div ref="gnoRequestLogo" v-show="store.contentStep >= 1" class="opacity-0">
@@ -31,8 +31,7 @@ import GnoJSON from '@/assets/lottie/logo.json'
3131
3232
import { useFaucetDetail } from '@/stores/faucetDetail'
3333
34-
// import { req } from '@/data/mockedRequest'
35-
import { Status, Request, FaucetResponse } from '@/types'
34+
import { Request, FaucetResponse } from '@/types'
3635
3736
const txLink = ref('')
3837
@@ -44,7 +43,7 @@ const DOMFaucetRequest = ref<HTMLElement | null>(null)
4443
const gnoRequestLogo = ref<HTMLElement | null>(null)
4544
4645
const popupHeight = reactive({ from: 0, to: 0 })
47-
const error = ref<Status | null>(null)
46+
const error = ref<string | null>(null)
4847
4948
const requestFaucet = async (address: string, amount: number, secret: string) => {
5049
popupHeight.from = DOMpopup.value?.getBoundingClientRect().height ?? 0
@@ -60,38 +59,36 @@ const requestFaucet = async (address: string, amount: number, secret: string) =>
6059
})
6160
gsap.to(gnoRequestLogo.value, { autoAlpha: 1, delay: 0.5 })
6261
63-
console.log(address)
64-
console.log(amount)
65-
console.log(secret)
66-
67-
const opt: Request = {
68-
to: address,
69-
amount: amount.toString(),
70-
captcha: secret,
71-
}
62+
// default
63+
const minTimer = new Promise((resolve) => setTimeout(resolve, 2000))
7264
7365
try {
74-
const { data }: { data: FaucetResponse } = await axios.post(store.selectedFaucet.url, {
75-
opt,
66+
const response = await fetch(store.selectedFaucet.url, {
67+
method: 'POST',
7668
headers: {
77-
Accept: 'application/json',
78-
'Content-Type': 'application/json;charset=UTF-8',
69+
'Content-Type': 'application/json',
7970
},
71+
body: JSON.stringify({
72+
to: address,
73+
amount: amount.toString(),
74+
captcha: secret,
75+
}),
8076
})
8177
82-
console.log(data)
78+
await minTimer
79+
const faucetResponse = await response.json()
80+
81+
store.status = !faucetResponse.result ? 'error' : 'success'
8382
84-
//TODO: check types and error / result content
85-
if (data.error) {
86-
error.value = data.error
83+
// Check the faucet response
84+
if (!response.ok || store.status === 'error') {
85+
error.value = faucetResponse.error.message
8786
store.contentStep = 0
88-
throw new Error(data.error)
8987
} else {
90-
store.status = 'success'
91-
txLink.value = data.result ?? ''
88+
txLink.value = faucetResponse.result ?? ''
9289
}
9390
} catch (e) {
94-
console.error('error ' + e)
91+
console.log('error :' + e)
9592
}
9693
}
9794

src/components/faucet/content/FaucetContentForm.vue

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
<div>
33
<h2 class="text-600 mb-12">{{ name }}</h2>
44
<form class="w-full space-y-12" @submit.prevent="requestFaucet">
5-
<Input :label="'Enter your wallet address'" :placeholder="'e.g. g1juwee0ynsdvaukvxk3j5s4cl6nn24uxwlydxrl'" v-model="bindAddress" />
5+
<Input :label="'Enter your wallet address'" :placeholder="'e.g. g1juwee0ynsdvaukvxk3j5s4cl6nn24uxwlydxrl'" v-model="bindAddress" required />
66
<Select v-if="store.selectedFaucet.amounts" :label="'Select faucet amount'" :options="options" @update="(option) => SelectAmount(option)" />
77
<Recaptcha @validation="captchaValidation" />
88
<div>
99
<div class="flex gap-4">
10-
<Button text="Request drip" class="w-full" type="submit" />
1110
<Button text="Cancel" variant="outline" @click.prevent="() => closePopup()" class="w-full" />
11+
<Button text="Request drip" class="w-full" type="submit" :disabled="captchaValid === false || bindAddress === ''" />
1212
</div>
13-
<div v-if="error" class="text-center text-red-200 mt-6">{{ errorDetail[error] }}</div>
13+
<div v-if="error" class="text-center text-red-200 mt-6">{{ error }}</div>
1414
</div>
1515
</form>
1616
</div>
@@ -27,14 +27,14 @@ import Recaptcha from '@/components/ui/Recaptcha.vue'
2727
import { useFaucetDetail } from '@/stores/faucetDetail'
2828
2929
import { SelectOption } from '@/components/ui/Select.vue'
30-
import { Status, Faucet } from '@/types'
30+
import { Faucet } from '@/types'
3131
3232
type Props = {
3333
name: Faucet['name']
3434
options: SelectOption[]
35-
error?: Status | null
35+
error?: string | null
3636
}
37-
const props = defineProps<Props>()
37+
defineProps<Props>()
3838
const store = useFaucetDetail()
3939
4040
const bindAddress = ref('')
@@ -54,15 +54,8 @@ const closePopup = () => store.popupToggle()
5454
5555
const emit = defineEmits(['requestFaucet'])
5656
57-
const errorDetail = {
58-
FAIL: 'Request failed',
59-
EMPTY: 'Not sufficient tokens available on this network. Try lowering the requested amount or try again later.',
60-
INVALID_ADDRESS: 'Invalid wallet address',
61-
BUSY_FAUCET: 'Too many requests',
62-
}
63-
6457
const requestFaucet = () => {
65-
if (captchaValid.value === false) return
58+
if (captchaValid.value === false || bindAddress.value === '') return
6659
emit('requestFaucet', bindAddress.value, store.selectedFaucet.amounts && amount.value?.value, captchaSecret.value)
6760
}
6861
</script>

src/components/ui/Button.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<template>
2-
<button class="p-4 rounded border border-light ease-in-out duration-300" :class="css[variant]">
3-
{{ text }}
4-
</button>
2+
<button class="p-4 rounded border border-light ease-in-out duration-300" :class="[css[variant], disabled && ['bg-grey-100 border-grey-100 hover:bg-grey-100 hover:!border-grey-100 hover:cursor-not-allowed']]">{{ text }}</button>
53
</template>
64

75
<script setup lang="ts">
86
type Props = {
97
text: string
108
variant?: 'default' | 'outline'
9+
disabled?: boolean
1110
}
1211
const props = withDefaults(defineProps<Props>(), {
1312
variant: 'default',
13+
disabled: false,
1414
})
1515
1616
const css = {

src/types/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export type RequestStatus = 'pending' | 'success' | 'error' | 'null'
22

3-
export type Status = 'FAIL' | 'EMPTY' | 'INVALID_ADDRESS' | 'BUSY_FAUCET' | undefined
43
export type Code = 'success' | 'error'
54

65
export interface Faucet {
@@ -20,5 +19,5 @@ export interface Request {
2019

2120
export interface FaucetResponse {
2221
result: string //json: "result
23-
error: Status //json: error,omitempty
22+
error: string //json: error,omitempty
2423
}

0 commit comments

Comments
 (0)