From a250d3420eb259a8975249735d4efc5a9dcad098 Mon Sep 17 00:00:00 2001 From: mantikoros Date: Thu, 21 Apr 2022 12:23:46 -0500 Subject: [PATCH] AddLiquidityPanel: handle loading, errors --- web/components/add-liquidity-panel.tsx | 28 ++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/web/components/add-liquidity-panel.tsx b/web/components/add-liquidity-panel.tsx index 392f3c24..4b2ed4c1 100644 --- a/web/components/add-liquidity-panel.tsx +++ b/web/components/add-liquidity-panel.tsx @@ -1,4 +1,6 @@ +import clsx from 'clsx' import { useState } from 'react' + import { Contract } from '../../common/contract' import { formatMoney } from '../../common/util/format' import { useUser } from '../hooks/use-user' @@ -15,8 +17,10 @@ export function AddLiquidityPanel(props: { contract: Contract }) { const [amount, setAmount] = useState(undefined) const [error, setError] = useState(undefined) const [isSuccess, setIsSuccess] = useState(false) + const [isLoading, setIsLoading] = useState(false) const onAmountChange = (amount: number | undefined) => { + setIsSuccess(false) setAmount(amount) // Check for errors. @@ -34,14 +38,19 @@ export function AddLiquidityPanel(props: { contract: Contract }) { const submit = () => { if (!amount) return + setIsLoading(true) setIsSuccess(false) addLiquidity({ amount, contractId }) - .then((r) => - r.status === 'success' - ? (setIsSuccess(true), setError(undefined)) - : setError('Server error') - ) + .then((r) => { + if (r.status === 'success') { + setIsSuccess(true) + setError(undefined) + setIsLoading(false) + } else { + setError('Server error') + } + }) .catch((e) => setError('Server error')) } @@ -55,8 +64,13 @@ export function AddLiquidityPanel(props: { contract: Contract }) { onChange={onAmountChange} label="M$" error={error} + disabled={isLoading} /> - @@ -64,6 +78,8 @@ export function AddLiquidityPanel(props: { contract: Contract }) { {isSuccess && amount && (
Success! Added {formatMoney(amount)} in liquidity.
)} + + {isLoading &&
Processing...
} ) }