From 6e0876c4eebaf3f8f1edd405b0f5cea8de75e1a1 Mon Sep 17 00:00:00 2001 From: "Carina.Akaia.io" Date: Mon, 24 Jun 2024 17:23:31 +0400 Subject: [PATCH] wip --- src/common/api/potlock/hooks.ts | 20 ++--- src/common/assets/svgs/twitter.tsx | 9 +- .../potlock/interfaces/donate.interfaces.ts | 8 +- .../donation/components/DonationFlow.tsx | 2 +- .../donation/components/DonationSuccess.tsx | 83 ++++++++++++++----- src/modules/donation/hooks/fees.ts | 9 +- src/modules/donation/models.ts | 11 ++- 7 files changed, 103 insertions(+), 39 deletions(-) diff --git a/src/common/api/potlock/hooks.ts b/src/common/api/potlock/hooks.ts index 3db6e433..c955c9e8 100644 --- a/src/common/api/potlock/hooks.ts +++ b/src/common/api/potlock/hooks.ts @@ -18,11 +18,11 @@ export const useAccounts = () => { return { ...queryResult, data: queryResult.data?.data }; }; -export const useAccount = ({ accountId }: ByAccountId) => { - const queryResult = swrHooks.useV1AccountsRetrieve2( - accountId, - POTLOCK_REQUEST_CONFIG, - ); +export const useAccount = ({ accountId }: Partial) => { + const queryResult = swrHooks.useV1AccountsRetrieve2(accountId ?? "unknown", { + ...POTLOCK_REQUEST_CONFIG, + swr: { enabled: Boolean(accountId) }, + }); return { ...queryResult, data: queryResult.data?.data }; }; @@ -52,11 +52,11 @@ export const useAccountDonationsReceived = ({ accountId }: ByAccountId) => { return { ...queryResult, data: queryResult.data?.data }; }; -export const usePot = ({ potId }: ByPotId) => { - const queryResult = swrHooks.useV1PotsRetrieve2( - potId, - POTLOCK_REQUEST_CONFIG, - ); +export const usePot = ({ potId }: Partial) => { + const queryResult = swrHooks.useV1PotsRetrieve2(potId ?? "unknown", { + ...POTLOCK_REQUEST_CONFIG, + swr: { enabled: Boolean(potId) }, + }); return { ...queryResult, data: queryResult.data?.data }; }; diff --git a/src/common/assets/svgs/twitter.tsx b/src/common/assets/svgs/twitter.tsx index 51d1f422..33f4df4e 100644 --- a/src/common/assets/svgs/twitter.tsx +++ b/src/common/assets/svgs/twitter.tsx @@ -1,7 +1,12 @@ import { Icon } from "./styles"; -const TwitterSvg = () => ( - +const TwitterSvg = (props: any) => ( + = ({ return h(DonationConfirmation, { form }); case "success": - return h(DonationSuccess, { result }); + return h(DonationSuccess, { result, form }); default: return ( diff --git a/src/modules/donation/components/DonationSuccess.tsx b/src/modules/donation/components/DonationSuccess.tsx index 5843efef..8ca5e992 100644 --- a/src/modules/donation/components/DonationSuccess.tsx +++ b/src/modules/donation/components/DonationSuccess.tsx @@ -1,17 +1,46 @@ -import { Check, Copy, XIcon } from "lucide-react"; +import { Check, Copy } from "lucide-react"; import Link from "next/link"; +import { UseFormReturn } from "react-hook-form"; +import { potlock } from "@/common/api/potlock"; +import TwitterSvg from "@/common/assets/svgs/twitter"; import { Button, DialogDescription, Skeleton } from "@/common/ui/components"; +import { ModalErrorBody } from "@/modules/core"; import { DonationBreakdown } from "./DonationBreakdown"; -import { DonationState } from "../models"; +import { useDonationFees } from "../hooks/fees"; +import { DonationInputs, DonationState } from "../models"; export type DonationSuccessProps = { result?: DonationState["successResult"]; + form: UseFormReturn; }; -export const DonationSuccess = ({ result }: DonationSuccessProps) => { - return ( +export const DonationSuccess = ({ result, form }: DonationSuccessProps) => { + const [potAccountId] = form.watch(["potAccountId"]); + + const { data: account, error: accountError } = potlock.useAccount({ + accountId: result?.recipient_id, + }); + + const isLoading = result === undefined || account === undefined; + + // !TODO: override with values from result + const fees = useDonationFees({ + amount: parseFloat(result?.amount ?? "0"), + referrerAccountId: result?.referrer_id ?? undefined, + potAccountId, + bypassProtocolFee: result?.protocol_fee === 0, + bypassChefFee: potAccountId !== undefined, + }); + + return accountError !== undefined ? ( + + ) : (
{
-

- Donation Successful -

+ {isLoading ? ( + + ) : ( +

+ Donation Successful +

+ )} - + + + + )}
@@ -63,16 +102,20 @@ export const DonationSuccess = ({ result }: DonationSuccessProps) => {
- {result ? ( - - ) : ( + {isLoading ? ( + ) : ( + )} -
- {`Txn Hash : ${result?.id}`} - -
+ {isLoading ? ( + + ) : ( +
+ {`Txn Hash : ${result.id}`} + +
+ )}
); }; diff --git a/src/modules/donation/hooks/fees.ts b/src/modules/donation/hooks/fees.ts index d298f17f..91b40dfb 100644 --- a/src/modules/donation/hooks/fees.ts +++ b/src/modules/donation/hooks/fees.ts @@ -3,7 +3,14 @@ import { TOTAL_FEE_BASIS_POINTS } from "@/modules/core/constants"; import { DonationInputs } from "../models"; -export type DonationFeeInputs = DonationInputs & {}; +export type DonationFeeInputs = Pick< + DonationInputs, + | "amount" + | "referrerAccountId" + | "potAccountId" + | "bypassProtocolFee" + | "bypassChefFee" +> & {}; export type DonationFees = { projectAllocationAmount: number; diff --git a/src/modules/donation/models.ts b/src/modules/donation/models.ts index 7b1c2f48..cd3ad78d 100644 --- a/src/modules/donation/models.ts +++ b/src/modules/donation/models.ts @@ -193,13 +193,22 @@ export const donationModel = createModel()({ amount, allocationStrategy, potDistributionStrategy, + referrerAccountId, + bypassProtocolFee, + message, ...props }: DonationSubmissionInputs & DonationInputs): Promise { if ("accountId" in props) { switch (allocationStrategy) { case DonationAllocationStrategyEnum.direct: return void donateNearDirectly( - { recipient_id: props.accountId }, + { + recipient_id: props.accountId, + message, + referrer_id: referrerAccountId, + bypass_protocol_fee: bypassProtocolFee, + }, + amount, ) .then((result) => {