mirror of
https://github.com/oceanprotocol/market.git
synced 2024-11-15 09:44:53 +01:00
cleanup
This commit is contained in:
parent
823513d4ee
commit
f60163b54d
@ -12,7 +12,7 @@ import Tooltip from '../../../atoms/Tooltip'
|
|||||||
import { ReactComponent as Caret } from '../../../../images/caret.svg'
|
import { ReactComponent as Caret } from '../../../../images/caret.svg'
|
||||||
import { graphql, useStaticQuery } from 'gatsby'
|
import { graphql, useStaticQuery } from 'gatsby'
|
||||||
import * as Yup from 'yup'
|
import * as Yup from 'yup'
|
||||||
import { Field, Formik } from 'formik'
|
import { Field, FieldInputProps, Formik } from 'formik'
|
||||||
import Input from '../../../atoms/Input'
|
import Input from '../../../atoms/Input'
|
||||||
|
|
||||||
const contentQuery = graphql`
|
const contentQuery = graphql`
|
||||||
@ -38,7 +38,11 @@ const contentQuery = graphql`
|
|||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
const initialValues: Partial<{ amount: number }> = {
|
interface FormAddLiquidity {
|
||||||
|
amount: number
|
||||||
|
}
|
||||||
|
|
||||||
|
const initialValues: FormAddLiquidity = {
|
||||||
amount: undefined
|
amount: undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,11 +68,22 @@ export default function Add({
|
|||||||
|
|
||||||
const { ocean, accountId, balance } = useOcean()
|
const { ocean, accountId, balance } = useOcean()
|
||||||
const [txId, setTxId] = useState<string>('')
|
const [txId, setTxId] = useState<string>('')
|
||||||
const [isLoading, setIsLoading] = useState<boolean>()
|
|
||||||
const [coin, setCoin] = useState<string>('OCEAN')
|
const [coin, setCoin] = useState<string>('OCEAN')
|
||||||
const [dtBalance, setDtBalance] = useState<string>()
|
const [dtBalance, setDtBalance] = useState<string>()
|
||||||
const [amountMax, setAmountMax] = useState<string>()
|
const [amountMax, setAmountMax] = useState<string>()
|
||||||
|
|
||||||
|
// Live validation rules
|
||||||
|
// https://github.com/jquense/yup#number
|
||||||
|
const validationSchema = Yup.object().shape<FormAddLiquidity>({
|
||||||
|
amount: Yup.number()
|
||||||
|
.min(1, 'Must be more or equal to 1')
|
||||||
|
.max(
|
||||||
|
Number(amountMax),
|
||||||
|
`Must be less or equal to ${Number(amountMax).toFixed(2)}`
|
||||||
|
)
|
||||||
|
.required('Required')
|
||||||
|
})
|
||||||
|
|
||||||
// Get datatoken balance when datatoken selected
|
// Get datatoken balance when datatoken selected
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!ocean || coin === 'OCEAN') return
|
if (!ocean || coin === 'OCEAN') return
|
||||||
@ -94,9 +109,8 @@ export default function Add({
|
|||||||
getMaximum()
|
getMaximum()
|
||||||
}, [ocean, poolAddress, coin])
|
}, [ocean, poolAddress, coin])
|
||||||
|
|
||||||
|
// Submit
|
||||||
async function handleAddLiquidity(amount: number, resetForm: () => void) {
|
async function handleAddLiquidity(amount: number, resetForm: () => void) {
|
||||||
setIsLoading(true)
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result =
|
const result =
|
||||||
coin === 'OCEAN'
|
coin === 'OCEAN'
|
||||||
@ -112,21 +126,9 @@ export default function Add({
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error.message)
|
console.error(error.message)
|
||||||
toast.error(error.message)
|
toast.error(error.message)
|
||||||
} finally {
|
|
||||||
setIsLoading(false)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const validationSchema = Yup.object().shape<{ amount: number }>({
|
|
||||||
amount: Yup.number()
|
|
||||||
.min(1, 'Must be more or equal to 1')
|
|
||||||
.max(
|
|
||||||
Number(amountMax),
|
|
||||||
`Must be less or equal to ${Number(amountMax).toFixed(2)}`
|
|
||||||
)
|
|
||||||
.required('Required')
|
|
||||||
})
|
|
||||||
|
|
||||||
// TODO: this is only a prototype and is an accessibility nightmare.
|
// TODO: this is only a prototype and is an accessibility nightmare.
|
||||||
// Needs to be refactored to either use custom select element instead of tippy.js,
|
// Needs to be refactored to either use custom select element instead of tippy.js,
|
||||||
// or use <button> in this implementation.
|
// or use <button> in this implementation.
|
||||||
@ -146,12 +148,13 @@ export default function Add({
|
|||||||
initialValues={initialValues}
|
initialValues={initialValues}
|
||||||
validationSchema={validationSchema}
|
validationSchema={validationSchema}
|
||||||
onSubmit={async (values, { setSubmitting, resetForm }) => {
|
onSubmit={async (values, { setSubmitting, resetForm }) => {
|
||||||
// kick off
|
|
||||||
await handleAddLiquidity(values.amount, resetForm)
|
await handleAddLiquidity(values.amount, resetForm)
|
||||||
setSubmitting(false)
|
setSubmitting(false)
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{({ values, setFieldValue, submitForm }) => {
|
{({ values, isSubmitting, setFieldValue, submitForm }) => {
|
||||||
|
// TODO: move these 2 const to some useEffect() so it dos not
|
||||||
|
// constantly re-renders all the time
|
||||||
const newPoolTokens =
|
const newPoolTokens =
|
||||||
totalBalance &&
|
totalBalance &&
|
||||||
((values.amount / Number(totalBalance.ocean)) * 100).toFixed(2)
|
((values.amount / Number(totalBalance.ocean)) * 100).toFixed(2)
|
||||||
@ -201,7 +204,8 @@ export default function Add({
|
|||||||
)}
|
)}
|
||||||
</Field>
|
</Field>
|
||||||
|
|
||||||
{(Number(balance.ocean) || dtBalance) > values.amount && (
|
{(Number(balance.ocean) || dtBalance) >
|
||||||
|
(values.amount || 0) && (
|
||||||
<Button
|
<Button
|
||||||
className={styles.buttonMax}
|
className={styles.buttonMax}
|
||||||
style="text"
|
style="text"
|
||||||
@ -226,7 +230,7 @@ export default function Add({
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Actions
|
<Actions
|
||||||
isLoading={isLoading}
|
isLoading={isSubmitting}
|
||||||
loaderMessage="Adding Liquidity..."
|
loaderMessage="Adding Liquidity..."
|
||||||
actionName={content.action}
|
actionName={content.action}
|
||||||
action={submitForm}
|
action={submitForm}
|
||||||
|
Loading…
Reference in New Issue
Block a user