Skip to content

Commit

Permalink
Implement API response handling in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
ikem-legend committed Jul 4, 2024
1 parent dce3c74 commit 684c315
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 21 deletions.
12 changes: 12 additions & 0 deletions web/public/error-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions web/public/success-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions web/public/warning-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 48 additions & 21 deletions web/src/Faucet.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { onMount } from 'svelte';
import { getAddress } from '@ethersproject/address';
import { CloudflareProvider } from '@ethersproject/providers';
import { setDefaults as setToast, toast } from 'bulma-toast';
let input = null;
let faucetInfo = {
Expand All @@ -15,6 +14,7 @@
let mounted = false;
let hcaptchaLoaded = false;
let feedback = null;
onMount(async () => {
const res = await fetch('/api/info');
Expand All @@ -35,18 +35,13 @@
});
}
setToast({
position: 'bottom-center',
dismissible: true,
pauseOnHover: true,
closeOnClick: false,
animate: { in: 'fadeIn', out: 'fadeOut' },
});
async function handleRequest() {
let address = input;
if (address === null) {
toast({ message: 'input required', type: 'is-warning' });
feedback = {
message: 'Please enter a valid address or ENS name',
type: 'error',
}
return;
}
Expand All @@ -55,19 +50,28 @@
const provider = new CloudflareProvider();
address = await provider.resolveName(address);
if (!address) {
toast({ message: 'invalid ENS name', type: 'is-warning' });
feedback = {
message: 'Invalid ENS name',
type: 'error',
}
return;
}
} catch (error) {
toast({ message: error.reason, type: 'is-warning' });
feedback = {
message: error.reason,
type: 'error',
}
return;
}
}
try {
address = getAddress(address);
} catch (error) {
toast({ message: error.reason, type: 'is-warning' });
feedback = {
message: error.reason,
type: 'error',
}
return;
}
Expand All @@ -92,8 +96,10 @@
});
let { msg } = await res.json();
let type = res.ok ? 'is-success' : 'is-warning';
toast({ message: msg, type });
feedback = {
message: msg,
type: msg.includes('exceeded') ? 'warning' : 'success',
}
} catch (err) {
console.error(err);
}
Expand Down Expand Up @@ -123,8 +129,7 @@
<div class="navbar-brand">
<a class="navbar-item" href="../..">
<span class="icon">
<!-- svelte-ignore a11y-missing-attribute -->
<img src="../faucet-logo.svg"/>
<img src="../faucet-logo.svg" alt="logo" />
</span>
<span><b>{faucetInfo.symbol} Faucet</b></span>
</a>
Expand All @@ -137,8 +142,7 @@
href="https://github.com/liskhq/lsk-faucet"
>
<span class="icon">
<!-- svelte-ignore a11y-missing-attribute -->
<img src="../github-logo.svg"/>
<img src="../github-logo.svg" alt="github-logo" />
</span>
<span>View Source</span>
</a>
Expand All @@ -150,7 +154,7 @@
</div>

<div class="hero-body">
<div class="container has-text-centered container-postion">
<div class="container has-text-centered container-position">
<div class="column is-8 is-offset-2">
<h1 class="title">
Receive {faucetInfo.payout}
Expand Down Expand Up @@ -179,6 +183,11 @@
</button>
</p>
</div>
{#if feedback}
<div class="feedback"><span class={feedback.type}>
<img src={`../${feedback.type}-icon.svg`} alt="info" />{feedback.message}</span>
</div>
{/if}
</div>
</div>
</div>
Expand Down Expand Up @@ -244,8 +253,26 @@
.request-btn {
border-radius: 0px 8px 8px 0px;
}
.container-postion {
.container-position {
max-width: 65%;
bottom: 80px;
}
.feedback {
font-size: 16px;
line-height: 22px;
margin-top: 4px;
}
.feedback img {
margin-right: 8px;
vertical-align: bottom;
}
.feedback .success {
color: #F2F4F7;
}
.feedback .warning {
color: #FEC84B;
}
.feedback .error {
color: #F04437;
}
</style>

0 comments on commit 684c315

Please sign in to comment.