Skip to content

Commit

Permalink
Improvements for forms
Browse files Browse the repository at this point in the history
  • Loading branch information
kkosiorowska committed Dec 29, 2023
1 parent 062cadf commit 1b1b9ab
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
26 changes: 7 additions & 19 deletions dapp/src/components/Modals/Staking/StakeForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,21 @@ import { FormikErrors, withFormik } from "formik"
import { ModalStep } from "../../../contexts"
import FormBase, { FormBaseProps, FormValues } from "../../shared/FormBase"
import { useTransactionContext, useWalletContext } from "../../../hooks"
import { formatSatoshiAmount } from "../../../utils"
import { BITCOIN_MIN_AMOUNT, ERRORS } from "../../../constants"
import { getErrorsObj, validateTokenAmount } from "../../../utils"

const StakeFormik = withFormik<
{ onSubmitForm: (values: FormValues) => void } & FormBaseProps,
FormValues
>({
validate: ({ amount: value }, { tokenBalance }) => {
mapPropsToValues: () => ({
amount: "",
}),
validate: async ({ amount }, { tokenBalance }) => {
const errors: FormikErrors<FormValues> = {}

if (!value) errors.amount = ERRORS.REQUIRED
else {
const valueInBI = BigInt(value)
const maxValueInBI = BigInt(tokenBalance)
const minValueInBI = BigInt(BITCOIN_MIN_AMOUNT)
errors.amount = validateTokenAmount(amount, tokenBalance)

const isMaximumValueExceeded = valueInBI > maxValueInBI
const isMinimumValueFulfilled = valueInBI > minValueInBI

if (isMaximumValueExceeded) errors.amount = ERRORS.EXCEEDED_VALUE
else if (!isMinimumValueFulfilled)
errors.amount = ERRORS.INSUFFICIENT_VALUE(
formatSatoshiAmount(BITCOIN_MIN_AMOUNT),
)
}

return errors
return getErrorsObj(errors)
},
handleSubmit: (values, { props }) => {
props.onSubmitForm(values)
Expand Down
7 changes: 0 additions & 7 deletions dapp/src/constants/staking.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
export const BITCOIN_MIN_AMOUNT = "1000000" // 0.01 BTC

export const ERRORS = {
REQUIRED: "Required.",
EXCEEDED_VALUE: "The amount exceeds your current balance.",
INSUFFICIENT_VALUE: (minValue: string) =>
`The minimum amount must be at least ${minValue} BTC.`,
}
35 changes: 35 additions & 0 deletions dapp/src/utils/forms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { BITCOIN_MIN_AMOUNT } from "../constants"
import { formatSatoshiAmount } from "./numbers"

const ERRORS = {
REQUIRED: "Required.",
EXCEEDED_VALUE: "The amount exceeds your current balance.",
INSUFFICIENT_VALUE: (minValue: string) =>
`The minimum amount must be at least ${minValue} BTC.`,
}

export function getErrorsObj<T>(errors: { [key in keyof T]: string }) {
return (Object.keys(errors) as Array<keyof T>).every((name) => !errors[name])
? {}
: errors
}

export function validateTokenAmount(
value: string,
tokenBalance: string,
): string | undefined {
if (!value) return ERRORS.REQUIRED

const valueInBI = BigInt(value)
const maxValueInBI = BigInt(tokenBalance)
const minValueInBI = BigInt(BITCOIN_MIN_AMOUNT)

const isMaximumValueExceeded = valueInBI > maxValueInBI
const isMinimumValueFulfilled = valueInBI >= minValueInBI

if (isMaximumValueExceeded) return ERRORS.EXCEEDED_VALUE
if (!isMinimumValueFulfilled)
return ERRORS.INSUFFICIENT_VALUE(formatSatoshiAmount(BITCOIN_MIN_AMOUNT))

return undefined
}
1 change: 1 addition & 0 deletions dapp/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./numbers"
export * from "./address"
export * from "./forms"

0 comments on commit 1b1b9ab

Please sign in to comment.