diff --git a/src/modules/donation/constants.ts b/src/modules/donation/constants.ts index 7eea5060..8a1efd55 100644 --- a/src/modules/donation/constants.ts +++ b/src/modules/donation/constants.ts @@ -1,3 +1,3 @@ -export const DONATION_MIN_AMOUNT = 0.0001; +export const DONATION_MIN_NEAR_AMOUNT = 0.1; export const DONATION_MAX_MESSAGE_LENGTH = 100; diff --git a/src/modules/donation/hooks/forms.ts b/src/modules/donation/hooks/forms.ts index c06ec998..e0bd9946 100644 --- a/src/modules/donation/hooks/forms.ts +++ b/src/modules/donation/hooks/forms.ts @@ -3,6 +3,7 @@ import { useCallback } from "react"; import { zodResolver } from "@hookform/resolvers/zod"; import { SubmitHandler, useForm } from "react-hook-form"; +import { dispatch } from "@/app/_store"; import { NEAR_TOKEN_DENOM } from "@/common/constants"; import useIsHuman from "@/modules/core/hooks/useIsHuman"; @@ -18,9 +19,12 @@ export const useDonationForm = (_: DonationFormParameters) => { const isFtDonation = watch("tokenId") !== NEAR_TOKEN_DENOM; const isSenderHumanVerified = useIsHuman(watch("recipientAccountId")); - const onSubmit: SubmitHandler = useCallback((data) => { - console.table(data); - }, []); + const onSubmit: SubmitHandler = useCallback( + (data) => dispatch.donation.submit(data), + [], + ); + + console.table(form.getValues()); return { isFtDonation, diff --git a/src/modules/donation/models.ts b/src/modules/donation/models.ts index f3b79e7b..1291b673 100644 --- a/src/modules/donation/models.ts +++ b/src/modules/donation/models.ts @@ -15,7 +15,10 @@ import { NEAR_TOKEN_DENOM } from "@/common/constants"; import { donateNearDirectly } from "@/common/contracts/potlock/donate"; import { DirectDonation } from "@/common/contracts/potlock/interfaces/donate.interfaces"; -import { DONATION_MAX_MESSAGE_LENGTH, DONATION_MIN_AMOUNT } from "./constants"; +import { + DONATION_MAX_MESSAGE_LENGTH, + DONATION_MIN_NEAR_AMOUNT, +} from "./constants"; export type DonationParameters = ByAccountId | ByPotId; @@ -61,7 +64,10 @@ export const donationSchema = object({ amount: number() .positive() .finite() - .min(DONATION_MIN_AMOUNT) + .min( + DONATION_MIN_NEAR_AMOUNT, + `The minimum donation amount is ${DONATION_MIN_NEAR_AMOUNT} NEAR.`, + ) .refine( (n) => !number().int().safeParse(n).success, "Must be a floating point number.", @@ -75,7 +81,9 @@ export const donationSchema = object({ bypassProtocolFee: boolean().default(false), bypassChefFee: boolean().default(false), -}); +}).refine(({ tokenId, amount }) => { + return tokenId === NEAR_TOKEN_DENOM || amount > 0.0; +}, "Incorrect donation amount"); export type DonationInputs = FromSchema; @@ -128,17 +136,15 @@ export const donationModel = createModel()({ }, effects: (dispatch) => ({ - submitDonation({ - amount, - donationType, - recipientAccountId, - }: DonationInputs) { + submit({ amount, donationType, recipientAccountId }: DonationInputs) { switch (donationType) { case DonationType.direct: return donateNearDirectly( { recipient_id: recipientAccountId }, amount, - ); + ) + .then(dispatch.success) + .catch(dispatch.failure); } }, }),