Refactor forms

- add a link to the faucet page from `Rinkeby` badge in header
- refactor the `<TextInput>` component to include
  - a loading indicator
  - a keyboard shortcut to submit the input by pressing the `enter` key
  - add a keyboard shortcut hint <kbd>⏎</kbd> that shows up whenever the
    input is ready to be submitted
  - a validation message
- align all pages with a single input form to have a similar look and feel
- improve validation messages and error handling in all forms

Signed-off-by: Rūdolfs Ošiņš <rudolfs@osins.org>
This commit is contained in:
Rūdolfs Ošiņš
2022-10-08 10:00:19 +02:00
parent f215f9f4fd
commit 3ce7aada0f
11 changed files with 629 additions and 491 deletions
+123 -110
View File
@@ -1,24 +1,24 @@
<script lang="ts">
import type { Config } from "@app/config";
import type { BigNumber } from "@ethersproject/bignumber";
import { session } from "@app/session";
import { setOpenGraphMetaTag, toWei } from "@app/utils";
import { formatEther } from "@ethersproject/units";
import { navigate } from "svelte-routing";
import {
calculateTimeLock,
getMaxWithdrawAmount,
lastWithdrawalByUser,
calculateTimeLock,
} from "./lib";
import Button from "@app/Button.svelte";
import TextInput from "@app/TextInput.svelte";
export let config: Config;
let amount: string;
let maxWithdrawAmount: BigNumber;
let lastWithdrawal: BigNumber;
let error: string | undefined;
let amount: string = "";
let loading: boolean = false;
let validationMessage: string | undefined = undefined;
let valid: boolean = false;
setOpenGraphMetaTag([
{ prop: "og:title", content: "Radicle Faucet" },
@@ -26,132 +26,145 @@
{ prop: "og:url", content: window.location.href },
]);
async function withdraw() {
const [state, message] = await isAbleToWithdraw(amount);
if (state === true) navigate("/faucet/withdraw", { state: { amount } });
else error = message;
}
async function withdraw(amount: string) {
if (!valid || !$session) {
return;
}
async function isAbleToWithdraw(amount: string): Promise<[boolean, string?]> {
loading = true;
try {
if (!$session) {
return [false];
}
if (!amount || amount === "0") {
return [false, "Not able to withdraw zero tokens"];
}
if (toWei(amount).gt(maxWithdrawAmount)) {
return [
false,
`Reduce amount, max withdrawal is ${formatEther(maxWithdrawAmount)}`,
];
}
const currentTime = new Date().getTime();
const timelock = await calculateTimeLock(amount, $session.signer, config);
// Converting a 10 digit to 13 digit timestamp by multiplying by 1000
// since JS doesn't display a correct Date string when passing a 10 digit timestamp.
const nextAvailableWithdraw = lastWithdrawal.add(timelock).mul(1000);
if (nextAvailableWithdraw.gt(currentTime)) {
return [
false,
`Not ready to withdraw, return after ${new Date(
nextAvailableWithdraw.toNumber(),
).toLocaleString("en-GB")}`,
];
const lastWithdrawal = await lastWithdrawalByUser(
$session.signer,
config,
);
const maxWithdrawAmount = await getMaxWithdrawAmount(
$session.signer,
config,
);
if (toWei(amount).gt(maxWithdrawAmount)) {
validationMessage = `Reduce amount, max withdrawal is ${formatEther(
maxWithdrawAmount,
)}.`;
return;
}
return [true];
} catch (e: any) {
console.error(e);
error = e.message;
// Converting a 10 digit to 13 digit timestamp by multiplying by 1000
// since JS doesn't display a correct Date string when passing a 10 digit
// timestamp.
const nextAvailableWithdraw = lastWithdrawal.add(timelock).mul(1000);
if (nextAvailableWithdraw.gt(currentTime)) {
validationMessage = `Not ready to withdraw, return after ${new Date(
nextAvailableWithdraw.toNumber(),
).toLocaleString("en-GB")}`;
return;
}
return [false];
navigate("/faucet/withdraw", { state: { amount } });
} catch (error) {
validationMessage = "There was an error, check the dev console.";
console.error(error);
} finally {
loading = false;
}
}
$: if ($session) {
getMaxWithdrawAmount($session.signer, config).then(
x => (maxWithdrawAmount = x),
);
lastWithdrawalByUser($session.signer, config).then(
x => (lastWithdrawal = x),
);
function validate(amount: string) {
if (amount === "") {
return { valid: false };
}
if (isNaN(Number(amount)) || Number(amount) <= 0) {
return {
valid: false,
validationMessage: "Please enter a positive number.",
};
}
return { valid: true };
}
$: ({ valid, validationMessage } = validate(amount));
</script>
<style>
.input-caption {
font-size: var(--font-size-medium);
text-align: left;
color: var(--color-secondary);
margin-bottom: 2rem;
margin-left: 0.5rem;
}
.input-main {
main {
display: flex;
align-items: flex-start;
color: var(--color-secondary);
flex-direction: column;
}
.error {
padding-left: 1rem;
padding-top: 1rem;
}
.description :global(p) {
padding: 0;
margin: 0;
}
.description.invalid {
color: var(--color-negative) !important;
}
.name {
display: flex;
gap: 1.5rem;
height: 100%;
justify-content: center;
padding-bottom: 24vh;
padding-top: 5rem;
width: 28rem;
}
.title {
color: var(--color-secondary);
font-size: var(--font-size-medium);
}
.subtitle {
color: var(--color-secondary);
}
.form {
display: flex;
gap: 1rem;
}
</style>
<svelte:head>
<title>Radicle: Faucet</title>
<title>Radicle &ndash; Faucet</title>
</svelte:head>
<main class="off-centered">
<div>
{#if config.network.name === "homestead"}
<div class="input-caption">
To get RAD tokens on <span class="txt-bold">
{config.network.name}
</span>
, please check the known exchanges.
</div>
{:else if !$session}
<div class="input-caption">
To get RAD tokens on <span class="txt-bold">
{config.network.name}
</span>
, please connect your wallet.
</div>
{:else}
<div class="input-caption">
Obtain RAD tokens on <span class="txt-bold">
{config.network.name}
</span>
</div>
<div class="input-main">
<div class="name">
<div style="width: 14.5rem;">
<TextInput
placeholder="Set amount to withdraw"
bind:value={amount}
on:input={() => (error = "")} />
</div>
<Button variant="primary" on:click={withdraw}>Withdraw</Button>
</div>
{#if error}
<div class="error description invalid txt-small faded">
{error}
</div>
{/if}
</div>
{/if}
<main>
<div class="title">
Obtain RAD tokens on <span class="txt-bold">
{config.network.name}
</span>
</div>
{#if config.network.name === "homestead"}
<div class="subtitle">
To get RAD tokens on <span class="txt-bold">
{config.network.name},
</span>
please
<br />
check
<a class="link" href="https://docs.radicle.xyz/get-involved/obtain-rad">
popular exchanges
</a>
&#8203;.
</div>
{:else if !$session}
<div class="subtitle">
To get RAD tokens on <span class="txt-bold">
{config.network.name}
</span>
&#8203;,
<br />
please connect your wallet.
</div>
{:else}
<div class="form">
<TextInput
autofocus
placeholder="Enter amount to withdraw"
{validationMessage}
on:submit={() => {
withdraw(amount);
}}
bind:value={amount}
{valid}
{loading} />
<Button
variant="primary"
on:click={() => withdraw(amount)}
disabled={!valid || loading}>
Withdraw
</Button>
</div>
{/if}
</main>
+76 -70
View File
@@ -8,102 +8,108 @@
export let config: Config;
let input = "";
let valid: boolean = false;
let validationMessage: string | undefined = undefined;
function register() {
navigate(`/registrations/${label}/form`);
if (!valid) {
return;
}
navigate(`/registrations/${ensName}/form`);
}
function validate(input: string): string[] {
const errors: string[] = [];
function validate(input: string) {
if (input === "") {
return { valid: false };
}
if (input && input.includes(".")) {
errors.push("Please do not use dots as separators.");
return {
valid: false,
validationMessage: "Please do not use dots as separators.",
};
}
if (input && input.length < 2) {
errors.push("Please enter a minimum of 2 characters.");
return {
valid: false,
validationMessage: "Please enter a minimum of 2 characters.",
};
}
if (input && input.length > 128) {
errors.push("Please enter a maximum of 128 characters.");
return {
valid: false,
validationMessage: "Please enter a maximum of 128 characters.",
};
}
return errors;
return { valid: true };
}
$: label = input.trim();
$: errors = validate(label);
$: ensName = input.trim();
$: ({ valid, validationMessage } = validate(ensName));
</script>
<style>
div.input-caption {
font-size: var(--font-size-medium);
text-align: left;
margin-left: 1.5rem;
padding-left: 1.5rem;
color: var(--color-secondary);
}
div.input-main {
main {
display: flex;
align-items: center;
flex-direction: row;
margin-left: 1.5rem;
flex-direction: column;
gap: 1.5rem;
height: 100%;
justify-content: center;
padding-bottom: 24vh;
padding-top: 5rem;
width: 32rem;
}
.title {
color: var(--color-secondary);
font-size: var(--font-size-medium);
}
.subtitle {
color: var(--color-secondary);
}
.name {
margin: 1rem;
width: 22rem;
}
.input-info {
position: absolute;
font-style: italic;
margin-top: 0.1rem;
}
.explainer {
width: 28rem;
margin: 1rem 0;
.form {
display: flex;
gap: 1rem;
}
</style>
<svelte:head>
<title>Radicle: Register</title>
<title>Radicle &ndash; Register</title>
</svelte:head>
<main class="off-centered">
<div>
<div class="input-caption">
Register a <span class="txt-bold">{config.registrar.domain}</span>
name
<div class="txt-small explainer">
Register a unique name with our ENS registrar, under the <span
class="txt-bold">
radicle.eth
</span>
domain (e.g. cloudhead.radicle.eth). Radicle names never expire and free
to register.
</div>
</div>
<div class="input-main">
<span class="name">
<TextInput bind:value={input} autofocus>
<svelte:fragment slot="right">
.{config.registrar.domain}
</svelte:fragment>
</TextInput>
{#if errors}
<div class="input-info">
{#each errors as error}
<div>{error}</div>
{/each}
</div>
{/if}
</span>
<main>
<div class="title">
Register a <span class="txt-bold">{config.registrar.domain}</span>
name
</div>
<Button
disabled={!label || errors.length !== 0}
variant="primary"
size="regular"
on:click={register}>
Check
</Button>
</div>
<div class="subtitle">
Register a unique name with our ENS registrar, under <br />
the
<span class="txt-bold">radicle.eth</span>
domain (e.g. cloudhead.radicle.eth).
<br />
Radicle names never expire and free to register.
</div>
<div class="form">
<TextInput
bind:value={input}
autofocus
on:submit={register}
{valid}
{validationMessage}>
<svelte:fragment slot="right">
.{config.registrar.domain}
</svelte:fragment>
</TextInput>
<Button
disabled={!valid}
variant="primary"
size="regular"
on:click={register}>
Check
</Button>
</div>
</main>
+153 -118
View File
@@ -1,52 +1,78 @@
<script lang="ts">
import { State, state } from "./state";
import { getInfo, withdrawVested } from "./vesting";
import type { VestingInfo } from "./vesting";
import type { Session } from "@app/session";
import type { Config } from "@app/config";
import Modal from "@app/Modal.svelte";
import type { VestingInfo } from "./vesting";
import * as utils from "@app/utils";
import Address from "@app/Address.svelte";
import { formatAddress, isAddressEqual } from "@app/utils";
import Button from "@app/Button.svelte";
import Modal from "@app/Modal.svelte";
import TextInput from "@app/TextInput.svelte";
import { state, getInfo, withdrawVested } from "./vesting";
export let config: Config;
export let session: Session | null;
let contractAddress = "";
let info: VestingInfo | null = null;
let validationMessage: string | undefined = undefined;
let valid: boolean = false;
async function loadContract(config: Config) {
state.set(State.Loading);
info = await getInfo(contractAddress, config);
state.set(State.Idle);
}
if (!valid) {
return;
}
function reset() {
info = null;
state.set(State.Idle);
state.set("loading");
try {
info = await getInfo(contractAddress, config);
} catch (error) {
validationMessage =
"Couldn't load contract, check dev console for details.";
console.error(error);
}
state.set("idle");
}
$: isBeneficiary =
info && session && isAddressEqual(info.beneficiary, session.address);
info && session && utils.isAddressEqual(info.beneficiary, session.address);
function validate(address: string) {
if (address === "") {
return { valid: false };
}
if (!utils.isAddress(address)) {
return {
valid: false,
validationMessage: "Please enter a valid Ethereum address.",
};
}
return { valid: true };
}
$: ({ valid, validationMessage } = validate(contractAddress));
</script>
<style>
.input-caption {
font-size: var(--font-size-medium);
text-align: left;
margin-left: 1.5rem;
margin-bottom: 2rem;
color: var(--color-secondary);
}
.input-main {
main {
display: flex;
align-items: center;
flex-direction: row;
margin-left: 1.5rem;
color: var(--color-secondary);
gap: 1rem;
flex-direction: column;
gap: 1.5rem;
height: 100%;
justify-content: center;
padding-bottom: 24vh;
padding-top: 5rem;
width: 38rem;
}
.title {
color: var(--color-secondary);
font-size: var(--font-size-medium);
}
.form {
display: flex;
gap: 1rem;
}
table {
table-layout: fixed;
@@ -63,95 +89,104 @@
<title>Radicle &ndash; Vesting</title>
</svelte:head>
<main class="off-centered">
<div>
{#if info}
<Modal>
<span slot="title">
{contractAddress}
</span>
<span slot="body">
{#if $state === State.Withdrawn}
Tokens successfully withdrawn to {formatAddress(info.beneficiary)}.
{:else}
<table>
<tr>
<td class="label">Beneficiary</td>
<td>
<Address
{config}
address={info.beneficiary}
compact
resolve />
</td>
</tr>
<tr>
<td class="label">Allocation</td>
<td>
{info.totalVesting}
<span class="txt-bold">{info.symbol}</span>
</td>
</tr>
<tr>
<td class="label">Withdrawn</td>
<td>
{info.withdrawn}
<span class="txt-bold">{info.symbol}</span>
</td>
</tr>
<tr>
<td class="label">Withdrawable</td>
<td>
{info.withdrawableBalance}
<span class="txt-bold">{info.symbol}</span>
</td>
</tr>
</table>
{/if}
</span>
<span slot="actions">
{#if isBeneficiary}
{#if $state === State.WithdrawingSign}
<Button disabled waiting={true} variant="primary">
Waiting for signature…
</Button>
{:else if $state === State.Withdrawing}
<Button disabled waiting={true} variant="primary">
Withdrawing…
</Button>
{:else if $state === State.Idle}
<Button
on:click={() => withdrawVested(contractAddress, config)}
variant="primary">
Withdraw
</Button>
{/if}
{/if}
<Button on:click={reset} variant="primary">Back</Button>
</span>
</Modal>
{:else}
<div class="input-caption">
Enter your Radicle <span class="txt-bold">vesting contract</span>
address
</div>
<div class="input-main">
<span class="name">
<div style="width: 25rem;">
<TextInput
autofocus
disabled={$state === State.Loading}
bind:value={contractAddress} />
</div>
</span>
<Button
on:click={() => loadContract(config)}
variant="primary"
waiting={$state === State.Loading}
disabled={$state === State.Loading}>
Load
</Button>
</div>
{/if}
</div>
</main>
{#if info}
<Modal>
<span slot="title">
{contractAddress}
</span>
<span slot="body">
{#if $state === "withdrawn"}
Tokens successfully withdrawn to {utils.formatAddress(
info.beneficiary,
)}.
{:else}
<table>
<tr>
<td class="label">Beneficiary</td>
<td>
<Address {config} address={info.beneficiary} compact resolve />
</td>
</tr>
<tr>
<td class="label">Allocation</td>
<td>
{info.totalVesting}
<span class="txt-bold">{info.symbol}</span>
</td>
</tr>
<tr>
<td class="label">Withdrawn</td>
<td>
{info.withdrawn}
<span class="txt-bold">{info.symbol}</span>
</td>
</tr>
<tr>
<td class="label">Withdrawable</td>
<td>
{info.withdrawableBalance}
<span class="txt-bold">{info.symbol}</span>
</td>
</tr>
</table>
{/if}
</span>
<span slot="actions">
{#if isBeneficiary}
{#if $state === "withdrawingSign"}
<Button disabled waiting={true} variant="primary">
Waiting for signature…
</Button>
{:else if $state === "withdrawing"}
<Button disabled waiting={true} variant="primary">
Withdrawing…
</Button>
{:else if $state === "idle"}
<Button
on:click={() => withdrawVested(contractAddress, config)}
variant="primary">
Withdraw
</Button>
{/if}
{/if}
<Button
on:click={() => {
info = null;
state.set("idle");
}}
variant="primary">
Back
</Button>
</span>
</Modal>
{:else}
<main>
<div class="title">
Your Radicle <span class="txt-bold">vesting contract</span>
</div>
<div class="form">
<TextInput
autofocus
placeholder="Enter vesting contract address"
{valid}
{validationMessage}
loading={$state === "loading"}
disabled={$state === "loading"}
on:submit={() => {
loadContract(config);
}}
bind:value={contractAddress} />
<Button
on:click={() => loadContract(config)}
variant="primary"
waiting={$state === "loading"}
disabled={!valid || $state === "loading"}>
Load
</Button>
</div>
</main>
{/if}
-18
View File
@@ -1,18 +0,0 @@
import { writable } from "svelte/store";
export enum State {
Error = -1,
Idle = 0,
Loading = 1,
Found = 2,
NotFound = 3,
WithdrawingSign = 4,
Withdrawing = 5,
Withdrawn = 6,
}
export const state = writable(State.Idle);
state.subscribe(s => {
console.debug("vesting.state", s);
});
+16 -10
View File
@@ -1,9 +1,11 @@
import { ethers } from "ethers";
import { formatBalance } from "@app/utils";
import * as session from "@app/session";
import { State, state } from "./state";
import type { Config } from "@app/config";
import { ethers } from "ethers";
import { assert } from "@app/error";
import { writable } from "svelte/store";
import * as session from "@app/session";
import * as utils from "@app/utils";
export interface VestingInfo {
token: string;
@@ -14,6 +16,10 @@ export interface VestingInfo {
withdrawn: string;
}
export const state = writable<
"idle" | "loading" | "withdrawingSign" | "withdrawing" | "withdrawn"
>("idle");
export async function withdrawVested(
address: string,
config: Config,
@@ -27,14 +33,14 @@ export async function withdrawVested(
);
const signer = config.signer;
state.set(State.WithdrawingSign);
state.set("withdrawingSign");
const tx = await contract.connect(signer).withdrawVested();
state.set(State.Withdrawing);
state.set("withdrawing");
await tx.wait();
session.state.refreshBalance(config);
state.set(State.Withdrawn);
state.set("withdrawn");
}
export async function getInfo(
@@ -63,8 +69,8 @@ export async function getInfo(
token: token,
symbol: symbol,
beneficiary: beneficiary,
totalVesting: formatBalance(total),
withdrawableBalance: formatBalance(withdrawable),
withdrawn: formatBalance(withdrawn),
totalVesting: utils.formatBalance(total),
withdrawableBalance: utils.formatBalance(withdrawable),
withdrawn: utils.formatBalance(withdrawn),
};
}