From 1b1b9ab6324d7ea371cd028cd2333af5360ddf5a Mon Sep 17 00:00:00 2001 From: Karolina Kosiorowska Date: Fri, 29 Dec 2023 09:07:32 +0100 Subject: [PATCH] Improvements for forms --- .../components/Modals/Staking/StakeForm.tsx | 26 ++++---------- dapp/src/constants/staking.ts | 7 ---- dapp/src/utils/forms.ts | 35 +++++++++++++++++++ dapp/src/utils/index.ts | 1 + 4 files changed, 43 insertions(+), 26 deletions(-) create mode 100644 dapp/src/utils/forms.ts diff --git a/dapp/src/components/Modals/Staking/StakeForm.tsx b/dapp/src/components/Modals/Staking/StakeForm.tsx index 3af0c6712..c201dbc60 100644 --- a/dapp/src/components/Modals/Staking/StakeForm.tsx +++ b/dapp/src/components/Modals/Staking/StakeForm.tsx @@ -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 = {} - 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) diff --git a/dapp/src/constants/staking.ts b/dapp/src/constants/staking.ts index 19cd34be0..421c57bb5 100644 --- a/dapp/src/constants/staking.ts +++ b/dapp/src/constants/staking.ts @@ -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.`, -} diff --git a/dapp/src/utils/forms.ts b/dapp/src/utils/forms.ts new file mode 100644 index 000000000..70249a13d --- /dev/null +++ b/dapp/src/utils/forms.ts @@ -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(errors: { [key in keyof T]: string }) { + return (Object.keys(errors) as Array).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 +} diff --git a/dapp/src/utils/index.ts b/dapp/src/utils/index.ts index 613e0f071..563f2e6d1 100644 --- a/dapp/src/utils/index.ts +++ b/dapp/src/utils/index.ts @@ -1,2 +1,3 @@ export * from "./numbers" export * from "./address" +export * from "./forms"