AddLiquidityPanel: handle loading, errors

This commit is contained in:
mantikoros 2022-04-21 12:23:46 -05:00
parent c3cde8485e
commit a250d3420e

View File

@ -1,4 +1,6 @@
import clsx from 'clsx'
import { useState } from 'react' import { useState } from 'react'
import { Contract } from '../../common/contract' import { Contract } from '../../common/contract'
import { formatMoney } from '../../common/util/format' import { formatMoney } from '../../common/util/format'
import { useUser } from '../hooks/use-user' import { useUser } from '../hooks/use-user'
@ -15,8 +17,10 @@ export function AddLiquidityPanel(props: { contract: Contract }) {
const [amount, setAmount] = useState<number | undefined>(undefined) const [amount, setAmount] = useState<number | undefined>(undefined)
const [error, setError] = useState<string | undefined>(undefined) const [error, setError] = useState<string | undefined>(undefined)
const [isSuccess, setIsSuccess] = useState(false) const [isSuccess, setIsSuccess] = useState(false)
const [isLoading, setIsLoading] = useState(false)
const onAmountChange = (amount: number | undefined) => { const onAmountChange = (amount: number | undefined) => {
setIsSuccess(false)
setAmount(amount) setAmount(amount)
// Check for errors. // Check for errors.
@ -34,14 +38,19 @@ export function AddLiquidityPanel(props: { contract: Contract }) {
const submit = () => { const submit = () => {
if (!amount) return if (!amount) return
setIsLoading(true)
setIsSuccess(false) setIsSuccess(false)
addLiquidity({ amount, contractId }) addLiquidity({ amount, contractId })
.then((r) => .then((r) => {
r.status === 'success' if (r.status === 'success') {
? (setIsSuccess(true), setError(undefined)) setIsSuccess(true)
: setError('Server error') setError(undefined)
) setIsLoading(false)
} else {
setError('Server error')
}
})
.catch((e) => setError('Server error')) .catch((e) => setError('Server error'))
} }
@ -55,8 +64,13 @@ export function AddLiquidityPanel(props: { contract: Contract }) {
onChange={onAmountChange} onChange={onAmountChange}
label="M$" label="M$"
error={error} error={error}
disabled={isLoading}
/> />
<button className="btn btn-primary ml-2" onClick={submit}> <button
className={clsx('btn btn-primary ml-2', isLoading && 'btn-disabled')}
onClick={submit}
disabled={isLoading}
>
Add Add
</button> </button>
</Row> </Row>
@ -64,6 +78,8 @@ export function AddLiquidityPanel(props: { contract: Contract }) {
{isSuccess && amount && ( {isSuccess && amount && (
<div>Success! Added {formatMoney(amount)} in liquidity.</div> <div>Success! Added {formatMoney(amount)} in liquidity.</div>
)} )}
{isLoading && <div>Processing...</div>}
</> </>
) )
} }